blob: 7634e2a5f8cb2d5a9992a032f443439ff1f2a583 [file] [log] [blame]
asharif455157b2013-02-15 21:15:05 +00001# Copyright 2010 Google Inc. All Rights Reserved.
asharif455157b2013-02-15 21:15:05 +00002"""lock_machine.py related unit-tests.
3
4MachineManagerTest tests MachineManager.
5"""
6
Luis Lozanof2a3ef42015-12-15 13:49:30 -08007__author__ = 'asharif@google.com (Ahmad Sharif)'
asharif455157b2013-02-15 21:15:05 +00008
yunlian8c9419b2013-02-19 21:11:29 +00009from multiprocessing import Process
10import time
11import unittest
asharif455157b2013-02-15 21:15:05 +000012
13import lock_machine
yunlian8c9419b2013-02-19 21:11:29 +000014
15
16def LockAndSleep(machine):
17 lock_machine.Machine(machine, auto=True).Lock(exclusive=True)
18 time.sleep(1)
asharif455157b2013-02-15 21:15:05 +000019
20
21class MachineTest(unittest.TestCase):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080022
asharif455157b2013-02-15 21:15:05 +000023 def setUp(self):
24 pass
25
asharif455157b2013-02-15 21:15:05 +000026 def testRepeatedUnlock(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027 mach = lock_machine.Machine('qqqraymes.mtv')
asharif455157b2013-02-15 21:15:05 +000028 for i in range(10):
29 self.assertFalse(mach.Unlock())
Luis Lozanof2a3ef42015-12-15 13:49:30 -080030 mach = lock_machine.Machine('qqqraymes.mtv', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000031 for i in range(10):
32 self.assertFalse(mach.Unlock())
asharif455157b2013-02-15 21:15:05 +000033
34 def testLockUnlock(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080035 mach = lock_machine.Machine('otter.mtv', '/tmp')
asharif455157b2013-02-15 21:15:05 +000036 for i in range(10):
37 self.assertTrue(mach.Lock(exclusive=True))
38 self.assertTrue(mach.Unlock(exclusive=True))
39
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 mach = lock_machine.Machine('otter.mtv', '/tmp', True)
yunlian8c9419b2013-02-19 21:11:29 +000041 for i in range(10):
42 self.assertTrue(mach.Lock(exclusive=True))
43 self.assertTrue(mach.Unlock(exclusive=True))
44
asharif455157b2013-02-15 21:15:05 +000045 def testSharedLock(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 mach = lock_machine.Machine('chrotomation.mtv')
asharif455157b2013-02-15 21:15:05 +000047 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 Lozanof2a3ef42015-12-15 13:49:30 -080054 mach = lock_machine.Machine('chrotomation.mtv', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000055 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
asharif455157b2013-02-15 21:15:05 +000062 def testExclusiveLock(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 mach = lock_machine.Machine('atree.mtv')
asharif455157b2013-02-15 21:15:05 +000064 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 Lozanof2a3ef42015-12-15 13:49:30 -080070 mach = lock_machine.Machine('atree.mtv', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000071 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
asharif455157b2013-02-15 21:15:05 +000077 def testExclusiveState(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 mach = lock_machine.Machine('testExclusiveState')
asharif455157b2013-02-15 21:15:05 +000079 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 Lozanof2a3ef42015-12-15 13:49:30 -080084 mach = lock_machine.Machine('testExclusiveState', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000085 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 Lozanof2a3ef42015-12-15 13:49:30 -080091 mach = lock_machine.Machine('lockgone', auto=True)
92 p = Process(target=LockAndSleep, args=('lockgone',))
yunlian8c9419b2013-02-19 21:11:29 +000093 p.start()
94 time.sleep(1.1)
95 p.join()
96 self.assertTrue(mach.Lock(exclusive=True))
97
98 def testAutoLockFromOther(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080099 mach = lock_machine.Machine('other_lock', auto=True)
100 p = Process(target=LockAndSleep, args=('other_lock',))
yunlian8c9419b2013-02-19 21:11:29 +0000101 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
yunlianb0eaee82013-02-19 21:13:28 +0000108 def testUnlockByOthers(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800109 mach = lock_machine.Machine('other_unlock', auto=True)
110 p = Process(target=LockAndSleep, args=('other_unlock',))
yunlianb0eaee82013-02-19 21:13:28 +0000111 p.start()
112 time.sleep(0.5)
113 self.assertTrue(mach.Unlock(exclusive=True))
114 self.assertTrue(mach.Lock(exclusive=True))
115
116
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117if __name__ == '__main__':
asharif455157b2013-02-15 21:15:05 +0000118 unittest.main()