Skip to content

Commit f89f1ba

Browse files
committed
Fix Unix socket test server cleanup
Shut down the Unix socket test server before closing it and wait for the server thread to finish. This avoids a race where serve_forever can access a closed file descriptor and the client can fail with BrokenPipeError in CI.
1 parent 7f355dd commit f89f1ba

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

tests/connections/test_debug_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from gvm.connections._connection import AbstractGvmConnection
99

1010

11-
class TestConnection(AbstractGvmConnection):
11+
class DummyConnection(AbstractGvmConnection):
1212
def connect(self) -> None:
1313
pass
1414

1515

1616
class DebugConnectionTestCase(unittest.TestCase):
1717
def test_is_gvm_connection(self):
18-
connection = DebugConnection(TestConnection())
18+
connection = DebugConnection(DummyConnection())
1919
self.assertTrue(isinstance(connection, GvmConnection))

tests/connections/test_gvm_connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
from gvm.errors import GvmError
1515

1616

17-
class TestConnection(AbstractGvmConnection):
17+
class DummyConnection(AbstractGvmConnection):
1818
def connect(self) -> None:
1919
pass
2020

2121

2222
class GvmConnectionTestCase(unittest.TestCase):
2323
# pylint: disable=protected-access
2424
def test_init_no_args(self):
25-
connection = TestConnection()
25+
connection = DummyConnection()
2626

2727
self.assertIsNone(connection._socket)
2828
self.assertEqual(connection._timeout, DEFAULT_TIMEOUT)
2929

3030
def test_init_with_none(self):
31-
connection = TestConnection(timeout=None)
31+
connection = DummyConnection(timeout=None)
3232

3333
self.assertIsNone(connection._socket)
3434
self.assertEqual(connection._timeout, DEFAULT_TIMEOUT)
@@ -37,7 +37,7 @@ def test_read_no_data(self):
3737
read_mock = MagicMock()
3838
read_mock.return_value = None
3939

40-
connection = TestConnection()
40+
connection = DummyConnection()
4141
connection._read = read_mock
4242

4343
with self.assertRaises(GvmError, msg="Remote closed the connection"):
@@ -49,7 +49,7 @@ def test_read_trigger_timeout(self):
4949
read_mock = MagicMock()
5050
read_mock.side_effect = [b"<foo>xyz<bar></bar>", b"</foo>"]
5151

52-
connection = TestConnection(timeout=0)
52+
connection = DummyConnection(timeout=0)
5353
connection._read = read_mock
5454

5555
with self.assertRaises(
@@ -58,5 +58,5 @@ def test_read_trigger_timeout(self):
5858
connection.read()
5959

6060
def test_is_gvm_connection(self):
61-
connection = TestConnection()
61+
connection = DummyConnection()
6262
self.assertTrue(isinstance(connection, GvmConnection))

tests/connections/test_unix_socket_connection.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
class DummyRequestHandler(socketserver.BaseRequestHandler):
2424
def handle(self):
2525
response = b'<gmp_response status="200" status_text="OK"/>'
26+
27+
self.request.settimeout(0.2)
28+
try:
29+
self.request.recv(16 * 1024)
30+
except TimeoutError:
31+
pass
32+
except OSError:
33+
pass
34+
2635
self.request.sendall(response)
2736

2837

@@ -47,29 +56,33 @@ def setUp(self):
4756
self.server_thread.start()
4857

4958
def tearDown(self):
50-
self.socket_server.server_close()
5159
self.socket_server.shutdown()
5260
self.server_thread.join(60.0)
61+
self.socket_server.server_close()
5362
self.socket_path.unlink(missing_ok=True)
5463

5564
def test_unix_socket_connection_connect_read(self):
5665
connection = UnixSocketConnection(
5766
path=self.socket_name, timeout=DEFAULT_TIMEOUT
5867
)
68+
self.addCleanup(connection.disconnect)
69+
5970
connection.connect()
6071
resp = connection.read()
72+
6173
self.assertEqual(resp, b'<gmp_response status="200" status_text="OK"/>')
62-
connection.disconnect()
6374

6475
def test_unix_socket_connection_connect_send_bytes_read(self):
6576
connection = UnixSocketConnection(
6677
path=self.socket_name, timeout=DEFAULT_TIMEOUT
6778
)
79+
self.addCleanup(connection.disconnect)
80+
6881
connection.connect()
6982
connection.send(b"<gmp/>")
7083
resp = connection.read()
84+
7185
self.assertEqual(resp, b'<gmp_response status="200" status_text="OK"/>')
72-
connection.disconnect()
7386

7487
def test_unix_socket_connect_file_not_found(self):
7588
connection = UnixSocketConnection(path="foo", timeout=DEFAULT_TIMEOUT)

0 commit comments

Comments
 (0)