Merge branch '3656-arv-create' closes #3656
[arvados.git] / services / nodemanager / tests / test_timedcallback.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import time
6 import unittest
7
8 import mock
9 import pykka
10
11 import arvnodeman.timedcallback as timedcallback
12 from . import testutil
13
14 @testutil.no_sleep
15 class TimedCallBackActorTestCase(testutil.ActorTestMixin, unittest.TestCase):
16     def test_immediate_turnaround(self):
17         receiver = mock.Mock()
18         deliverer = timedcallback.TimedCallBackActor.start().proxy()
19         deliverer.schedule(time.time() - 1, receiver,
20                            'immediate').get(self.TIMEOUT)
21         self.stop_proxy(deliverer)
22         receiver.assert_called_with('immediate')
23
24     def test_delayed_turnaround(self):
25         receiver = mock.Mock()
26         with mock.patch('time.time', return_value=0) as mock_now:
27             deliverer = timedcallback.TimedCallBackActor.start().proxy()
28             deliverer.schedule(1, receiver, 'delayed')
29             deliverer.schedule(3, receiver, 'failure').get(self.TIMEOUT)
30             self.assertFalse(receiver.called)
31             mock_now.return_value = 2
32             deliverer.schedule(3, receiver, 'failure').get(self.TIMEOUT)
33             self.stop_proxy(deliverer)
34         receiver.assert_called_with('delayed')
35
36     def test_out_of_order_scheduling(self):
37         receiver = mock.Mock()
38         with mock.patch('time.time', return_value=1.5) as mock_now:
39             deliverer = timedcallback.TimedCallBackActor.start().proxy()
40             deliverer.schedule(2, receiver, 'second')
41             deliverer.schedule(1, receiver, 'first')
42             deliverer.schedule(3, receiver, 'failure').get(self.TIMEOUT)
43             receiver.assert_called_with('first')
44             mock_now.return_value = 2.5
45             deliverer.schedule(3, receiver, 'failure').get(self.TIMEOUT)
46             self.stop_proxy(deliverer)
47         receiver.assert_called_with('second')
48
49     def test_dead_actors_ignored(self):
50         receiver = mock.Mock(name='dead_actor', spec=pykka.ActorRef)
51         receiver.tell.side_effect = pykka.ActorDeadError
52         deliverer = timedcallback.TimedCallBackActor.start().proxy()
53         deliverer.schedule(time.time() - 1, receiver.tell,
54                            'error').get(self.TIMEOUT)
55         self.assertTrue(self.stop_proxy(deliverer), "deliverer died")
56         receiver.tell.assert_called_with('error')
57
58
59 if __name__ == '__main__':
60     unittest.main()
61