File: mpm_utils_tests.py

package info (click to toggle)
uhd 4.9.0.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184,180 kB
  • sloc: cpp: 262,887; python: 112,011; ansic: 102,670; vhdl: 57,031; tcl: 19,924; xml: 8,581; makefile: 3,028; sh: 2,812; pascal: 230; javascript: 120; csh: 94; asm: 20; perl: 11
file content (55 lines) | stat: -rw-r--r-- 1,432 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# Copyright 2020 Ettus Research, a National Instruments Brand
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
import unittest
from base_tests import TestBase
from usrp_mpm import mpmutils


class MockLockable:
    """
    Class which exposes whether lock() or unlock() have been called on it
    """
    def __init__(self):
        self.locked = False

    def lock(self):
        self.locked = True

    def unlock(self):
        self.locked = False


class TestMpmUtils(TestBase):
    """
    Tests for the myriad utilities in mpmutils
    """
    def test_normal_usage(self):
        """
        Checks whether in normal operation the resource gets unlocked
        """
        my_resource = MockLockable()
        with mpmutils.lock_guard(my_resource):
            self.assertEqual(my_resource.locked, True)
        self.assertEqual(my_resource.locked, False)

    def test_unlocks_after_exception(self):
        """
        Checked whether the resource gets unlocked after an exception occurs
        """
        my_resource = MockLockable()
        try:
            with mpmutils.lock_guard(my_resource):
                self.assertEqual(my_resource.locked, True)
                raise Exception("This is just a drill")
        except Exception:
            # Eat the raised exception
            pass
        finally:
            self.assertEqual(my_resource.locked, False)


if __name__ == '__main__':
    unittest.main()