예제 #1
0
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()')
예제 #2
0
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')
예제 #3
0
def test_connection_batch_start_fail():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [False]
        conn = Connection('myhost', 1234)
        with pytest.raises(BatchStarted):
            conn.batch_start()
            conn.batch_start()
예제 #4
0
def test_connection_batch_start_fail():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [False]
        conn = Connection('myhost', 1234)
        with pytest.raises(BatchStarted):
            conn.batch_start()
            conn.batch_start()
예제 #5
0
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')
예제 #6
0
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()')
예제 #7
0
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()')
예제 #8
0
def test_connection_transact():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.side_effect = [[False], [True]]
        conn = Connection('myhost', 1234)
        conn._wfile.write.reset_mock()
        conn._rfile.readline.return_value = b'bar\n'
        result = conn.transact('foo()')
        conn._wfile.write.assert_called_once_with(b'foo()\n')
        assert result == 'bar'
예제 #9
0
def test_connection_close():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [False]
        conn = Connection('myhost', 1234)
        s = conn._socket
        conn.close()
        # Repeated attempts shouldn't fail
        conn.close()
        s.close.assert_called_once_with()
예제 #10
0
def test_connection_transact():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.side_effect = [[False], [True]]
        conn = Connection('myhost', 1234, ignore_errors=False)
        conn._wfile.write.reset_mock()
        conn._rfile.readline.return_value = b'bar\n'
        result = conn.transact('foo()')
        conn._wfile.write.assert_called_once_with(b'foo()\n')
        assert result == 'bar'
예제 #11
0
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
예제 #12
0
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
예제 #13
0
def test_connection_init_unknown():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [True]
        mock_sock = socket.socket()
        mock_file = mock_sock.makefile()
        mock_file.readline.return_value = b'bar\n'
        with pytest.raises(CommandError):
            conn = Connection('myhost', 1234)
예제 #14
0
def test_connection_init_juice():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [True]
        mock_sock = socket.socket()
        mock_file = mock_sock.makefile()
        mock_file.readline.return_value = b'Fail\n'
        conn = Connection('myhost', 1234)
        conn._socket.connect.assert_called_once_with(('myhost', 1234))
        assert conn.server_version == 'raspberry-juice'
예제 #15
0
def test_connection_close():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [False]
        conn = Connection('myhost', 1234)
        s = conn._socket
        conn.close()
        # Repeated attempts shouldn't fail
        conn.close()
        s.close.assert_called_once_with()
예제 #16
0
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
예제 #17
0
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
예제 #18
0
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)
예제 #19
0
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)
예제 #20
0
def test_connection_init_pi():
    with mock.patch('socket.socket'), mock.patch('select.select'):
        select.select.return_value = [False]
        conn = Connection('myhost', 1234)
        conn._socket.connect.assert_called_once_with(('myhost', 1234))
        assert conn.server_version == 'minecraft-pi'