Beispiel #1
0
def test_ClientBrokerWorker( basicParameters ):
    broker = plhc_api.CreateBrokerFromParameters( basicParameters.bpb.Build() )
    client = plhc_api.CreateClientFromParameters( basicParameters.cpb.Build() )
    clientProxy = plhc_api.CreateClientProxyFromParameters( basicParameters.cppb.Build() )
    requestHandlerCallback = AggregatingRequestHandlerCallback()
    requestHandler = plhc_api.SimpleRequestHandlerCB( \
        [ 1, 2, 3 ], requestHandlerCallback )
    worker = plhc_api.CreateWorkerFromParameters( basicParameters.wpb.Build(), requestHandler )

    broker.Start()
    clientProxy.Start()
    worker.Start()

    time.sleep( 1 )

    client.Activate()

    responses = plhc_ext.RequestResponseList()
    numRetrieved = 0

    client.SendSynchronousRequestEmpty( 1, 1 )
    client.SendSynchronousRequestEmpty( 1, 10 )
    client.SendSynchronousRequestEmpty( 2, 2 )
    client.SendSynchronousRequestEmpty( 2, 20 )
    client.SendSynchronousRequestEmpty( 3, 3 )
    client.SendSynchronousRequestEmpty( 3, 30 )

    numRetrieved = client.RetrieveResponsesTimeoutMS( responses, 6, plhc_ext.DelayMS( 1000 ) )

    client.Deactivate()

    broker.Stop()
    clientProxy.Stop()
    worker.Stop()

    print( requestHandlerCallback.receivedRequests )
    assert( 6 == len( responses ) )
    assert( 6 == len( requestHandlerCallback.receivedRequests ) )
    for eachRequestType in [ 1, 2, 3 ]:
        reqKey1 = ( eachRequestType, eachRequestType )
        reqKey2 = ( eachRequestType, 10 * eachRequestType )
        req1 = requestHandlerCallback.receivedRequests.get( reqKey1, None )
        req2 = requestHandlerCallback.receivedRequests.get( reqKey2, None )
        assert( req1 is not None )
        assert( req2 is not None )
        assert( 0 == req1[ 1 ].TotalNumberOfFrames() )
        assert( 0 == req2[ 1 ].TotalNumberOfFrames() )
        assert( None == req1[ 1 ].NextBytes() )
        assert( None == req2[ 1 ].NextBytes() )
        assert( plhc_ext.MessageFlag.Nil == req1[ 0 ].flags )
        assert( plhc_ext.MessageFlag.Nil == req2[ 0 ].flags )
Beispiel #2
0
    def sendRandomSyncRequest( self ):
        ret = 0
        id = getRandomId()
        zmsg = plhc_ext.PyZMQMessage()
        zmsg.AppendBytes( bytes( getRandomUTF8ByteStr() ) )

        ret = self.plhcClient.SendSynchronousRequest( self.rtype, id, zmsg )
        if( ret != 0 ):
            print( "NotifyClient.Error: failed to send sync request[{}]".format( ret ) )
            self.numErrors = self.numErrors + 1

        ret = self.plhcClient.RetrieveResponsesTimeoutMS( \
            self.syncResponses, 1, plhc_ext.DelayMS( 10 ) ) 
        if( ret != 1 ):
            print( "NotifyClient.Warning: failed to retrieve response in 60ms[{}|{}]".format( self.rtype, id ) )
Beispiel #3
0
    def run( self ):
        self.plhcClient.Activate()

        self.plhcClient.SendSynchronousRequestEmpty( 1, 1 )
        self.plhcClient.SendSynchronousRequestEmpty( 1, 10 )
        self.plhcClient.SendSynchronousRequestEmpty( 2, 2 )
        self.plhcClient.SendSynchronousRequestEmpty( 2, 20 )
        self.plhcClient.SendSynchronousRequestEmpty( 3, 3 )
        self.plhcClient.SendSynchronousRequestEmpty( 3, 30 )

        self.numRetrieved = self.plhcClient.RetrieveResponsesTimeoutMS( \
            self.responses, 6, plhc_ext.DelayMS( 10 ) )

        self.plhcClient.Deactivate()

        self.SignalDone()
Beispiel #4
0
    def sendRandomAsyncRequest( self ):
        ret = 0
        id = getRandomId()
        msg = bytes( getRandomUTF8ByteStr() )
        zmsg = plhc_ext.PyZMQMessage()
        zmsg.AppendBytes( msg )

        notifyType = "{}-{}".format( self.rtype, id )

        print( "NotifyClient: subscribing[{}]".format( notifyType ) )

        subscriptions = [ notifyType ]
        subscriptions = plhc_api.CreateNotificationTypeListFromPyIterable( subscriptions )
        ret = self.plhcSubscriber.Subscribe( subscriptions, plhc_ext.DelayMS( 50 ) )
        if( ret != 0 ):
            print( "NotifyClient.Error: failed to sub to {} in timeout[{}]".format( subscriptions, ret ) )
            self.numErrors = self.numErrors + 1

        ret = self.plhcClient.SendAsynchronousRequest( self.rtype, id, zmsg )
        if( ret != 0 ):
            print( "NotifyClient.Error: failed to send async request[{}]".format( ret ) )
            self.numErrors = self.numErrors + 1
