Ejemplo n.º 1
0
    def test_call(self):
        self._subject._sock.recv.return_value = _TEST_SENDIR_RESPONSE

        assert_that(self._subject.call(_TEST_SENDIR_REQUEST),
                    equal_to(_TEST_SENDIR_RESPONSE.decode()))

        assert_that(
            self._subject._sock.mock_calls,
            equal_to([
                call.send(b'sendir,1:1,0,36000,1,1,32,32,64,32,32,64,32,3264'),
                call.recv(1024)
            ]))
Ejemplo n.º 2
0
def test_receiving_a_message_in_chunks(message_count, chunks):
    client_thread = ClientThread(None, None, None)
    mock_conn = Mock(spec=socket.socket)
    mock_conn.recv.side_effect = create_message_generator(message_count, chunks)

    remainder = ''
    for i in range(message_count):
        message, remainder = client_thread._receive_message(mock_conn, remainder)
        assert message == 'message'

    print('remainder: %s' % remainder)
    print('rem len: %d' % len(remainder))
    assert remainder == ''
    mock_conn.assert_has_calls([call.recv(4096)] * chunks)
Ejemplo n.º 3
0
def test__send__FullMessages(sock_mock):
    # The test assume that the socket is always correctly connected when
    # fileno is queried
    sock_mock().fileno.return_value = 11

    message = 'ACommandWithParameters:#P1#P2;'
    msg_reply = 'ACommandWithParameters:done;'
    sock_mock().send.return_value = len(message)
    sock_mock().recv.return_value = msg_reply.encode()

    sock_conn = SocketConnector(host='127.0.0.3', port=10006)
    reply = sock_conn.__send__(message)

    assert reply == msg_reply
    sock_calls = [
        call.connect(('127.0.0.3', 10006)),
        call.send(message.encode()),
        # call.shutdown(socket.SHUT_WR),
        call.recv(1024),
        call.fileno(),  # This from is_connected()
        call.close()
    ]
    sock_mock().assert_has_calls(sock_calls)
Ejemplo n.º 4
0
 def test_sends_requests_receives_replies(self):
     tcp_communication(PORT, requests=[REQUEST])
     calls = [call.sendall(REQUEST), call.recv(ANY), call.close()]
     assert_equal(calls, self.conn.method_calls)
Ejemplo n.º 5
0
 def test_receives_before_sending_if_so_requested(self):
     tcp_communication(PORT, receive_first=True)
     calls = [call.recv(ANY), call.close()]
     assert_equal(calls, self.conn.method_calls)
Ejemplo n.º 6
0
 def test_sends_requests_receives_replies(self):
     tcp_communication(PORT, requests=[REQUEST])
     calls = [call.sendall(REQUEST), call.recv(ANY), call.close()]
     assert_equal(calls, self.conn.method_calls)
Ejemplo n.º 7
0
 def test_receives_before_sending_if_so_requested(self):
     tcp_communication(PORT, receive_first=True)
     calls = [call.recv(ANY), call.close()]
     assert_equal(calls, self.conn.method_calls)