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