Beispiel #5
0
    def run( self ):
        startTimeS = 0.0
        stopTimeS = 0.0
        elapsedTimeMS = 0
        numResponsesOutstanding = 0
        self.numErrors = 0

        self.plhcSubscriber.Start()

        time.sleep( 1 ) # HACK wait a bit to ensure sub connected with some luck
                        # NOT A PROD PATTERN, NOT A GUARANTEE of connected and subbed

        self.plhcClient.Activate()

        numSyncRequestsLeft = self.numSyncRequests
        numAsyncRequestsLeft = self.numAsyncRequests

        startTime = time.perf_counter()
        while( ( numSyncRequestsLeft > 0 ) and ( numAsyncRequestsLeft > 0 ) ):
            sendSyncNext = random.choice( [ True, False ] )
            if( sendSyncNext ):
                self.sendRandomSyncRequest()
                numSyncRequestsLeft = numSyncRequestsLeft - 1
            else:
                self.sendRandomAsyncRequest()
                numAsyncRequestsLeft = numAsyncRequestsLeft - 1

        for _ in range( numSyncRequestsLeft ):
            self.sendRandomSyncRequest()
            
        for _ in range( numAsyncRequestsLeft ):
            self.sendRandomAsyncRequest()

        endTime = time.perf_counter()
        elapsedTimeMS = int( 1000.0 * ( endTime - startTime ) )

        numResponsesOutstanding = self.numSyncRequests - len( self.syncResponses )

        if( numResponsesOutstanding > 0 ):
            numResponsesReceived = len( self.syncResponses )
            timeToWaitMS = elapsedTimeMS

            if( numResponsesOutstanding > numResponsesReceived ):
                waitMultiplier = int( numResponsesOutstanding / numResponsesReceived )
                if waitMultiplier <= 1:
                    waitMultiplier = 2
                timeToWaitMS = timeToWaitMS * waitMultiplier

            print( "NotifyClient.Info: Waiting additional {}ms for {} responses".format( \
                timeToWaitMS,
                numResponsesOutstanding ) )
                
            ret = self.plhcClient.RetrieveResponsesTimeoutMS( \
                self.syncResponses, numResponsesOutstanding, plhc_ext.DelayMS( timeToWaitMS ) ) 

            if( ret != numResponsesOutstanding ):
                print( "NotifyClient.Error: Did not receive expected number of responses[{}]".format( \
                    ret ) )
                self.numErrors = self.numErrors + ( numResponsesOutstanding - ret )

        endTime = time.perf_counter()
        elapsedTimeMS = int( 1000.0 * ( endTime - startTime ) )

        numResponsesOutstanding = self.numAsyncRequests - len( self.notificationsReceived )

        if( numResponsesOutstanding > 0 ):
            numResponsesReceived = len( self.notificationsReceived )
            timeToWaitMS = elapsedTimeMS

            if( numResponsesOutstanding > numResponsesReceived ):
                waitMultiplier = int( numResponsesOutstanding / numResponsesReceived )
                if waitMultiplier <= 1:
                    waitMultiplier = 2
                timeToWaitMS = timeToWaitMS * waitMultiplier

            if timeToWaitMS < 1000:
                timeToWaitMS = 1000

            print( "NotifyClient.Info: Waiting additional {}ms for {} notifications".format( \
                timeToWaitMS,
                numResponsesOutstanding ) )

            time.sleep( int( timeToWaitMS / 1000 ) )

            numResponsesOutstanding = self.numAsyncRequests - len( self.notificationsReceived )
            if( numResponsesOutstanding > 0 ):
                print( "NotifyClient.Error: some notifications still outstanding[{}]".format( \
                    numResponsesOutstanding ) )

        self.plhcClient.Deactivate()
        self.plhcSubscriber.Stop()

        print( "Run Results:" )
        print( "NumSyncRequests     {}".format( self.numSyncRequests ) )
        print( "NumSyncResponses    {}".format( len( self.syncResponses ) ) )
        print( "NumAsyncRequests    {}".format( self.numAsyncRequests ) )
        print( "NumNotifications    {}".format( len( self.notificationsReceived ) ) )
        print( "NumErrors           {}".format( self.numErrors ) )

        self.SignalDone()