blob: bc20a88bacfcfcef40a19196283410a694541c44 [file] [log] [blame]
Zhizhou Yang5534af82020-01-15 16:25:04 -08001#!/usr/bin/env python3
Zhizhou Yangbf7ee872019-10-14 17:36:09 -07002# -*- 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
asharif455157b2013-02-15 21:15:05 +00008"""lock_machine.py related unit-tests.
9
10MachineManagerTest tests MachineManager.
11"""
12
Caroline Tice88272d42016-01-13 09:48:29 -080013from __future__ import print_function
14
Luis Lozanof2a3ef42015-12-15 13:49:30 -080015__author__ = 'asharif@google.com (Ahmad Sharif)'
asharif455157b2013-02-15 21:15:05 +000016
yunlian8c9419b2013-02-19 21:11:29 +000017from multiprocessing import Process
18import time
19import unittest
asharif455157b2013-02-15 21:15:05 +000020
Caroline Tice88272d42016-01-13 09:48:29 -080021import file_lock_machine
yunlian8c9419b2013-02-19 21:11:29 +000022
23
24def LockAndSleep(machine):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070025 file_lock_machine.Machine(machine, '/tmp', auto=True).Lock(exclusive=True)
yunlian8c9419b2013-02-19 21:11:29 +000026 time.sleep(1)
asharif455157b2013-02-15 21:15:05 +000027
28
29class MachineTest(unittest.TestCase):
Caroline Tice88272d42016-01-13 09:48:29 -080030 """Class for testing machine locking."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080031
asharif455157b2013-02-15 21:15:05 +000032 def setUp(self):
33 pass
34
asharif455157b2013-02-15 21:15:05 +000035 def testRepeatedUnlock(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070036 mach = file_lock_machine.Machine('qqqraymes.mtv', '/tmp')
Caroline Tice88272d42016-01-13 09:48:29 -080037 for _ in range(10):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070038 self.assertTrue(mach.Unlock())
39 mach = file_lock_machine.Machine('qqqraymes.mtv', '/tmp', auto=True)
Caroline Tice88272d42016-01-13 09:48:29 -080040 for _ in range(10):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070041 self.assertTrue(mach.Unlock())
asharif455157b2013-02-15 21:15:05 +000042
43 def testLockUnlock(self):
Caroline Tice88272d42016-01-13 09:48:29 -080044 mach = file_lock_machine.Machine('otter.mtv', '/tmp')
45 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000046 self.assertTrue(mach.Lock(exclusive=True))
47 self.assertTrue(mach.Unlock(exclusive=True))
48
Caroline Tice88272d42016-01-13 09:48:29 -080049 mach = file_lock_machine.Machine('otter.mtv', '/tmp', True)
50 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000051 self.assertTrue(mach.Lock(exclusive=True))
52 self.assertTrue(mach.Unlock(exclusive=True))
53
asharif455157b2013-02-15 21:15:05 +000054 def testSharedLock(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070055 mach = file_lock_machine.Machine('chrotomation.mtv', '/tmp')
Caroline Tice88272d42016-01-13 09:48:29 -080056 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000057 self.assertTrue(mach.Lock(exclusive=False))
Caroline Tice88272d42016-01-13 09:48:29 -080058 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000059 self.assertTrue(mach.Unlock(exclusive=False))
60 self.assertTrue(mach.Lock(exclusive=True))
61 self.assertTrue(mach.Unlock(exclusive=True))
62
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070063 mach = file_lock_machine.Machine('chrotomation.mtv', '/tmp', auto=True)
Caroline Tice88272d42016-01-13 09:48:29 -080064 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000065 self.assertTrue(mach.Lock(exclusive=False))
Caroline Tice88272d42016-01-13 09:48:29 -080066 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000067 self.assertTrue(mach.Unlock(exclusive=False))
68 self.assertTrue(mach.Lock(exclusive=True))
69 self.assertTrue(mach.Unlock(exclusive=True))
70
asharif455157b2013-02-15 21:15:05 +000071 def testExclusiveLock(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070072 mach = file_lock_machine.Machine('atree.mtv', '/tmp')
asharif455157b2013-02-15 21:15:05 +000073 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080074 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000075 self.assertFalse(mach.Lock(exclusive=True))
76 self.assertFalse(mach.Lock(exclusive=False))
77 self.assertTrue(mach.Unlock(exclusive=True))
78
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070079 mach = file_lock_machine.Machine('atree.mtv', '/tmp', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000080 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080081 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000082 self.assertFalse(mach.Lock(exclusive=True))
83 self.assertFalse(mach.Lock(exclusive=False))
84 self.assertTrue(mach.Unlock(exclusive=True))
85
asharif455157b2013-02-15 21:15:05 +000086 def testExclusiveState(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070087 mach = file_lock_machine.Machine('testExclusiveState', '/tmp')
asharif455157b2013-02-15 21:15:05 +000088 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080089 for _ in range(10):
asharif455157b2013-02-15 21:15:05 +000090 self.assertFalse(mach.Lock(exclusive=False))
91 self.assertTrue(mach.Unlock(exclusive=True))
92
Zhizhou Yangbf7ee872019-10-14 17:36:09 -070093 mach = file_lock_machine.Machine('testExclusiveState', '/tmp', auto=True)
yunlian8c9419b2013-02-19 21:11:29 +000094 self.assertTrue(mach.Lock(exclusive=True))
Caroline Tice88272d42016-01-13 09:48:29 -080095 for _ in range(10):
yunlian8c9419b2013-02-19 21:11:29 +000096 self.assertFalse(mach.Lock(exclusive=False))
97 self.assertTrue(mach.Unlock(exclusive=True))
98
99 def testAutoLockGone(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -0700100 mach = file_lock_machine.Machine('lockgone', '/tmp', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 p = Process(target=LockAndSleep, args=('lockgone',))
yunlian8c9419b2013-02-19 21:11:29 +0000102 p.start()
103 time.sleep(1.1)
104 p.join()
105 self.assertTrue(mach.Lock(exclusive=True))
106
107 def testAutoLockFromOther(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -0700108 mach = file_lock_machine.Machine('other_lock', '/tmp', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800109 p = Process(target=LockAndSleep, args=('other_lock',))
yunlian8c9419b2013-02-19 21:11:29 +0000110 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
yunlianb0eaee82013-02-19 21:13:28 +0000117 def testUnlockByOthers(self):
Zhizhou Yangbf7ee872019-10-14 17:36:09 -0700118 mach = file_lock_machine.Machine('other_unlock', '/tmp', auto=True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800119 p = Process(target=LockAndSleep, args=('other_unlock',))
yunlianb0eaee82013-02-19 21:13:28 +0000120 p.start()
121 time.sleep(0.5)
122 self.assertTrue(mach.Unlock(exclusive=True))
123 self.assertTrue(mach.Lock(exclusive=True))
124
125
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126if __name__ == '__main__':
asharif455157b2013-02-15 21:15:05 +0000127 unittest.main()