def testUnwrapAlreadyDone(self): ar_outer = AsyncResult() ar_inner = AsyncResult() ar_outer.set(ar_inner) ar_inner.set('test') ar_unwrapped = ar_outer.Unwrap() self.assertEqual(ar_unwrapped.get(), 'test')
def testWhenAllFailure(self): ar1 = AsyncResult() ar2 = AsyncResult() ar_agg = AsyncResult.WhenAll((ar1, ar2)) ar1.set(1) gevent.sleep(0) ar2.set_exception(Exception()) gevent.sleep(0) self.assertIsNotNone(ar_agg.exception)
def testWhenAnyFailure(self): ar1 = AsyncResult() ar2 = AsyncResult() ar_agg = AsyncResult.WhenAny((ar1, ar2)) ar1.set_exception(Exception()) gevent.sleep(0) ar2.set(1) gevent.sleep(0) self.assertEqual(ar_agg.value, 1)
def Open(self): if not self._open_result: def open_impl(): if self._num_failures[0] > 0: self._num_failures[0] -= 1 self._state = ChannelState.Closed raise Exception("Error opening socket") gevent.sleep(self._open_delay) self._state = ChannelState.Open self._open_result = AsyncResult() self._open_result.SafeLink(open_impl) return self._open_result
def testUnwrapLater(self): ar_outer = AsyncResult() ar_inner = AsyncResult() def set_ar_inner(): gevent.sleep(.01) ar_outer.set(ar_inner) gevent.sleep(.01) ar_inner.set('test') gevent.spawn(set_ar_inner) ar_unwrapped = ar_outer.Unwrap() self.assertFalse(ar_unwrapped.ready()) self.assertEqual(ar_unwrapped.get(), 'test')
def testWhenAllSuccessful(self): ar1 = AsyncResult() ar2 = AsyncResult() ar_agg = AsyncResult.WhenAll((ar1, ar2)) self.assertEqual(ar_agg.ready(), False) ar1.set(1) gevent.sleep(0) self.assertEqual(ar_agg.ready(), False) ar2.set(2) gevent.sleep(0) self.assertEqual(ar_agg.ready(), True) self.assertEqual(ar_agg.value, [1,2])
class MockSink(ClientMessageSink): def __init__(self, properties): super(MockSink, self).__init__() self._state = ChannelState.Idle self.ProcessRequest = None self.ProcessResponse = None self._open_delay = properties.get('open_delay', 0) self._num_failures = properties.get('num_failures', [0]) self._properties = properties self._open_result = None self.endpoint = properties[SinkProperties.Endpoint] def AsyncProcessRequest(self, sink_stack, msg, stream, headers): if self.ProcessRequest: self.ProcessRequest(sink_stack, msg, stream, headers) else: if self.is_closed: sink_stack.AsyncProcessResponseMessage( MethodReturnMessage(error=FailedFastError())) def AsyncProcessResponse(self, sink_stack, context, stream, msg): if self.ProcessResponse: self.ProcessResponse(sink_stack, context, stream, msg) @property def state(self): return self._state @state.setter def state(self, value): self._state = value def Open(self): if not self._open_result: def open_impl(): if self._num_failures[0] > 0: self._num_failures[0] -= 1 self._state = ChannelState.Closed raise Exception("Error opening socket") gevent.sleep(self._open_delay) self._state = ChannelState.Open self._open_result = AsyncResult() self._open_result.SafeLink(open_impl) return self._open_result def Close(self): self._state = ChannelState.Closed self._open_result = None def Fault(self): self._state = ChannelState.Closed self.on_faulted.Set()
def testWhenAnySuccessful(self): ar1 = AsyncResult() ar2 = AsyncResult() ar_agg = AsyncResult.WhenAny((ar1, ar2)) self.assertEqual(ar_agg.ready(), False) ar1.set('hi') gevent.sleep(0) self.assertEqual(ar_agg.value, 'hi')