asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 1 | # Copyright 2010 Google Inc. All Rights Reserved. |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 2 | """lock_machine.py related unit-tests. |
| 3 | |
| 4 | MachineManagerTest tests MachineManager. |
| 5 | """ |
| 6 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 9 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 10 | |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 11 | from multiprocessing import Process |
| 12 | import time |
| 13 | import unittest |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 14 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 15 | import file_lock_machine |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | def LockAndSleep(machine): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 19 | file_lock_machine.Machine(machine, auto=True).Lock(exclusive=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 20 | time.sleep(1) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class MachineTest(unittest.TestCase): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 24 | """Class for testing machine locking.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 25 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 26 | def setUp(self): |
| 27 | pass |
| 28 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 29 | def testRepeatedUnlock(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 30 | mach = file_lock_machine.Machine('qqqraymes.mtv') |
| 31 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 32 | self.assertFalse(mach.Unlock()) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 33 | mach = file_lock_machine.Machine('qqqraymes.mtv', auto=True) |
| 34 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 35 | self.assertFalse(mach.Unlock()) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 36 | |
| 37 | def testLockUnlock(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 38 | mach = file_lock_machine.Machine('otter.mtv', '/tmp') |
| 39 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 40 | self.assertTrue(mach.Lock(exclusive=True)) |
| 41 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 42 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 43 | mach = file_lock_machine.Machine('otter.mtv', '/tmp', True) |
| 44 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 45 | self.assertTrue(mach.Lock(exclusive=True)) |
| 46 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 47 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 48 | def testSharedLock(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 49 | mach = file_lock_machine.Machine('chrotomation.mtv') |
| 50 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 51 | self.assertTrue(mach.Lock(exclusive=False)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 52 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 53 | self.assertTrue(mach.Unlock(exclusive=False)) |
| 54 | self.assertTrue(mach.Lock(exclusive=True)) |
| 55 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 56 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 57 | mach = file_lock_machine.Machine('chrotomation.mtv', auto=True) |
| 58 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 59 | self.assertTrue(mach.Lock(exclusive=False)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 60 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 61 | self.assertTrue(mach.Unlock(exclusive=False)) |
| 62 | self.assertTrue(mach.Lock(exclusive=True)) |
| 63 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 64 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 65 | def testExclusiveLock(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 66 | mach = file_lock_machine.Machine('atree.mtv') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 67 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 68 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 69 | self.assertFalse(mach.Lock(exclusive=True)) |
| 70 | self.assertFalse(mach.Lock(exclusive=False)) |
| 71 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 72 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 73 | mach = file_lock_machine.Machine('atree.mtv', auto=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 74 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 75 | for _ in range(10): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 76 | self.assertFalse(mach.Lock(exclusive=True)) |
| 77 | self.assertFalse(mach.Lock(exclusive=False)) |
| 78 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 79 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 80 | def testExclusiveState(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 81 | mach = file_lock_machine.Machine('testExclusiveState') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 82 | self.assertTrue(mach.Lock(exclusive=True)) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 83 | for _ in range(10): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 84 | self.assertFalse(mach.Lock(exclusive=False)) |
| 85 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 86 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 87 | mach = file_lock_machine.Machine('testExclusiveState', auto=True) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +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): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 90 | self.assertFalse(mach.Lock(exclusive=False)) |
| 91 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 92 | |
| 93 | def testAutoLockGone(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 94 | mach = file_lock_machine.Machine('lockgone', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 95 | p = Process(target=LockAndSleep, args=('lockgone',)) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 96 | p.start() |
| 97 | time.sleep(1.1) |
| 98 | p.join() |
| 99 | self.assertTrue(mach.Lock(exclusive=True)) |
| 100 | |
| 101 | def testAutoLockFromOther(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 102 | mach = file_lock_machine.Machine('other_lock', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 103 | p = Process(target=LockAndSleep, args=('other_lock',)) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 104 | p.start() |
| 105 | time.sleep(0.5) |
| 106 | self.assertFalse(mach.Lock(exclusive=True)) |
| 107 | p.join() |
| 108 | time.sleep(0.6) |
| 109 | self.assertTrue(mach.Lock(exclusive=True)) |
| 110 | |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 111 | def testUnlockByOthers(self): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 112 | mach = file_lock_machine.Machine('other_unlock', auto=True) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 113 | p = Process(target=LockAndSleep, args=('other_unlock',)) |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 114 | p.start() |
| 115 | time.sleep(0.5) |
| 116 | self.assertTrue(mach.Unlock(exclusive=True)) |
| 117 | self.assertTrue(mach.Lock(exclusive=True)) |
| 118 | |
| 119 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 120 | if __name__ == '__main__': |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 121 | unittest.main() |