Beispiel #1
0
 def test_lease_False(self,
                      mock_retry,
                      mock_queue_error,
                      mock_release_lease,
                      mock_acquire_lease,
                      mock_uuid):
     mock_uuid.uuid4.return_value.hex = 'bobloblaw'
     mock_acquire_lease.return_value = False
     mock_release_lease.return_value = False
     instance = Context('name')
     instance.dispatch('event', {'foo': 'bar'})
     mock_acquire_lease.assert_called_with(
         'bobloblaw',
         0,
         0,
         primary=False
     )
     mock_release_lease.assert_called_with(
         'bobloblaw',
         0,
         0,
         False,
         primary=False
     )
     self.assertEqual(
         [
             mock.call('cache', 'System error acquiring primary=True lease.'),
             mock.call('cache', 'Could not acquire lease. Retrying.'),
             mock.call('cache', 'Could not release lease.')
         ],
         mock_queue_error.mock_calls
     )
     mock_retry.assert_called_with(
         {'foo': 'bar'}
     )
Beispiel #2
0
 def test_lease_True(self,
                     mock_dispatch_and_retry,
                     mock_queue_error,
                     mock_release_lease,
                     mock_acquire_lease,
                     mock_uuid):
     mock_uuid.uuid4.return_value.hex = 'bobloblaw'
     mock_acquire_lease.return_value = 'foobar'
     mock_release_lease.return_value = 'foobar'
     instance = Context('name')
     instance.dispatch('event', {'foo': 'bar'})
     mock_acquire_lease.assert_called_with(
         'bobloblaw',
         0,
         0,
         primary=True
     )
     mock_release_lease.assert_called_with(
         'bobloblaw',
         0,
         0,
         'foobar',
         primary=True
     )
     self.assertFalse(mock_queue_error.called)
     mock_dispatch_and_retry.assert_called_with(
         'event',
         {'foo': 'bar'}
     )
Beispiel #3
0
    def test_more_retries(self):
        context = Context('name', max_retries=1)
        context.retries = 0

        action = self.MyAction('name')

        with self.assertRaises(RuntimeError):
            action.execute(context, None)
Beispiel #4
0
 def test_execute_with_action(self):
     context = Context('name', initial_user_context={'foo': 'bar'})
     self.assertEqual(None, context.current_state)
     action = mock.Mock()
     t = Transition('name', 'target', action=action)
     t.execute(context, 'obj')
     self.assertEqual('target', context.current_state)
     action.execute.assert_called_with({'foo': 'bar'}, 'obj')
Beispiel #5
0
    def test_max_retry(self):
        context = Context('name', max_retries=1)
        context.retries = 1

        action = self.MyAction('name')

        event = action.execute(context, None)

        self.assertEqual(event, 'fail')
 def test_lease_fence_token_int(self, mock_retry, mock_queue_error,
                                mock_release_lease, mock_acquire_lease,
                                mock_uuid):
     mock_uuid.uuid4.return_value.hex = 'bobloblaw'
     # equivalent to long in Python 2
     mock_acquire_lease.return_value = int(500)
     mock_release_lease.return_value = False
     instance = Context('name')
     obj = {'foo': 'bar'}
     instance.dispatch('event', obj)
     self.assertTrue(
         'fence_token' in obj,
         "Fence token should be set on the object if it's a numeric type.")
     self.assertEqual(obj['fence_token'], int(500))
 def test_dispatch_to_final_no_event(self):
     entry_action = mock.Mock()
     do_action = mock.Mock()
     exit_action = mock.Mock()
     transition = mock.Mock()
     state = State('name',
                   entry_action=entry_action,
                   do_action=do_action,
                   exit_action=exit_action,
                   final=True)
     state.add_transition(transition, 'event')
     context = Context('name', initial_state=state)
     context['foo'] = 'bar'
     event = state.dispatch(context, 'event', 'obj')
     self.assertIsNone(event)
 def test_dispatch(self):
     entry_action = mock.Mock()
     do_action = mock.Mock()
     exit_action = mock.Mock()
     transition = mock.Mock()
     state = State('name',
                   entry_action=entry_action,
                   do_action=do_action,
                   exit_action=exit_action)
     state.add_transition(transition, 'event')
     context = Context('name',
                       initial_state=state,
                       initial_system_context={'sys': 'tem'},
                       initial_user_context={'foo': 'bar'})
     event = state.dispatch(context, 'event', 'obj')
     exit_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     do_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     entry_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     transition.execute.assert_called_with({'foo': 'bar'}, 'obj')
     self.assertIsNotNone(event)
Beispiel #9
0
 def test_execute_no_action(self):
     context = Context('name')
     self.assertEqual(None, context.current_state)
     t = Transition('name', 'target')
     t.execute(context, None)
     self.assertEqual('target', context.current_state)