def testSettingGettingAndHavingContext(self): """Should be able to set context, get it, and check for membership""" self.assertRaises(KeyError, context.get, 'foo') with context.set(foo='bar'): self.assertEquals('bar', context.get('foo')) self.assertFalse(context.has('foo'))
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 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 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 testContextIsRestoredWhenExceptionsThrown(self): """ When an error occurs in a deferred, context should be restored appropriately This test has a follow up that imposes an ordering requirement based on method name. """ self.assertFalse(context.has('foo')) try: with context.set(foo='bar'): self.assert_(context.has('foo')) yield threads.deferToThread(self.crashy) except AssertionError: pass self.assertFalse(context.has('foo'))
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')))
def testFromThread(self): """Test saving context across a thread deferment.""" with context.set(source='thread'): return threads.deferToThread(lambda: None). \ addCallback(lambda _: self.assertEqual('thread', context.get('source')))
def testFromRequest(self): """Test saving context across a socket request.""" with context.set(source='page'): return client.getPage('http://greplin.com').addCallback( lambda _: self.assertEqual('page', context.get('source')))
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 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')))
def testFromThread(self): """Test saving context across a thread deferment.""" with context.set(source = 'thread'): return threads.deferToThread(lambda: None). \ addCallback(lambda _: self.assertEqual('thread', context.get('source')))
def testFromRequest(self): """Test saving context across a socket request.""" with context.set(source = 'page'): return client.getPage('http://greplin.com').addCallback(lambda _: self.assertEqual('page', context.get('source')))