blob: 0ffe094d230f97238375b775e647398d4b4a76c1 [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
Caroline Tice88272d42016-01-13 09:48:29 -08007from __future__ import print_function
8
Luis Lozanof2a3ef42015-12-15 13:49:30 -08009__author__ = 'asharif@google.com (Ahmad Sharif)'
asharif455157b2013-02-15 21:15:05 +000010
yunlian8c9419b2013-02-19 21:11:29 +000011from multiprocessing import Process
12import time
13import unittest
asharif455157b2013-02-15 21:15:05 +000014
Caroline Tice88272d42016-01-13 09:48:29 -080015import file_lock_machine
yunlian8c9419b2013-02-19 21:11:29 +000016
17
18def LockAndSleep(machine):
Caroline Tice88272d42016-01-13 09:48:29 -080019 file_lock_machine.Machine(machine, auto=True).Lock(exclusive=True)
yunlian8c9419b2013-02-19 21:11:29 +000020 time.sleep(1)
asharif455157b2013-02-15 21:15:05 +000021
22
23class MachineTest(unittest.TestCase):
Caroline Tice88272d42016-01-13 09:48:29 -080024 """Class for testing machine locking."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025
asharif455157b2013-02-15 21:15:05 +000026 def setUp(self):
27 pass
28
asharif455157b2013-02-15 21:15:05 +000029 def testRepeatedUnlock(self):
Caroline Tice88272d42016-01-13 09:48:29 -080030 mach = file_lock_machine.Machine('qqqraymes.mtv')
31 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000032 self.assertFalse(mach.Unlock())
Caroline Tice88272d42016-01-13 09:48:29 -080033 mach = file_lock_machine.Machine('qqqraymes.mtv', auto=True)
34 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000035 self.assertFalse(mach.Unlock())
asharif455157b2013-02-15 21:15:05 +000036
37 def testLockUnlock(self):
Caroline Tice88272d42016-01-13 09:48:29 -080038 mach = file_lock_machine.Machine('otter.mtv', '/tmp')
39 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000040 self.assertTrue(mach.Lock(exclusive=True))
41 self.assertTrue(mach.Unlock(exclusive=True))
42
Caroline Tice88272d42016-01-13 09:48:29 -080043 mach = file_lock_machine.Machine('otter.mtv', '/tmp', True)
44 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000045 self.assertTrue(mach.Lock(exclusive=True))
46 self.assertTrue(mach.Unlock(exclusive=True))
47
asharif455157b2013-02-15 21:15:05 +000048 def testSharedLock(self):
Caroline Tice88272d42016-01-13 09:48:29 -080049 mach = file_lock_machine.Machine('chrotomation.mtv')
50 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000051 self.assertTrue(mach.Lock(exclusive=False))
Caroline Tice88272d42016-01-13 09:48:29 -080052 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000053 self.assertTrue(mach.Unlock(exclusive=False))
54 self.assertTrue(mach.Lock(exclusive=True))
55 self.assertTrue(mach.Unlock(exclusive=True))
56
Caroline Tice88272d42016-01-13 09:48:29 -080057 mach = file_lock_machine.Machine('chrotomation.mtv', auto=True)
58 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000059 self.assertTrue(mach.Lock(exclusive=False))
Caroline Tice88272d42016-01-13 09:48:29 -080060 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000061 self.assertTrue(mach.Unlock(exclusive=False))
62 self.assertTrue(mach.Lock(exclusive=True))
63 self.assertTrue(mach.Unlock(exclusive=True))
64
asharif455157b2013-02-15 21:15:05 +000065 def testExclusiveLock(self):
Caroline Tice88272d42016-01-13 09:48:29 -080066 mach = file_lock_machine.Machine('atree.mtv')
asharif455157b2013-02-15 21:15:05 +000067 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080068 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000069 self.assertFalse(mach.Lock(exclusive=True))
70 self.assertFalse(mach.Lock(exclusive=False))
71 self.assertTrue(mach.Unlock(exclusive=True))
72
Caroline Tice88272d42016-01-13 09:48:29 -080073 mach = file_lock_machine.Machine('atree.mtv', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000074 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080075 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000076 self.assertFalse(mach.Lock(exclusive=True))
77 self.assertFalse(mach.Lock(exclusive=False))
78 self.assertTrue(mach.Unlock(exclusive=True))
79
asharif455157b2013-02-15 21:15:05 +000080 def testExclusiveState(self):
Caroline Tice88272d42016-01-13 09:48:29 -080081 mach = file_lock_machine.Machine('testExclusiveState')
asharif455157b2013-02-15 21:15:05 +000082 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080083 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000084 self.assertFalse(mach.Lock(exclusive=False))
85 self.assertTrue(mach.Unlock(exclusive=True))
86
Caroline Tice88272d42016-01-13 09:48:29 -080087 mach = file_lock_machine.Machine('testExclusiveState', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000088 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080089 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000090 self.assertFalse(mach.Lock(exclusive=False))
91 self.assertTrue(mach.Unlock(exclusive=True))
92
93 def testAutoLockGone(self):
Caroline Tice88272d42016-01-13 09:48:29 -080094 mach = file_lock_machine.Machine('lockgone', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080095 p = Process(target=LockAndSleep, args=('lockgone',))
yunlian8c9419b2013-02-19 21:11:29 +000096 p.start()
97 time.sleep(1.1)
98 p.join()
99 self.assertTrue(mach.Lock(exclusive=True))
100
101 def testAutoLockFromOther(self):
Caroline Tice88272d42016-01-13 09:48:29 -0800102 mach = file_lock_machine.Machine('other_lock', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 p = Process(target=LockAndSleep, args=('other_lock',))
yunlian8c9419b2013-02-19 21:11:29 +0000104 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
yunlianb0eaee82013-02-19 21:13:28 +0000111 def testUnlockByOthers(self):
Caroline Tice88272d42016-01-13 09:48:29 -0800112 mach = file_lock_machine.Machine('other_unlock', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800113 p = Process(target=LockAndSleep, args=('other_unlock',))
yunlianb0eaee82013-02-19 21:13:28 +0000114 p.start()
115 time.sleep(0.5)
116 self.assertTrue(mach.Unlock(exclusive=True))
117 self.assertTrue(mach.Lock(exclusive=True))
118
119
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120if __name__ == '__main__':
asharif455157b2013-02-15 21:15:05 +0000121 unittest.main()