Example #1
0
 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 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])
Example #4
0
    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])
Example #5
0
    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 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)
Example #11
0
 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())
Example #12
0
 def _deferSleepAndAppend(self, amount, message):
     """Runs a sleep in Twisted."""
     yield time.sleep(amount)
     self._messages.append(message)
Example #13
0
 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')
Example #14
0
 def tupleReturn():
     """Simple asynchronous function that returns a tuple."""
     yield time.sleep(0.01)
     defer.returnValue((5, 'abc'))
Example #15
0
 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)
Example #16
0
 def simpleAsyncMethod(self, value):
   """Simple asynchronous method that returns a given value."""
   yield time.sleep(0.01)
   defer.returnValue(value)
Example #17
0
 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')))
Example #19
0
 def sleep(self):
     """Fake sleep by just returning right away."""
     self.log.append('sleep')
     return time.sleep(0)
Example #20
0
 def simple():
   """Simple asynchronous function that returns a value."""
   yield time.sleep(0.01)
   defer.returnValue(5)
Example #21
0
 def sleeper():
   """Simple asynchronous function that sleeps for a long time."""
   yield time.sleep(100)
Example #22
0
 def simple():
   """Simple asynchronous function."""
   yield time.sleep(0.01)
   defer.returnValue(200)
Example #23
0
 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())
Example #24
0
 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)
Example #25
0
 def simple():
     """Simple asynchronous function that returns a value."""
     yield time.sleep(0.01)
     defer.returnValue(5)
Example #26
0
 def sleep(self):
   """Fake sleep by just returning right away."""
   self.log.append('sleep')
   return time.sleep(0)
Example #27
0
 def tupleReturn():
   """Simple asynchronous function that returns a tuple."""
   yield time.sleep(0.01)
   defer.returnValue((5, 'abc'))
 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')))
Example #29
0
 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')