def testFiberState(self): def depth1(): state = fiber.get_state() self.assertNotEqual(None, state) self.assertTrue("root" in state) state.add("depth1 in") sub_state = depth2() self.assertEqual(state, sub_state) self.assertTrue("depth2 in" in state) self.assertTrue("depth2 out" in state) self.assertTrue("depth3" in state) state.add("depth1 out") return state def depth2(): state = fiber.get_state() self.assertNotEqual(None, state) self.assertTrue("root" in state) self.assertTrue("depth1 in" in state) state.add("depth2 in") sub_state = depth3() self.assertEqual(state, sub_state) self.assertTrue("depth3" in state) state.add("depth2 out") return state def depth3(): state = fiber.get_state() self.assertNotEqual(None, state) self.assertTrue("root" in state) self.assertTrue("depth1 in" in state) self.assertTrue("depth2 in" in state) state.add("depth3") return state self.assertEqual(None, fiber.get_state()) state = set(["root"]) fiber.set_state(state) self.assertEqual(state, fiber.get_state()) sub_state = depth1() # check that all states are reference to the same instance self.assertEqual(state, sub_state) self.assertTrue("root" in state) self.assertTrue("depth1 in" in state) self.assertTrue("depth1 out" in state) self.assertTrue("depth2 in" in state) self.assertTrue("depth2 out" in state) self.assertTrue("depth3" in state)
def errback(failure, expected, value=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, failure.value) if value is None: return failure return value
def set_tag(tag): # Extra depth to account for this function frame state = fiber.get_state(depth=1) if state is None: state = set() fiber.set_state(state, depth=1) state.add(tag)
def testCustomStateDepth(self): def set_tag(tag): # Extra depth to account for this function frame state = fiber.get_state(depth=1) if state is None: state = set() fiber.set_state(state, depth=1) state.add(tag) def have_tag(tag): # Extra depth to account for this function frame state = fiber.get_state(depth=1) return state and tag in state def sub_function(): self.assertTrue(have_tag("spam")) self.assertFalse(have_tag("spam")) set_tag("spam") self.assertTrue(have_tag("spam")) sub_function() self.assertNotEqual(None, fiber.get_state()) # Check exception for invalid depth self.assertRaises(RuntimeError, fiber.get_state, depth=-100) self.assertRaises(RuntimeError, fiber.get_state, depth=666) self.assertRaises(RuntimeError, fiber.set_state, None, depth=666)
def _side_effect_wrapper(callable, args, kwargs, name): section_state = fiber.get_state() if section_state is not None: # We are in a woven section entry = section_state.get(JOURNAL_ENTRY_TAG, None) if entry is not None: # We are in a replayable section mode = section_state.get(RECMODE_TAG, None) if mode == JournalMode.replay: return entry.next_side_effect(name, *args, **kwargs) # Create a side-effect entry effect = entry.new_side_effect(name, *args, **kwargs) # Keep it in the replayable section state section_state[SIDE_EFFECT_TAG] = effect # Break the fiber to allow new replayable sections fiber.break_fiber() # Keep the side-effect entry to detect we are in one fiber.set_stack_var(SIDE_EFFECT_TAG, effect) try: result = callable(*args, **kwargs) result = _check_side_effet_result(result, name) effect.set_result(result) effect.commit() return result except Exception, e: # FIXME: handle exceptions in side effects properly error.handle_exception(None, e, "Exception raised by side-effect %s", reflect.canonical_name(callable)) raise
def errback(failure, expected, value=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, failure.value) f = fiber.Fiber() f.add_callback(common.break_chain) f.succeed(value or failure) return f
def check_depth(depth): state = fiber.get_state() desc = state["descriptor"] if not self._fid: self._fid = desc.fiber_id else: self.assertEqual(self._fid, desc.fiber_id) self.assertEqual(depth, desc.fiber_depth)
def callback(result, expected, value=None, exception=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, result) if exception: raise exception if value is None: return result return value
def depth3(): state = fiber.get_state() self.assertNotEqual(None, state) self.assertTrue("root" in state) self.assertTrue("depth1 in" in state) self.assertTrue("depth2 in" in state) state.add("depth3") return state
def check_fiber(result, fidref): # Using a ugly list to pass a reference to the fid fid = fidref and fidref[0] or None state = fiber.get_state() desc = state["descriptor"] if fid: self.assertEqual(fid, desc.fiber_id) else: fidref.append(desc.fiber_id) return result
def callback(result, expected, value=None, exception=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, result) if exception: d = common.break_chain(exception) d.addCallback(raise_error) return d else: if value is None: value = result return common.break_chain(value)
def depth1(): state = fiber.get_state() self.assertNotEqual(None, state) self.assertTrue("root" in state) state.add("depth1 in") sub_state = depth2() self.assertEqual(state, sub_state) self.assertTrue("depth2 in" in state) self.assertTrue("depth2 out" in state) self.assertTrue("depth3" in state) state.add("depth1 out") return state
def callback(result, expected, value=None, exception=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, result) if exception: f = fiber.Fiber() f.add_callback(common.break_chain) f.add_callback(raise_error) f.succeed(exception) return f else: if value is None: value = result f = fiber.Fiber() f.add_callback(common.break_chain) f.succeed(value) return f
def errback(failure, expected, value=None): self.assertNotEqual(None, fiber.get_state()) if expected is not None: self.assertEqual(expected, failure.value) return common.break_chain(value or failure)
def have_tag(tag): # Extra depth to account for this function frame state = fiber.get_state(depth=1) return state and tag in state