def tests(): c = context() print bool(c) print c.__getstate__() c.array = [ 1, 2, 3 ] c.attr = "Hel" print c.attr c.attr += "lo" print c.__getstate__() c = context.start_new() print c.__getstate__() c._dontstore = "never_store" print c.__getstate__() c.ensure("array", [1]).array += [2] c.ensure("array", [1]).array += [3] print c.__getstate__() del c.array print c.__getstate__() c.__setstate__({ "attr": ["a", 1]}) print c.__getstate__() c.__setstate__({ "ensure": "Don't do this!"}) try: print c.ensure("apple", "pear") except TypeError: print "Don't do this!" c.clear() print c.__getstate__()
def tests(): c = context.start_new() gen_cl("load: command_test").run(c) print c.__dict__ gen_cl("apply: get_a_tuple -> a, b, c").run(c) gen_cl('apply: print_string(c)').run(c) gen_cl("apply: get_a_tuple() -> a").run(c) gen_cl('apply: print_string(a)').run(c) try: gen_cl("apply: get_a_tuple -> a, b").run(c) except Exception, e: print e try: gen_cl("apply: no_such_func -> a, b").run(c) except Exception, e: print e print c.__getstate__() gen_cl("yield: a b").run(c) print c._yield gen_cl("yield: c").run(c) print c._yield c._await_tags += [('get_email', "*****@*****.**")] c._await_tags += [('get_empty', None)] gen_cl("await: get_email -> d").run(c) gen_cl("apply: print_string(d)").run(c) print c.__dict__ try: gen_cl("await: not_yet_exists -> x").run(c) except policy.SleepPolicy, e: print "Sleeping..." gen_cl("await: get_empty -> x").run(c) gen_cl("assert: d").run(c) try: gen_cl("assert: x").run(c) except AssertionError: print "Assertion not passed"
def tests(): # sample1 ############################################## c = context.start_new() if True: p = policy.load(sample1) p.load_context(c) p.provide("modify_email", "*****@*****.**") p.resume() if True: p = policy.load(sample1) p.load_context(c) p.provide("verf_email_recv") p.resume() assert(p.is_end()) # sample2 ############################################## c = context.start_new() if True: p = policy.load(sample2) p.load_context(c) p.provide("first_string", "First Record: ") p.resume() if True: p = policy.load(sample2) p.load_context(c) p.provide("second_string", "Second Record: ") p.resume() if True: p = policy.load(sample2) p.load_context(c) p.provide("return_result") print p.resume()[0] assert(p.is_end())
def resume(self): """ Resume a policy from interruption: 1. Run all essential(!) commands prior to breakpoint 2. Keep on running from breakpoint until policy finished 3. return yielded values """ if not self.context: self.context = context.start_new() try: for ln, cmdline in enumerate(self.cmdlines): if ln == self.context.ip: cmdline.run(self.context) self.context.ip += 1 elif cmdline.essential: cmdline.run(self.context) except SleepPolicy, e: pass # sleep == normal exit