def test_connection_closed(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.return_value = [False] conn = Connection('myhost', 1234) conn.close() with pytest.raises(ConnectionClosed): conn.send('foo()')
def test_connection_send(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.return_value = [False] conn = Connection('myhost', 1234) conn._wfile.write.reset_mock() conn.send('foo()') conn._wfile.write.assert_called_once_with(b'foo()\n')
def test_connection_send_error(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.side_effect = [[False], [True]] conn = Connection('myhost', 1234, ignore_errors=False) conn._rfile.readline.return_value = b'Fail\n' with pytest.raises(ConnectionError): conn.send('foo()')
def test_connection_send_error(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.side_effect = [[False], [True]] conn = Connection('myhost', 1234) conn._rfile.readline.return_value = b'Fail\n' with pytest.raises(ConnectionError): conn.send('foo()')
def test_connection_batch_forget(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.return_value = [False] conn = Connection('myhost', 1234) conn._wfile.write.reset_mock() conn.batch_start() conn.send('foo()') conn.send('bar()') conn.send('baz()') conn.batch_forget() assert not conn._wfile.write.called
def test_connection_batch_exception(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.return_value = [False] conn = Connection('myhost', 1234) conn._wfile.write.reset_mock() try: with conn.batch_start(): conn.send('foo()') conn.send('bar()') conn.send('baz()') raise Exception('boo') except Exception: pass assert not conn._wfile.write.called
def test_connection_ignore_errors(): with mock.patch('socket.socket'), mock.patch('select.select'): select.select.side_effect = [[False], [True], [False]] conn = Connection('myhost', 1234, ignore_errors=True) conn.send('foo()') conn._socket.recv.assert_called_once_with(1500)