aboutsummaryrefslogtreecommitdiff
path: root/file_lock_machine_test.py
blob: 467c183d40899e0f8fbad9972cfa2dfce2957f14 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""lock_machine.py related unit-tests.

MachineManagerTest tests MachineManager.
"""


__author__ = "asharif@google.com (Ahmad Sharif)"

from multiprocessing import Process
import time
import unittest

import file_lock_machine


def LockAndSleep(machine):
    file_lock_machine.Machine(machine, "/tmp", auto=True).Lock(exclusive=True)
    time.sleep(1)


class MachineTest(unittest.TestCase):
    """Class for testing machine locking."""

    def setUp(self):
        pass

    def testRepeatedUnlock(self):
        mach = file_lock_machine.Machine("qqqraymes.mtv", "/tmp")
        for _ in range(10):
            self.assertTrue(mach.Unlock())
        mach = file_lock_machine.Machine("qqqraymes.mtv", "/tmp", auto=True)
        for _ in range(10):
            self.assertTrue(mach.Unlock())

    def testLockUnlock(self):
        mach = file_lock_machine.Machine("otter.mtv", "/tmp")
        for _ in range(10):
            self.assertTrue(mach.Lock(exclusive=True))
            self.assertTrue(mach.Unlock(exclusive=True))

        mach = file_lock_machine.Machine("otter.mtv", "/tmp", True)
        for _ in range(10):
            self.assertTrue(mach.Lock(exclusive=True))
            self.assertTrue(mach.Unlock(exclusive=True))

    def testSharedLock(self):
        mach = file_lock_machine.Machine("chrotomation.mtv", "/tmp")
        for _ in range(10):
            self.assertTrue(mach.Lock(exclusive=False))
        for _ in range(10):
            self.assertTrue(mach.Unlock(exclusive=False))
        self.assertTrue(mach.Lock(exclusive=True))
        self.assertTrue(mach.Unlock(exclusive=True))

        mach = file_lock_machine.Machine("chrotomation.mtv", "/tmp", auto=True)
        for _ in range(10):
            self.assertTrue(mach.Lock(exclusive=False))
        for _ in range(10):
            self.assertTrue(mach.Unlock(exclusive=False))
        self.assertTrue(mach.Lock(exclusive=True))
        self.assertTrue(mach.Unlock(exclusive=True))

    def testExclusiveLock(self):
        mach = file_lock_machine.Machine("atree.mtv", "/tmp")
        self.assertTrue(mach.Lock(exclusive=True))
        for _ in range(10):
            self.assertFalse(mach.Lock(exclusive=True))
            self.assertFalse(mach.Lock(exclusive=False))
        self.assertTrue(mach.Unlock(exclusive=True))

        mach = file_lock_machine.Machine("atree.mtv", "/tmp", auto=True)
        self.assertTrue(mach.Lock(exclusive=True))
        for _ in range(10):
            self.assertFalse(mach.Lock(exclusive=True))
            self.assertFalse(mach.Lock(exclusive=False))
        self.assertTrue(mach.Unlock(exclusive=True))

    def testExclusiveState(self):
        mach = file_lock_machine.Machine("testExclusiveState", "/tmp")
        self.assertTrue(mach.Lock(exclusive=True))
        for _ in range(10):
            self.assertFalse(mach.Lock(exclusive=False))
        self.assertTrue(mach.Unlock(exclusive=True))

        mach = file_lock_machine.Machine(
            "testExclusiveState", "/tmp", auto=True
        )
        self.assertTrue(mach.Lock(exclusive=True))
        for _ in range(10):
            self.assertFalse(mach.Lock(exclusive=False))
        self.assertTrue(mach.Unlock(exclusive=True))

    def testAutoLockGone(self):
        mach = file_lock_machine.Machine("lockgone", "/tmp", auto=True)
        p = Process(target=LockAndSleep, args=("lockgone",))
        p.start()
        time.sleep(1.1)
        p.join()
        self.assertTrue(mach.Lock(exclusive=True))

    def testAutoLockFromOther(self):
        mach = file_lock_machine.Machine("other_lock", "/tmp", auto=True)
        p = Process(target=LockAndSleep, args=("other_lock",))
        p.start()
        time.sleep(0.5)
        self.assertFalse(mach.Lock(exclusive=True))
        p.join()
        time.sleep(0.6)
        self.assertTrue(mach.Lock(exclusive=True))

    def testUnlockByOthers(self):
        mach = file_lock_machine.Machine("other_unlock", "/tmp", auto=True)
        p = Process(target=LockAndSleep, args=("other_unlock",))
        p.start()
        time.sleep(0.5)
        self.assertTrue(mach.Unlock(exclusive=True))
        self.assertTrue(mach.Lock(exclusive=True))


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