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