def timeAfterFinished(): assert local('__reactor__') is reactor() # Code explaining code :-) currentReactor = reactor() currentReactor.addTimer(1.0, lambda: neverCalled.append(True)) yield 'async work...' currentReactor.addProcess(lambda: neverCalled.append(True)) raise StopIteration(42)
def timeAfterFinished(): assert local( '__reactor__') is reactor() # Code explaining code :-) currentReactor = reactor() currentReactor.addTimer(1.0, lambda: neverCalled.append(True)) yield 'async work...' currentReactor.addProcess(lambda: neverCalled.append(True)) raise StopIteration(42)
def helper(*args, **kwargs): this = yield yield # Works within an asProcess-passed generatorFunction only (needs contextual addProcess driving this generator and a reactor). tokenList = [] def cb(): tokenList.pop() this.throw(AssertionError, AssertionError('dieAfter triggered after %s seconds.' % seconds), None) tokenList.append(reactor().addTimer(seconds=seconds, callback=cb)) try: retval = yield generatorFunction(*args, **kwargs) except: c, v, t = exc_info() if tokenList: reactor().removeTimer(token=tokenList[0]) raise c, v, t
def test(): sA, sB, sC, s1, s2, s3 = (MockSok(x) for x in ['A', 'B', 'C', 1, 2, 3]) top = be((Observable(), (SocketPool(reactor=reactor(), unusedTimeout=0.025),), )) # Make sure 1st check all-sockets-ok yield sleep(seconds=(0.001)) # Initial set yield top.any.putSocketInPool(host='x', port=80, sock=sA) yield top.any.putSocketInPool(host='x', port=80, sock=sB) yield top.any.putSocketInPool(host='x', port=80, sock=sC) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s1) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s2) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3) self.assertEquals([], s2.log.calledMethodNames()) # sample # Pass time, no timeout - 1st check always all-sockets-ok yield sleep(seconds=(0.025 + 0.022)) # +/- 0.003 until next mostly-fatal check self.assertEquals([], s2.log.calledMethodNames()) # sample # Use some, put some back _sockC = yield top.any.getPooledSocket(host='x', port=80) _sockB = yield top.any.getPooledSocket(host='x', port=80) _sock3 = yield top.any.getPooledSocket(host='example.org', port=8080) self.assertEquals([sC, sB, s3], [_sockC, _sockB, _sock3]) self.assertEquals([], sC.log.calledMethodNames()) self.assertEquals([], sB.log.calledMethodNames()) self.assertEquals([], s3.log.calledMethodNames()) yield top.any.putSocketInPool(host='x', port=80, sock=sC) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3) yield sleep(seconds=0.015) # 0.025 - (0.015 - 0.003) = 0.013 until all-fatal check inPool = [] while True: result = yield top.any.getPooledSocket(host='x', port=80) if result == None: break inPool.append(result) while True: result = yield top.any.getPooledSocket(host='example.org', port=8080) if result == None: break inPool.append(result) self.assertEquals([sC, s3], inPool) self.assertEquals([], sC.log.calledMethodNames()) self.assertEquals([], s3.log.calledMethodNames()) self.assertEquals(['shutdown', 'close'], s1.log.calledMethodNames()) # sample shutdown, close = s1.log.calledMethods self.assertEquals(((SHUT_RDWR,), {}), (shutdown.args, shutdown.kwargs)) self.assertEquals(((), {}), (close.args, close.kwargs))
def test(): done = [] def handler(): lhs, rhs = socketpair() try: with SocketContext(lhs): # quick hack, can block. with Timer(0.01): yield 'a' yield 'b' finally: lhs.close(); rhs.close() done.append(True) g = Gio(reactor(), handler()) for _ in range(42): yield self.assertEquals([True], done) self.assertEquals([], reactor()._timers) self.assertEquals([], g._contextstack)
def test(): done = [] def handler(): yield yield 'a' done.append(True) try: g = Gio(reactor(), handler()) self.fail('must not come here') except AssertionError, e: self.assertEquals('Gio: No context available.', str(e))
def test(): done = [] def handler(): lhs, rhs = socketpair() try: with SocketContext(lhs): # quick hack, can block. with Timer(0.01): for i in xrange(999999): yield 'a' except TimeoutException: done.append(False) finally: lhs.close; rhs.close() yield g = Gio(reactor(), handler()) while not done: yield self.assertEquals([False], done) self.assertEquals([], reactor()._timers) self.assertEquals([], g._contextstack)
def helper(*args, **kwargs): this = yield yield # Works within an asProcess-passed generatorFunction only (needs contextual addProcess driving this generator and a reactor). tokenList = [] def cb(): tokenList.pop() this.throw( AssertionError, AssertionError('dieAfter triggered after %s seconds.' % seconds), None) tokenList.append(reactor().addTimer(seconds=seconds, callback=cb)) try: retval = yield generatorFunction(*args, **kwargs) except: c, v, t = exc_info() if tokenList: reactor().removeTimer(token=tokenList[0]) raise c, v, t
def test(): done = [] def handler(): lhs, rhs = socketpair() try: with SocketContext(lhs): # quick hack, can block. with Timer(0.01): yield 'a' yield 'b' finally: lhs.close() rhs.close() done.append(True) g = Gio(reactor(), handler()) for _ in range(42): yield self.assertEquals([True], done) self.assertEquals([], reactor()._timers) self.assertEquals([], g._contextstack)
def test(): done = [] def handler(): lhs, rhs = socketpair() try: with SocketContext(lhs): # quick hack, can block. with Timer(0.01): for i in xrange(999999): yield 'a' except TimeoutException: done.append(False) finally: lhs.close rhs.close() yield g = Gio(reactor(), handler()) while not done: yield self.assertEquals([False], done) self.assertEquals([], reactor()._timers) self.assertEquals([], g._contextstack)
def test(): top = be((Observable(), (SocketPool(reactor=reactor(), unusedTimeout=0.02),), )) yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('A')) yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('B')) yield sleep(seconds=0.001) result = yield top.any.getPooledSocket(host='x', port=80) self.assertEquals('B', result) yield sleep(seconds=0.04) result = yield top.any.getPooledSocket(host='x', port=80) self.assertEquals(None, result)
def test(): top = be(( Observable(), (SocketPool(reactor=reactor(), unusedTimeout=0.02), ), )) yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('A')) yield top.any.putSocketInPool(host='x', port=80, sock=MockSok('B')) yield sleep(seconds=0.001) result = yield top.any.getPooledSocket(host='x', port=80) self.assertEquals('B', result) yield sleep(seconds=0.04) result = yield top.any.getPooledSocket(host='x', port=80) self.assertEquals(None, result)
def handler(): self.assertEquals(thereactor, reactor())
def test(): additionalGlobals = { 'util_reloaded_id': howOften("util-reloaded"), 'using_util_reloaded_id': howOften("using-util-reloaded"), 'orphan_reloaded_id': howOften("orphan-reloaded"), } d = DynamicHtml([self.tempdir], reactor=reactor(), additionalGlobals=additionalGlobals) # Allow cleanup of directoryWatcherReadFD (whitebox) _reactorFdsKeys = reactor()._fds.keys() self.assertEquals(1, len(_reactorFdsKeys)) directoryWatcherReadFD = _reactorFdsKeys[0] yield zleep(0.01) # Allow DirectoryWatcher some reactor time # Initialized once util, orphan, using_util = d.getModule('util'), d.getModule('orphan'), d.getModule('using-util') self.assertEquals( ['util-reloaded:1', 'orphan-reloaded:1', 'using-util-reloaded:1'], [util.reloaded, orphan.reloaded, using_util.reloaded]) self.assertEquals('using-util-reloaded:1 - util-reloaded:1', using_util.using()) # Touch & **all** reloaded with open(fp_util, 'w') as f: f.write(util_contents) yield zleep(0.02) # Allow DirectoryWatcher some reactor time util, orphan, using_util = d.getModule('util'), d.getModule('orphan'), d.getModule('using-util') self.assertEquals( ['util-reloaded:2', 'orphan-reloaded:2', 'using-util-reloaded:2'], [util.reloaded, orphan.reloaded, using_util.reloaded]) self.assertEquals('using-util-reloaded:2 - util-reloaded:2', using_util.using()) # Remove file - nothing happens remove(fp_orphan) yield zleep(0.02) # Allow DirectoryWatcher some reactor time util, orphan, using_util = d.getModule('util'), d.getModule('orphan'), d.getModule('using-util') self.assertNotEqual(None, orphan) self.assertEquals( ['util-reloaded:2', 'orphan-reloaded:2', 'using-util-reloaded:2'], [util.reloaded, orphan.reloaded, using_util.reloaded]) # Modify util - reload **and** remove of deleted happens with open(fp_util, 'w') as f: f.write(util_contents) yield zleep(0.02) # Allow DirectoryWatcher some reactor time util, orphan, using_util = d.getModule('util'), d.getModule('orphan'), d.getModule('using-util') self.assertEquals(None, orphan) self.assertEquals( ['util-reloaded:3', 'using-util-reloaded:3'], [util.reloaded, using_util.reloaded]) self.assertEquals('using-util-reloaded:3 - util-reloaded:3', using_util.using()) # cleanup reactor().removeReader(sok=directoryWatcherReadFD)
def test(): sA, sB, sC, s1, s2, s3 = (MockSok(x) for x in ['A', 'B', 'C', 1, 2, 3]) top = be(( Observable(), (SocketPool(reactor=reactor(), unusedTimeout=0.025), ), )) # Make sure 1st check all-sockets-ok yield sleep(seconds=(0.001)) # Initial set yield top.any.putSocketInPool(host='x', port=80, sock=sA) yield top.any.putSocketInPool(host='x', port=80, sock=sB) yield top.any.putSocketInPool(host='x', port=80, sock=sC) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s1) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s2) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3) self.assertEquals([], s2.log.calledMethodNames()) # sample # Pass time, no timeout - 1st check always all-sockets-ok yield sleep( seconds=(0.025 + 0.022)) # +/- 0.003 until next mostly-fatal check self.assertEquals([], s2.log.calledMethodNames()) # sample # Use some, put some back _sockC = yield top.any.getPooledSocket(host='x', port=80) _sockB = yield top.any.getPooledSocket(host='x', port=80) _sock3 = yield top.any.getPooledSocket(host='example.org', port=8080) self.assertEquals([sC, sB, s3], [_sockC, _sockB, _sock3]) self.assertEquals([], sC.log.calledMethodNames()) self.assertEquals([], sB.log.calledMethodNames()) self.assertEquals([], s3.log.calledMethodNames()) yield top.any.putSocketInPool(host='x', port=80, sock=sC) yield top.any.putSocketInPool(host='example.org', port=8080, sock=s3) yield sleep( seconds=0.015 ) # 0.025 - (0.015 - 0.003) = 0.013 until all-fatal check inPool = [] while True: result = yield top.any.getPooledSocket(host='x', port=80) if result == None: break inPool.append(result) while True: result = yield top.any.getPooledSocket(host='example.org', port=8080) if result == None: break inPool.append(result) self.assertEquals([sC, s3], inPool) self.assertEquals([], sC.log.calledMethodNames()) self.assertEquals([], s3.log.calledMethodNames()) self.assertEquals(['shutdown', 'close'], s1.log.calledMethodNames()) # sample shutdown, close = s1.log.calledMethods self.assertEquals(((SHUT_RDWR, ), {}), (shutdown.args, shutdown.kwargs)) self.assertEquals(((), {}), (close.args, close.kwargs))