def notCrashySleepWithNonRootContext(self, sleepFor, parentCtx, **kwargs): """Sleeps for sleepFor seconds and then asserts that kwargs + parentCtx is the current context""" expected = {} expected.update(parentCtx) expected.update(kwargs) with context.set(**kwargs): yield sleep(sleepFor) self.assertEquals(expected, context.all())
def testMaintainsNonRootContextAcrossUnyieldedDeferred(self): """Deferreds that exist at the same time under a parent context should not interfere and have that parent context""" with context.set(parent=1): a = self.notCrashySleepWithNonRootContext(0.01, {'parent': 1}, a=1) b = self.notCrashySleepWithNonRootContext(0.001, {'parent': 1}, b=2) yield sleep(0.02) yield DeferredList([a, b])
def testDoesntMingleContextAcrossUnyieldedDeferred(self): """Deferreds that exist at the same time should not have interacting contexts""" self.assertFalse(context.has('a')) self.assertFalse(context.has('b')) a = self.notCrashySleep(0.01, a=1) self.assertFalse(context.has('a')) self.assertFalse(context.has('b')) b = self.notCrashySleep(0.001, b=2) self.assertFalse(context.has('a')) self.assertFalse(context.has('b')) yield sleep(0.02) yield DeferredList([a, b])
def testInlineCallbacks(self): """Test context saving across I/O.""" with thread.locals(someValue = 12345): yield time.sleep(0.001) self.assertEqual(None, thread.getLocal('anotherValue')) self.assertEqual(12345, thread.getLocal('someValue')) with thread.locals(anotherValue = 'abcde'): yield client.getPage('http://greplin.com') self.assertEqual('abcde', thread.getLocal('anotherValue')) self.assertEqual(12345, thread.getLocal('someValue')) yield threads.deferToThread(lambda: None) self.assertEqual(None, thread.getLocal('anotherValue')) self.assertEqual(12345, thread.getLocal('someValue')) self.assertEqual(None, thread.getLocal('someValue'))
def countdown(i): """Countdown function to generate chained asynchronous calls.""" if not i: reactor.stop() return print 'i is %d with frame %s' % (i, AsyncFrame.currentFrame.getName()) yield time.sleep(0.01) print 'done sleeping with frame %s' % AsyncFrame.currentFrame.getName() if i % 2: nestAndPrint() yield dispatchOdd(i - 1) else: print stacktrace() yield dispatchEven(i - 1)
def testInlineCallbacks(self): """Test context saving across I/O.""" with context.set(someValue=12345): yield time.sleep(0.001) self.assertFalse(context.has('anotherValue')) self.assertEqual(12345, context.get('someValue')) self.assertTrue(context.has('someValue')) with context.set(anotherValue='abcde'): yield client.getPage('http://greplin.com') self.assertEqual('abcde', context.get('anotherValue')) self.assertEqual(12345, context.get('someValue')) yield threads.deferToThread(lambda: None) self.assertFalse(context.has('anotherValue')) self.assertEqual(12345, context.get('someValue')) self.assertTrue(context.has('someValue')) self.assertFalse(context.has('someValue'))
def _deferSleepAndAppend(self, amount, message): """Runs a sleep in Twisted.""" yield time.sleep(amount) self._messages.append(message)
def notCrashySleep(self, sleepFor, **kwargs): """Sleeps for sleepFor seconds and asserts that kwargs is equal to the stored context variables""" with context.set(**kwargs): yield sleep(sleepFor) self.assertEquals(kwargs, context.all())
def instantYields(): """Simple asynchronous function that has instant results for a large number of yields.""" for i in xrange(1000): yield i yield time.sleep(0.01) # Make it actually async. defer.returnValue('done')
def tupleReturn(): """Simple asynchronous function that returns a tuple.""" yield time.sleep(0.01) defer.returnValue((5, 'abc'))
def chained(): """Simple asynchronous function that returns a value.""" result = yield time.sleep(0.01).addCallback( lambda _: time.sleep(0.01).addCallback(lambda _: 3.14)) defer.returnValue(result)
def simpleAsyncMethod(self, value): """Simple asynchronous method that returns a given value.""" yield time.sleep(0.01) defer.returnValue(value)
def sleeper(): """Simple asynchronous function that sleeps for a long time.""" yield time.sleep(100)
def testFromDelay(self): """Test saving context across a delayed call.""" with thread.locals(source = 'delay'): return time.sleep(0.001).addCallback(lambda _: self.assertEqual('delay', thread.getLocal('source')))
def sleep(self): """Fake sleep by just returning right away.""" self.log.append('sleep') return time.sleep(0)
def simple(): """Simple asynchronous function that returns a value.""" yield time.sleep(0.01) defer.returnValue(5)
def simple(): """Simple asynchronous function.""" yield time.sleep(0.01) defer.returnValue(200)
def chained(): """Simple asynchronous function that returns a value.""" result = yield time.sleep(0.01).addCallback(lambda _: time.sleep(0.01).addCallback(lambda _: 3.14)) defer.returnValue(result)
def testFromDelay(self): """Test saving context across a delayed call.""" with context.set(source='delay'): return time.sleep(0.001).addCallback( lambda _: self.assertEqual('delay', context.get('source')))