@@ -59,15 +59,11 @@ def test_send(self):
59
59
'payload' : 'test data'
60
60
}
61
61
62
- def mock_reinit (obj ):
63
- obj ._sock = mock .MagicMock ()
64
- obj ._sock .sendall .return_value = None
65
- obj ._dirty = False
66
-
67
- with mock .patch .object (KafkaConnection , 'reinit' , new = mock_reinit ):
68
- conn = KafkaConnection (fake_config ['host' ], fake_config ['port' ])
69
- conn .send (fake_config ['request_id' ], fake_config ['payload' ])
70
- conn ._sock .sendall .assert_called_with (fake_config ['payload' ])
62
+ assert socket .create_connection is self .MockCreateConn
63
+ conn = KafkaConnection (fake_config ['host' ], fake_config ['port' ])
64
+ socket .create_connection .reset_mock ()
65
+ conn .send (fake_config ['request_id' ], fake_config ['payload' ])
66
+ conn ._sock .sendall .assert_called_with (fake_config ['payload' ])
71
67
72
68
def test_init_creates_socket_connection (self ):
73
69
fake_config = {
@@ -76,7 +72,8 @@ def test_init_creates_socket_connection(self):
76
72
}
77
73
78
74
assert socket .create_connection is self .MockCreateConn
79
- conn = KafkaConnection (fake_config ['host' ], fake_config ['port' ])
75
+ socket .create_connection .reset_mock ()
76
+ KafkaConnection (fake_config ['host' ], fake_config ['port' ])
80
77
socket .create_connection .assert_called_with ((fake_config ['host' ], fake_config ['port' ]), DEFAULT_SOCKET_TIMEOUT_SECONDS )
81
78
82
79
def test_init_failure_raises_connection_error (self ):
@@ -88,9 +85,10 @@ def test_init_failure_raises_connection_error(self):
88
85
def raise_error (* args ):
89
86
raise socket .error
90
87
91
- with mock .patch .object (socket , 'create_connection' , new = raise_error ):
92
- with self .assertRaises (ConnectionError ):
93
- KafkaConnection (fake_config ['host' ], fake_config ['port' ])
88
+ assert socket .create_connection is self .MockCreateConn
89
+ socket .create_connection .side_effect = raise_error
90
+ with self .assertRaises (ConnectionError ):
91
+ KafkaConnection (fake_config ['host' ], fake_config ['port' ])
94
92
95
93
def test_send__reconnects_on_dirty_conn (self ):
96
94
fake_config = {
@@ -105,7 +103,6 @@ def test_send__reconnects_on_dirty_conn(self):
105
103
conn = KafkaConnection (fake_config ['host' ], fake_config ['port' ])
106
104
107
105
# Dirty it
108
-
109
106
try :
110
107
conn ._raise_connection_error ()
111
108
except ConnectionError :
0 commit comments