virt.virt_test_utils: run_autotest - 'tar' needs relative paths to strip the leading '/'
[autotest-zwu.git] / client / common_lib / barrier_unittest.py
blob58abeb37f2f9b300331ed1202c6bfbbbfb31f143
1 #!/usr/bin/python
3 __author__ = """Ashwin Ganti (aganti@google.com)"""
5 import os, sys, socket, errno, unittest, threading
6 from time import time, sleep
7 import common
8 from autotest_lib.client.common_lib import error, base_barrier
9 barrier = base_barrier
10 from autotest_lib.client.common_lib.test_utils import mock
13 class listen_server_test(unittest.TestCase):
15 def test_init(self):
16 server = barrier.listen_server()
17 server.close()
20 def test_close(self):
21 server = barrier.listen_server()
22 # cannot bind on the same port again
23 self.assertRaises(socket.error, barrier.listen_server)
24 server.close()
25 # now we can
26 server = barrier.listen_server()
27 server.close()
30 class barrier_test(unittest.TestCase):
32 def setUp(self):
33 self.god = mock.mock_god()
34 self.god.mock_io()
37 def tearDown(self):
38 self.god.unmock_io()
41 def test_initialize(self):
42 b = barrier.barrier('127.0.0.1#', 'testtag', 100, 11921)
43 self.assertEqual(b._hostid, '127.0.0.1#')
44 self.assertEqual(b._tag, 'testtag')
45 self.assertEqual(b._timeout_secs, 100)
46 self.assertEqual(b._port, 11921)
49 def test_get_host_from_id(self):
50 hostname = barrier.get_host_from_id('my_host')
51 self.assertEqual(hostname, 'my_host')
53 hostname = barrier.get_host_from_id('my_host#')
54 self.assertEqual(hostname, 'my_host')
56 self.assertRaises(error.BarrierError, barrier.get_host_from_id, '#my_host')
59 def test_update_timeout(self):
60 b = barrier.barrier('127.0.0.1#', 'update', 100)
61 b._update_timeout(120)
62 self.assertEqual(b._timeout_secs, 120)
65 def test_remaining(self):
66 b = barrier.barrier('127.0.0.1#', 'remain', 100)
67 remain = b._remaining()
68 self.assertEqual(remain, 100)
71 def test_master_welcome_garbage(self):
72 b = barrier.barrier('127.0.0.1#', 'garbage', 100)
73 waiting_before = dict(b._waiting)
74 seen_before = b._seen
76 sender, receiver = socket.socketpair()
77 try:
78 sender.send('GET /foobar?p=-1 HTTP/1.0\r\n\r\n')
79 # This should not raise an exception.
80 b._master_welcome((receiver, 'fakeaddr'))
82 self.assertEqual(waiting_before, b._waiting)
83 self.assertEqual(seen_before, b._seen)
85 sender, receiver = socket.socketpair()
86 sender.send('abcdefg\x00\x01\x02\n'*5)
87 # This should not raise an exception.
88 b._master_welcome((receiver, 'fakeaddr'))
90 self.assertEqual(waiting_before, b._waiting)
91 self.assertEqual(seen_before, b._seen)
92 finally:
93 sender.close()
94 receiver.close()
97 def test_rendezvous_basic(self):
98 # Basic rendezvous testing
99 self.rendezvous_test(60, port=11920)
102 def test_rendezvous_timeout(self):
103 # The rendezvous should time out here and throw a
104 # BarrierError since we are specifying a timeout of 0
105 self.assertRaises(error.BarrierError,
106 self.rendezvous_test, 0, port=11921)
109 def test_rendezvous_abort_ok(self):
110 # Test with abort flag set to not abort.
111 self.rendezvous_test(60, port=11920,
112 test_abort=True, abort=False)
115 def test_rendezvous_abort(self):
116 # The rendezvous should abort here and throw a
117 # BarrierError since we are asking to abort
118 self.assertRaises(error.BarrierError,
119 self.rendezvous_test, 0, port=11921,
120 test_abort=True, abort=True)
123 def test_rendezvous_servers_basic(self):
124 # The rendezvous should time out here and throw a
125 # BarrierError since we are specifying a timeout of 0
126 self.rendezvous_test(60, port=11921,
127 rendezvous_servers=True)
130 def test_rendezvous_servers_timeout(self):
131 # The rendezvous should time out here and throw a
132 # BarrierError since we are specifying a timeout of 0
133 self.assertRaises(error.BarrierError,
134 self.rendezvous_test, 0, port=11922,
135 rendezvous_servers=True)
138 def test_rendezvous_servers_abort_ok(self):
139 # Test with abort flag set to not abort.
140 self.rendezvous_test(60, port=11920, rendezvous_servers=True,
141 test_abort=True, abort=False)
144 def test_rendezvous_servers_abort(self):
145 # The rendezvous should abort here and throw a
146 # BarrierError since we are asking to abort
147 self.assertRaises(error.BarrierError,
148 self.rendezvous_test, 0, port=11922,
149 rendezvous_servers=True,
150 test_abort=True, abort=True)
153 # Internal utility function (not a unit test)
154 def rendezvous_test(self, timeout, port=11922,
155 rendezvous_servers=False, test_abort=False,
156 abort=False, listen_server=None):
157 if listen_server:
158 port = None
160 def _rdv(addr):
161 b1 = barrier.barrier(addr, "test_meeting", timeout, port,
162 listen_server=listen_server)
163 if not rendezvous_servers:
164 if test_abort:
165 b1.rendezvous('127.0.0.1#0', '127.0.0.1#1', abort=abort)
166 else:
167 b1.rendezvous('127.0.0.1#0', '127.0.0.1#1')
168 else:
169 if test_abort:
170 b1.rendezvous_servers('127.0.0.1#0', '127.0.0.1#1',
171 abort=abort)
172 else:
173 b1.rendezvous_servers('127.0.0.1#0', '127.0.0.1#1')
176 def _thread_rdv(addr):
177 # We need to ignore the exception on one side.
178 try:
179 _rdv(addr)
180 except error.BarrierError:
181 pass
183 client = threading.Thread(target=_thread_rdv,
184 args=('127.0.0.1#0',))
185 client.start()
186 _rdv('127.0.0.1#1')
187 client.join()
190 def test_reusing_listen_server(self):
192 Test that reusing the same listen server object works.
194 server = barrier.listen_server()
195 self.rendezvous_test(10, listen_server=server)
196 self.rendezvous_test(10, listen_server=server)
197 self.rendezvous_test(10, listen_server=server)
200 if __name__ == "__main__":
201 unittest.main()