def testFuture_SetResult_AddDoneCallback(self): f = tasklets.Future() f.set_result(42) self.assertEqual(f.get_result(), 42) f.add_callback(self.universal_callback, f) eventloop.run() self.assertEqual(self.log, [(f, )])
def testFuture_AddDoneCallback_SetException(self): f = tasklets.Future() f.add_callback(self.universal_callback, f) f.set_exception(RuntimeError(42)) eventloop.run() self.assertEqual(self.log, [(f, )]) self.assertEqual(f.done(), True)
def testFuture_SetResult_AddDoneCallback(self): f = tasklets.Future() f.set_result(42) self.assertEqual(f.get_result(), 42) f.add_callback(self.universal_callback, f) eventloop.run() self.assertEqual(self.log, [(f,)])
def testFuture_AddDoneCallback_SetResult(self): f = tasklets.Future() f.add_callback(self.universal_callback, f) self.assertEqual(self.log, []) # Nothing happened yet. f.set_result(42) eventloop.run() self.assertEqual(self.log, [(f, )])
def testFuture_AddDoneCallback_SetResult(self): f = tasklets.Future() f.add_callback(self.universal_callback, f) self.assertEqual(self.log, []) # Nothing happened yet. f.set_result(42) eventloop.run() self.assertEqual(self.log, [(f,)])
def testFuture_AddDoneCallback_SetException(self): f = tasklets.Future() f.add_callback(self.universal_callback, f) f.set_exception(RuntimeError(42)) eventloop.run() self.assertEqual(self.log, [(f,)]) self.assertEqual(f.done(), True)
def testFuture_WaitAny(self): self.assertEqual(tasklets.Future.wait_any([]), None) todo = self.create_futures() while todo: f = tasklets.Future.wait_any(todo) todo.remove(f) eventloop.run() self.assertEqual(self.log, [(f, ) for f in self.futs])
def testRun(self): record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.2, foo, 42) eventloop.queue_call(0.1, foo, arg='hello') eventloop.run() self.assertEqual(record, ['hello', 42])
def testFuture_WaitAny(self): self.assertEqual(tasklets.Future.wait_any([]), None) todo = self.create_futures() while todo: f = tasklets.Future.wait_any(todo) todo.remove(f) eventloop.run() self.assertEqual(self.log, [(f,) for f in self.futs])
def bench(n): """Top-level benchmark function.""" futs = [] for i in range(n): fut = fibonacci(i) futs.append(fut) eventloop.run() for fut in futs: fut.check_success()
def add_context_wrapper(*args): __ndb_debug__ = utils.func_info(func) tasklets.Future.clear_all_pending() # Reset context; a new one will be created on the first call to # get_context(). tasklets.set_context(None) ctx = tasklets.get_context() try: return tasklets.synctasklet(func)(*args) finally: eventloop.run() # Ensure writes are flushed, etc.
def testSleep(self): log = [] @tasklets.tasklet def foo(): log.append(time.time()) yield tasklets.sleep(0.1) log.append(time.time()) foo() eventloop.run() t0, t1 = log dt = t1-t0 self.assertAlmostEqual(dt, 0.1, places=2)
def testRunWithRpcs(self): record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.1, foo, 42) config = datastore_rpc.Configuration(on_completion=foo) rpc = self.conn.async_get(config, []) self.assertEqual(len(rpc.rpcs), 1) eventloop.queue_rpc(rpc) eventloop.run() self.assertEqual(record, [rpc.rpcs[0], 42]) self.assertEqual(rpc.state, 2) # TODO: Use apiproxy_rpc.RPC.FINISHING.
def testMultiFuture_PreCompleted(self): @tasklets.tasklet def foo(): yield tasklets.sleep(0.01) raise tasklets.Return(42) mfut = tasklets.MultiFuture() dep = foo() dep.wait() mfut.add_dependent(dep) mfut.complete() eventloop.run() self.assertTrue(mfut.done()) self.assertEqual(mfut.get_result(), [42])
def testRunWithRpcs(self): apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() stub = datastore_file_stub.DatastoreFileStub('_', None) apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub) record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.1, foo, 42) conn = datastore_rpc.Connection() config = datastore_rpc.Configuration(on_completion=foo) rpc = conn.async_get(config, []) self.assertEqual(len(rpc.rpcs), 1) eventloop.queue_rpc(rpc) eventloop.run() self.assertEqual(record, [rpc.rpcs[0], 42]) self.assertEqual(rpc.state, 2) # TODO: Use apiproxy_rpc.RPC.FINISHING.
def _finish(): # Compare to the finally clause in toplevel() in context.py. ctx = tasklets.get_context() tasklets.set_context(None) ctx.flush().check_success() eventloop.run() # Ensure writes are flushed, etc.
def run(self): setup_context() result = key.get() assert result == ent2, result eventloop.run()
def subverting_aries_fix(): # Variation by Guido van Rossum. def setup_context(): ctx = tasklets.get_context() ctx.set_datastore_policy(True) ctx.set_memcache_policy(True) ctx.set_cache_policy(False) return ctx key = model.Key(CrashTestDummyModel, 1) mkey = tasklets.get_context()._memcache_prefix + key.urlsafe() ent1 = CrashTestDummyModel(key=key, name=u'Brad Roberts') ent2 = CrashTestDummyModel(key=key, name=u'Ellen Reid') ctx = setup_context() # Store an original version of the entity # NOTE: Do not wish to store this one value in memcache, turning it off ent1.put(use_memcache=False) a_lock1 = threading.Lock() a_lock2 = threading.Lock() a_lock3 = threading.Lock() class A(threading.Thread): def run(self): ctx = setup_context() fut = ent2.put_async() # Get to the point that the lock is written to memcache wait_on_batcher(ctx._memcache_set_batcher) # Wait for B to cause a race condition a_lock2.acquire() a_lock1.acquire() wait_on_batcher(ctx._put_batcher) a_lock2.release() a_lock1.release() # Wait for C to read from memcache a_lock3.acquire() fut.check_success() a_lock3.release() class C(threading.Thread): def run(self): setup_context() result = key.get() assert result == ent2, result eventloop.run() logging.info('A: write lock to memcache') a = A() a_lock1.acquire() a_lock3.acquire() a.start() while memcache.get(mkey) != context._LOCKED: time.sleep(0.1) # Wait for the memcache lock to be set logging.info('M: evict the lock') memcache.flush_all() assert memcache.get(mkey) is None, 'lock was not evicted' logging.info("B: read from memcache (it's a miss)") b = key.get_async() wait_on_batcher(ctx._memcache_get_batcher) logging.info('B: write lock to memcache') wait_on_batcher(ctx._memcache_set_batcher) logging.info("B: read the lock back (it's a success)") wait_on_batcher(ctx._memcache_get_batcher) logging.info('B: read from datastore') wait_on_batcher(ctx._get_batcher) logging.info('A: write to datastore') a_lock1.release() a_lock2.acquire() a_lock2.release() logging.info('B: write to memcache (writes a stale value)') b.get_result() eventloop.run() # Puts to memcache are still stuck in the eventloop logging.info('C: read from memcache (sees a stale value)') c = C() c.start() c.join() logging.info('A: delete from memcache (deletes the stale value!)') a_lock3.release() a.join() pb3 = memcache.get(mkey) assert pb3 is not context._LOCKED, 'Received _LOCKED value' if pb3 is not None: ent3 = ctx._conn.adapter.pb_to_entity(pb3) assert ent3 == ent2, 'stale value in memcache; %r != %r' % (ent3, ent2) # Finally check the high-level API. ent4 = key.get() assert ent4 == ent2