3 __author__
= """Ashwin Ganti (aganti@google.com)"""
5 import os
, sys
, socket
, errno
, unittest
, threading
6 from time
import time
, sleep
8 from autotest_lib
.client
.common_lib
import error
, base_barrier
10 from autotest_lib
.client
.common_lib
.test_utils
import mock
13 class listen_server_test(unittest
.TestCase
):
16 server
= barrier
.listen_server()
21 server
= barrier
.listen_server()
22 # cannot bind on the same port again
23 self
.assertRaises(socket
.error
, barrier
.listen_server
)
26 server
= barrier
.listen_server()
30 class barrier_test(unittest
.TestCase
):
33 self
.god
= mock
.mock_god()
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
)
76 sender
, receiver
= socket
.socketpair()
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
)
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):
161 b1
= barrier
.barrier(addr
, "test_meeting", timeout
, port
,
162 listen_server
=listen_server
)
163 if not rendezvous_servers
:
165 b1
.rendezvous('127.0.0.1#0', '127.0.0.1#1', abort
=abort
)
167 b1
.rendezvous('127.0.0.1#0', '127.0.0.1#1')
170 b1
.rendezvous_servers('127.0.0.1#0', '127.0.0.1#1',
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.
180 except error
.BarrierError
:
183 client
= threading
.Thread(target
=_thread_rdv
,
184 args
=('127.0.0.1#0',))
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__":