def test_deferreds(self): target = Thing() item = base.Item("key") now = datetime.datetime.now() deferreds = [ driver.Deferred(now, target.append, [1, 2, 3], {"kwarg": 42}), driver.Deferred(now, os.getcwd, [], None), driver.Deferred(now, module_level_func, [], None), driver.Deferred(now, item.init, [], None, periodical=(11.1, 22.2)) ] x1, x2, x3, x4 = serializecycle(deferreds) assert x1["__class__"] == "tale.driver.Deferred" assert x1["action"] == "append" assert x1["vargs"] == [1, 2, 3] assert x1["kwargs"] == {"kwarg": 42} assert x1["periodical"] is None assert x1["owner"] == "class:tests.supportstuff.Thing" assert x1["due_gametime"] == now.isoformat() assert x2["action"] == "getcwd" assert x2["owner"] in ("module:os", "module:nt", "module:posix") assert x3["action"] == "module_level_func" assert x3["owner"] == "module:tests.test_serialize" assert x4["action"] == "init" assert len(x4["owner"]) == 4 assert x4["owner"][1] == "key" assert x4["periodical"] == (11.1, 22.2)
def testSerializable(self): target = Thing() deferreds = [the_driver.Deferred(datetime.datetime.now(), target.append, [1, 2, 3], {"kwarg": 42}), the_driver.Deferred(datetime.datetime.now(), os.getcwd, [], None), the_driver.Deferred(datetime.datetime.now(), module_level_func, [], None)] ser = pickle.dumps(deferreds, pickle.HIGHEST_PROTOCOL) data = pickle.loads(ser) self.assertEqual(deferreds, data)
def testSortable(self): t1 = datetime.datetime(1995, 1, 1) t2 = datetime.datetime(1996, 1, 1) t3 = datetime.datetime(1997, 1, 1) t4 = datetime.datetime(1998, 1, 1) t5 = datetime.datetime(1999, 1, 1) d1 = the_driver.Deferred(t5, os.getcwd, None, None) d2 = the_driver.Deferred(t2, os.getcwd, None, None) d3 = the_driver.Deferred(t4, os.getcwd, None, None) d4 = the_driver.Deferred(t1, os.getcwd, None, None) d5 = the_driver.Deferred(t3, os.getcwd, None, None) deferreds = sorted([d1, d2, d3, d4, d5]) dues = [d.due for d in deferreds] self.assertEqual([t1, t2, t3, t4, t5], dues)
def testCallable(self): def scoped_function(): pass t = Thing() d = the_driver.Deferred(None, t.append, [42], None) ctx = tale.util.Context(driver="driver", clock=None, config=None, player_connection=None) d(ctx=ctx) self.assertEqual([42], t.x) d = the_driver.Deferred(None, module_level_func, [], None) d(ctx=ctx) d = the_driver.Deferred(None, module_level_func_without_ctx, [], None) d(ctx=ctx) with self.assertRaises(ValueError): the_driver.Deferred(None, scoped_function, [], None) with self.assertRaises(ValueError): d = the_driver.Deferred(None, lambda a, ctx=None: 1, [42], None)
def testHeapq(self): t1 = datetime.datetime(1995, 1, 1) t2 = datetime.datetime(1996, 1, 1) t3 = datetime.datetime(1997, 1, 1) t4 = datetime.datetime(1998, 1, 1) t5 = datetime.datetime(1999, 1, 1) d1 = the_driver.Deferred(t5, os.getcwd, None, None) d2 = the_driver.Deferred(t2, os.getcwd, None, None) d3 = the_driver.Deferred(t4, os.getcwd, None, None) d4 = the_driver.Deferred(t1, os.getcwd, None, None) d5 = the_driver.Deferred(t3, os.getcwd, None, None) heap = [d1, d2, d3, d4, d5] heapq.heapify(heap) dues = [] while heap: dues.append(heapq.heappop(heap).due) self.assertEqual([t1, t2, t3, t4, t5], dues)
def testDue_gametime(self): # test due timings where the gameclock == 10 times realtime clock game_clock = tale.util.GameDateTime(datetime.datetime(2013, 7, 18, 15, 29, 59, 123), 10) # 10 times realtime due = game_clock.plus_realtime(datetime.timedelta(seconds=60)) # due in (realtime) 60 seconds (600 gametime seconds) d = the_driver.Deferred(due, os.getcwd, None, None) result = d.when_due(game_clock) # not realtime self.assertIsInstance(result, datetime.timedelta) self.assertEqual(datetime.timedelta(seconds=10 * 60), result) result = d.when_due(game_clock, True) # realtime self.assertEqual(datetime.timedelta(seconds=60), result) game_clock.add_gametime(datetime.timedelta(seconds=20)) # +20 gametime seconds (=2 realtime seconds) result = d.when_due(game_clock) # not realtime (game time) self.assertEqual(datetime.timedelta(seconds=580), result) result = d.when_due(game_clock, True) # realtime self.assertEqual(datetime.timedelta(seconds=58), result)