def testGeneratorFunctionResolveToPromise(self): (p1, resolve, _) = _GetPromiseAndFunctions() p2 = promise.Promise(lambda x, y: x(p1)) self.assertEquals(p2.state, promise.Promise.STATE_PENDING) p2.Then(self._AddToAccumulated) resolve(promise.Promise.Resolve(0)) self.assertEquals(self.accumulated, [0])
def _GetPromiseAndFunctions(): functions = {} def GeneratorFunction(resolve, reject): functions['resolve'] = resolve functions['reject'] = reject p = promise.Promise(GeneratorFunction) return (p, functions['resolve'], functions['reject'])
def _Call(self, *args, **kwargs): def GenerationMethod(resolve, reject): message = _GetMessage(method, flags, *args, **kwargs) if method.response_struct: def Accept(message): try: assert message.header.message_type == method.ordinal payload = message.payload response = method.response_struct.Deserialize( serialization.RootDeserializationContext( payload.data, payload.handles)) as_dict = response.AsDict() if len(as_dict) == 1: value = as_dict.values()[0] if not isinstance(value, dict): response = value resolve(response) return True except Exception as e: # Adding traceback similarly to python 3.0 (pep-3134) e.__traceback__ = sys.exc_info()[2] reject(e) return False finally: self._error_handler.RemoveCallback(reject) self._error_handler.AddCallback(reject) if not self._router.AcceptWithResponder( message, messaging.ForwardingMessageReceiver(Accept)): self._error_handler.RemoveCallback(reject) reject( messaging.MessagingException( "Unable to send message.")) else: if (self._router.Accept(message)): resolve(None) else: reject( messaging.MessagingException( "Unable to send message.")) return promise.Promise(GenerationMethod)