def test_acquire_callback(self): # Test that callbacks passed to acquire() run immediately after # release(), and that wait() callbacks aren't run until a release() # with no waiters on acquire(). sem = toro.Semaphore(0) history = [] sem.acquire().add_done_callback(make_callback('acquire1', history)) sem.acquire().add_done_callback(make_callback('acquire2', history)) def wait_callback(name): def cb(_): self.assertFalse(sem.locked()) history.append(name) return cb sem.wait().add_done_callback(wait_callback('wait1')) sem.wait().add_done_callback(wait_callback('wait2')) sem.release() history.append('release1') sem.release() history.append('release2') sem.release() history.append('release3') self.assertEqual([ # First release wakes first acquire 'acquire1', 'release1', # Second release wakes second acquire 'acquire2', 'release2', # Third release wakes all waits 'wait1', 'wait2', 'release3' ], history)
def test_acquire_callback(self): lock = toro.Lock() history = [] lock.acquire().add_done_callback(make_callback('acquire1', history)) lock.acquire().add_done_callback(make_callback('acquire2', history)) lock.release() history.append('release') self.assertEqual(['acquire1', 'acquire2', 'release'], history)
def test_acquire_callback(self): lock = toro.RWLock(max_readers=1) history = [] lock.acquire_read().add_done_callback(make_callback('acq1', history)) lock.acquire_read().add_done_callback(make_callback('acq2', history)) lock.release_read() history.append('release') self.assertEqual(['acq1', 'acq2', 'release'], history)
def test_get_callback(self): # Test that callbacks registered with get() run immediately after set() result = toro.AsyncResult(io_loop=self.io_loop) history = [] result.get().add_done_callback(make_callback('get1', history)) result.get().add_done_callback(make_callback('get2', history)) result.set('foo') history.append('set') self.assertEqual(['get1', 'get2', 'set'], history)
def test_notify_1(self): c = toro.Condition() history = [] c.wait().add_done_callback(make_callback('wait1', history)) c.wait().add_done_callback(make_callback('wait2', history)) c.notify(1) history.append('notify1') c.notify(1) history.append('notify2') self.assertEqual(['wait1', 'notify1', 'wait2', 'notify2'], history)
def test_acquire_callback(self): lock = toro.RWLock(max_readers=10) history = [] lock.acquire_write().add_done_callback(make_callback('acq1', history)) future = lock.acquire_write() future.add_done_callback(make_callback('acq2', history)) lock.release_write() yield future history.append('release') self.assertEqual(['acq1', 'acq2', 'release'], history)
def test_notify_all_with_timeout(self): c = toro.Condition(self.io_loop) st = time.time() history = [] c.wait().add_done_callback(make_callback(0, history)) c.wait(deadline=timedelta(seconds=.1)).add_done_callback( make_callback(1, history)) c.wait().add_done_callback(make_callback(2, history)) # Wait for callback 1 to time out yield gen.Task(self.io_loop.add_timeout, st + 0.2) self.assertEqual(['Timeout'], history) c.notify_all() self.assertEqual(['Timeout', 0, 2], history)
def test_notify_all(self): c = toro.Condition() history = [] for i in range(4): c.wait().add_done_callback(make_callback(i, history)) c.notify_all() history.append('notify_all') # Callbacks execute in the order they were registered self.assertEqual(list(range(4)) + ['notify_all'], history)
def test_queue_join_callback(self): # Test that callbacks passed to join() run immediately after task_done() q = toro.JoinableQueue() history = [] q.put('foo') q.put('foo') q.join().add_done_callback(make_callback('join', history)) q.task_done() history.append('task_done1') q.task_done() history.append('task_done2') self.assertEqual(['task_done1', 'join', 'task_done2'], history)
def test_notify_all(self): c = toro.Condition() history = [] for i in range(4): c.wait().add_done_callback(make_callback(i, history)) c.notify_all() history.append('notify_all') # Callbacks execute in the order they were registered self.assertEqual( list(range(4)) + ['notify_all'], history)
def test_notify_n(self): c = toro.Condition() history = [] for i in range(6): c.wait().add_done_callback(make_callback(i, history)) c.notify(3) # Callbacks execute in the order they were registered self.assertEqual(list(range(3)), history) c.notify(1) self.assertEqual(list(range(4)), history) c.notify(2) self.assertEqual(list(range(6)), history)
def test_notify_n_with_timeout(self): # Register callbacks 0, 1, 2, and 3. Callback 1 has a timeout. # Wait for that timeout to expire, then do notify(2) and make # sure everyone runs. Verifies that a timed-out callback does # not count against the 'n' argument to notify(). c = toro.Condition(self.io_loop) st = time.time() history = [] c.wait().add_done_callback(make_callback(0, history)) c.wait(deadline=timedelta(seconds=.1)).add_done_callback( make_callback(1, history)) c.wait().add_done_callback(make_callback(2, history)) c.wait().add_done_callback(make_callback(3, history)) # Wait for callback 1 to time out yield gen.Task(self.io_loop.add_timeout, st + 0.2) self.assertEqual(['Timeout'], history) c.notify(2) self.assertEqual(['Timeout', 0, 2], history) c.notify() self.assertEqual(['Timeout', 0, 2, 3], history)