def testSerialiseDeserialise(self): payload = Payload(filt={"key": "PayloadResponseTest"}) d = PayloadResponse(payload, timeout=0) d.addErrback(lambda _: True) # Handle the errback vortexMsg = payload.toVortexMsg() Payload().fromVortexMsg(vortexMsg)
def testTimeoutErrback(self): payload = Payload(filt={"key": "PayloadResponseTest"}) payloadReponse = PayloadResponse(payload, timeout=3) payloadReponse.addCallback(lambda pl: self.assertFalse(True)) self.assertFailure(payloadReponse, defer.TimeoutError) # PayloadIO().process(payload) return payloadReponse
def _processForProxy(self, payloadEnvelope: PayloadEnvelope, vortexName: str, sendResponse: SendVortexMsgResponseCallable, **kwargs): # Keep a copy of the incoming filt, in case they are using PayloadResponse responseFilt = copy(payloadEnvelope.filt) # Track the response, log an error if it fails # 5 Seconds is long enough. # VortexJS defaults to 10s, so we have some room for round trip time. pr = PayloadResponse( payloadEnvelope, timeout=PayloadResponse.TIMEOUT - 5, # 5 seconds less resultCheck=False, logTimeoutError=False) # This is not a lambda, so that it can have a breakpoint def reply(payloadEnvelope: PayloadEnvelope): payloadEnvelope.filt = responseFilt d: Deferred = payloadEnvelope.toVortexMsgDefer() d.addCallback(sendResponse) return d pr.addCallback(reply) pr.addCallback( lambda _: logger.debug("Received action response from server")) pr.addErrback(self.__handlePrFailure, payloadEnvelope, sendResponse) vortexMsg = yield payloadEnvelope.toVortexMsgDefer() try: yield VortexFactory.sendVortexMsg( vortexMsgs=vortexMsg, destVortexName=self._proxyToVortexName) except Exception as e: logger.exception(e)
def pushAction(self, tupleAction: TupleActionABC, additionalFilt=None) -> Deferred: """ Push Action This pushes the action, either locally or to the server, depending on the implementation. If pushed locally, the promise will resolve when the action has been saved. If pushed directly to the server, the promise will resolve when the server has responded. :param tupleAction The tuple action to send to the remote end :param additionalFilt: Any extra filt that may be required to send with the payload. """ filt = copy(self._filt) if additionalFilt: filt.update(additionalFilt) d = Payload(filt=filt, tuples=[tupleAction]).makePayloadEnvelopeDefer() d.addCallback(lambda payloadEnvelope: PayloadResponse( payloadEnvelope, destVortexName=self._destVortexName)) # Convert the data to TupleAction d.addCallback( lambda payloadEnvelope: payloadEnvelope.decodePayloadDefer()) d.addCallback(lambda payload: payload.tuples) return d
def __call__(self, *args, **kwargs): """ Call """ yesMainThread() try: # FAKE Exception so we can raise a better stack trace later raise Exception() except: stack = sys.exc_info()[2] logger.debug("Calling RPC for %s", self.__funcName) payloadEnvelope = yield (Payload( filt=copy(self._filt), tuples=[_VortexRPCArgTuple(args=args, kwargs=kwargs) ]).makePayloadEnvelopeDefer(compressionLevel=4)) pr = PayloadResponse(payloadEnvelope, timeout=self.__timeoutSeconds, resultCheck=False, logTimeoutError=False, destVortexName=self.__listeningVortexName) # Delete the payload, we don't need to keep it in memory while we # get the result. del payloadEnvelope pr.addCallback(self._processResponseCallback, stack) pr.addErrback(self._processResponseErrback, stack) val = yield pr return val
def testSuccesCb(self): payload = Payload(filt={"key": "PayloadResponseTest"}) payloadReponse = PayloadResponse(payload) payloadReponse.addCallback(lambda pl: self.assertIs(pl, payload)) payloadReponse.addErrback(lambda pl: self.assertTrue(False)) PayloadIO().process(payload, "", "", None, lambda _: None) return payloadReponse
def _handlePoll(self, payloadEnvelope: PayloadEnvelope, tupleSelector: TupleSelector, sendResponse: SendVortexMsgResponseCallable): useCache = payloadEnvelope.filt.get('useCache', True) # Keep a copy of the incoming filt, in case they are using PayloadResponse responseFilt = copy(payloadEnvelope.filt) # Restore the original payload filt (PayloadResponse) and send it back def reply(payload): payload.filt = responseFilt d = payload.toVortexMsgDefer() d.addCallback(sendResponse) d.addErrback(vortexLogFailure, logger, consumeError=True) # logger.debug("Received response from observable") if useCache: cache = self._getCache(tupleSelector) if cache and cache.lastServerPayloadDate is not None and cache.cacheEnabled: payloadEnvelope.encodedPayload = cache.encodedPayload payloadEnvelope.date = cache.lastServerPayloadDate reply(payloadEnvelope) return # Track the response, log an error if it fails # 5 Seconds is long enough pr = PayloadResponse( payloadEnvelope, timeout=PayloadResponse.TIMEOUT - 5, # 5 seconds less logTimeoutError=False ) pr.addErrback(self._handlePrFailure, tupleSelector) pr.addErrback(vortexLogFailure, logger, consumeError=True) pr.addCallback(reply) self._sendRequestToServer(payloadEnvelope)
def pollForTuples( self, tupleSelector: TupleSelector, logTimeoutError: bool = True ) -> Deferred: startFilt = copy(self._sendFilt) startFilt.update({"subscribe": False, "tupleSelector": tupleSelector}) def updateCacheCallback( payloadEnvelope: PayloadEnvelope, ) -> PayloadEnvelope: cache, _ = self._updateCache(payloadEnvelope) return payloadEnvelope pr = PayloadResponse( payloadEnvelope=PayloadEnvelope(startFilt), destVortexName=self._destVortexName, logTimeoutError=logTimeoutError, ) pr.addCallback(updateCacheCallback) pr.addCallback( lambda payloadEnvelope: payloadEnvelope.decodePayloadDefer() ) pr.addCallback(lambda payload: payload.tuples) return pr