Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 8 | """lock_machine.py related unit-tests. |
| 9 | |
| 10 | MachineManagerTest tests MachineManager. |
| 11 | """ |
| 12 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 13 | from __future__ import print_function |
| 14 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 15 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 16 | |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 17 | from multiprocessing import Process |
| 18 | import time |
| 19 | import unittest |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 20 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 21 | import file_lock_machine |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def LockAndSleep(machine): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 25 | file_lock_machine.Machine(machine, '/tmp', auto=True).Lock(exclusive=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 26 | time.sleep(1) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class MachineTest(unittest.TestCase): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 30 | """Class for testing machine locking.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 31 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 32 | def setUp(self): |
| 33 | pass |
| 34 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 35 | def testRepeatedUnlock(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 36 | mach = file_lock_machine.Machine('qqqraymes.mtv', '/tmp') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 37 | for _ in range(10): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 38 | self.assertTrue(mach.Unlock()) |
| 39 | mach = file_lock_machine.Machine('qqqraymes.mtv', '/tmp', auto=True) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 40 | for _ in range(10): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 41 | self.assertTrue(mach.Unlock()) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 42 | |
| 43 | def testLockUnlock(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 44 | mach = file_lock_machine.Machine('otter.mtv', '/tmp') |
| 45 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 46 | self.assertTrue(mach.Lock(exclusive=True)) |
| 47 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 48 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 49 | mach = file_lock_machine.Machine('otter.mtv', '/tmp', True) |
| 50 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 51 | self.assertTrue(mach.Lock(exclusive=True)) |
| 52 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 53 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 54 | def testSharedLock(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 55 | mach = file_lock_machine.Machine('chrotomation.mtv', '/tmp') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 56 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 57 | self.assertTrue(mach.Lock(exclusive=False)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 58 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 59 | self.assertTrue(mach.Unlock(exclusive=False)) |
| 60 | self.assertTrue(mach.Lock(exclusive=True)) |
| 61 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 62 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 63 | mach = file_lock_machine.Machine('chrotomation.mtv', '/tmp', auto=True) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 64 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 65 | self.assertTrue(mach.Lock(exclusive=False)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 66 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 67 | self.assertTrue(mach.Unlock(exclusive=False)) |
| 68 | self.assertTrue(mach.Lock(exclusive=True)) |
| 69 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 70 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 71 | def testExclusiveLock(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 72 | mach = file_lock_machine.Machine('atree.mtv', '/tmp') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 73 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 74 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 75 | self.assertFalse(mach.Lock(exclusive=True)) |
| 76 | self.assertFalse(mach.Lock(exclusive=False)) |
| 77 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 78 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 79 | mach = file_lock_machine.Machine('atree.mtv', '/tmp', auto=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 80 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 81 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 82 | self.assertFalse(mach.Lock(exclusive=True)) |
| 83 | self.assertFalse(mach.Lock(exclusive=False)) |
| 84 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 85 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 86 | def testExclusiveState(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 87 | mach = file_lock_machine.Machine('testExclusiveState', '/tmp') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 88 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 89 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 90 | self.assertFalse(mach.Lock(exclusive=False)) |
| 91 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 92 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 93 | mach = file_lock_machine.Machine('testExclusiveState', '/tmp', auto=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 94 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 95 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 96 | self.assertFalse(mach.Lock(exclusive=False)) |
| 97 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 98 | |
| 99 | def testAutoLockGone(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 100 | mach = file_lock_machine.Machine('lockgone', '/tmp', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 101 | p = Process(target=LockAndSleep, args=('lockgone',)) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 102 | p.start() |
| 103 | time.sleep(1.1) |
| 104 | p.join() |
| 105 | self.assertTrue(mach.Lock(exclusive=True)) |
| 106 | |
| 107 | def testAutoLockFromOther(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 108 | mach = file_lock_machine.Machine('other_lock', '/tmp', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 109 | p = Process(target=LockAndSleep, args=('other_lock',)) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 110 | p.start() |
| 111 | time.sleep(0.5) |
| 112 | self.assertFalse(mach.Lock(exclusive=True)) |
| 113 | p.join() |
| 114 | time.sleep(0.6) |
| 115 | self.assertTrue(mach.Lock(exclusive=True)) |
| 116 | |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 117 | def testUnlockByOthers(self): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 118 | mach = file_lock_machine.Machine('other_unlock', '/tmp', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 119 | p = Process(target=LockAndSleep, args=('other_unlock',)) |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 120 | p.start() |
| 121 | time.sleep(0.5) |
| 122 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 123 | self.assertTrue(mach.Lock(exclusive=True)) |
| 124 | |
| 125 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 126 | if __name__ == '__main__': |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 127 | unittest.main() |