Merge branch '8784-dir-listings'
[arvados.git] / services / nodemanager / tests / test_failure.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 errno
9 import logging
10 import time
11 import threading
12 import unittest
13
14 import mock
15 import pykka
16
17 from . import testutil
18
19 import arvnodeman.baseactor
20
21 class BogusActor(arvnodeman.baseactor.BaseNodeManagerActor):
22     def __init__(self, e):
23         super(BogusActor, self).__init__()
24         self.exp = e
25
26     def doStuff(self):
27         raise self.exp
28
29     def ping(self):
30         # Called by WatchdogActorTest, this delay is longer than the test timeout
31         # of 1 second, which should cause the watchdog ping to fail.
32         time.sleep(4)
33         return True
34
35 class ActorUnhandledExceptionTest(testutil.ActorTestMixin, unittest.TestCase):
36     def test_fatal_error(self):
37         for e in (MemoryError(), threading.ThreadError(), OSError(errno.ENOMEM, "")):
38             with mock.patch('os.kill') as kill_mock:
39                 act = BogusActor.start(e).tell_proxy()
40                 act.doStuff()
41                 act.actor_ref.stop(block=True)
42                 self.assertTrue(kill_mock.called)
43
44     @mock.patch('os.kill')
45     def test_nonfatal_error(self, kill_mock):
46         act = BogusActor.start(OSError(errno.ENOENT, "")).tell_proxy()
47         act.doStuff()
48         act.actor_ref.stop(block=True)
49         self.assertFalse(kill_mock.called)
50
51 class WatchdogActorTest(testutil.ActorTestMixin, unittest.TestCase):
52     @mock.patch('os.kill')
53     def test_time_timout(self, kill_mock):
54         act = BogusActor.start(OSError(errno.ENOENT, ""))
55         watch = arvnodeman.baseactor.WatchdogActor.start(1, act)
56         watch.stop(block=True)
57         act.stop(block=True)
58         self.assertTrue(kill_mock.called)