예제 #1
0
def test_socketcomm_close():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm.close()
    comm._conn.shutdown.assert_called_with()
    comm._conn.close.assert_called_with()
예제 #2
0
def test_socketcomm_address():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.getpeername.return_value = "127.0.0.1", 1234

    eq_(comm.address, ("127.0.0.1", 1234))
    comm._conn.getpeername.assert_called_with()
예제 #3
0
def test_serialcomm_read_raw_timeout():
    with pytest.raises(IOError):
        comm = SocketCommunicator(socket.socket())
        comm._conn = mock.MagicMock()
        comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b""])

        _ = comm.read_raw(-1)
예제 #4
0
def test_socketcomm_address():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.getpeername.return_value = "127.0.0.1", 1234

    assert comm.address == ("127.0.0.1", 1234)
    comm._conn.getpeername.assert_called_with()
예제 #5
0
def test_socketcomm_close():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm.close()
    comm._conn.shutdown.assert_called_with(socket.SHUT_RDWR)
    comm._conn.close.assert_called_with()
예제 #6
0
def test_socketcomm_flush_input():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm.read = mock.MagicMock()
    comm.flush_input()

    comm.read.assert_called_with(-1)
예제 #7
0
def test_serialcomm_read_raw_timeout():
    with pytest.raises(IOError):
        comm = SocketCommunicator(socket.socket())
        comm._conn = mock.MagicMock()
        comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b""])

        _ = comm.read_raw(-1)
예제 #8
0
def test_loopbackcomm_read_raw_2char_terminator():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\r", b"\n"])
    comm._terminator = "\r\n"

    eq_(comm.read_raw(), b"abc")
    comm._conn.recv.assert_has_calls([mock.call(1)] * 5)
    assert comm._conn.recv.call_count == 5
예제 #9
0
def test_socketcomm_read_raw():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\n"])

    eq_(comm.read_raw(), b"abc")
    comm._conn.recv.assert_has_calls([mock.call(1)]*4)
    assert comm._conn.recv.call_count == 4

    comm._conn.recv = mock.MagicMock()
    comm.read_raw(10)
    comm._conn.recv.assert_called_with(10)
예제 #10
0
def test_socketcomm_timeout():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.gettimeout.return_value = 1.234

    unit_eq(comm.timeout, 1.234 * pq.second)
    comm._conn.gettimeout.assert_called_with()

    comm.timeout = 10
    comm._conn.settimeout.assert_called_with(10)

    comm.timeout = 1000 * pq.millisecond
    comm._conn.settimeout.assert_called_with(1)
예제 #11
0
def test_socketcomm_timeout():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.gettimeout.return_value = 1.234

    unit_eq(comm.timeout, 1.234 * pq.second)
    comm._conn.gettimeout.assert_called_with()

    comm.timeout = 10
    comm._conn.settimeout.assert_called_with(10)

    comm.timeout = 1000 * pq.millisecond
    comm._conn.settimeout.assert_called_with(1)
예제 #12
0
def test_socketcomm_terminator():
    comm = SocketCommunicator(socket.socket())

    # Default terminator should be \n
    eq_(comm.terminator, "\n")

    comm.terminator = b"*"
    eq_(comm.terminator, "*")
    eq_(comm._terminator, "*")

    comm.terminator = u"\r"
    eq_(comm.terminator, u"\r")
    eq_(comm._terminator, u"\r")

    comm.terminator = "\r\n"
    eq_(comm.terminator, "\r\n")
    eq_(comm._terminator, "\r\n")
예제 #13
0
def test_socketcomm_terminator():
    comm = SocketCommunicator(socket.socket())

    # Default terminator should be \n
    eq_(comm.terminator, "\n")

    comm.terminator = b"*"
    eq_(comm.terminator, "*")
    eq_(comm._terminator, "*")

    comm.terminator = u"\r"  # pylint: disable=redefined-variable-type
    eq_(comm.terminator, u"\r")
    eq_(comm._terminator, u"\r")

    comm.terminator = "\r\n"
    eq_(comm.terminator, "\r\n")
    eq_(comm._terminator, "\r\n")
예제 #14
0
def test_socketcomm_terminator():
    comm = SocketCommunicator(socket.socket())

    # Default terminator should be \n
    assert comm.terminator == "\n"

    comm.terminator = b"*"
    assert comm.terminator == "*"
    assert comm._terminator == "*"

    comm.terminator = u"\r"
    assert comm.terminator == u"\r"
    assert comm._terminator == u"\r"

    comm.terminator = "\r\n"
    assert comm.terminator == "\r\n"
    assert comm._terminator == "\r\n"
예제 #15
0
def test_socketcomm_terminator():
    comm = SocketCommunicator(socket.socket())

    # Default terminator should be \n
    assert comm.terminator == "\n"

    comm.terminator = b"*"
    assert comm.terminator == "*"
    assert comm._terminator == "*"

    comm.terminator = u"\r"
    assert comm.terminator == u"\r"
    assert comm._terminator == u"\r"

    comm.terminator = "\r\n"
    assert comm.terminator == "\r\n"
    assert comm._terminator == "\r\n"
예제 #16
0
def test_socketcomm_query():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm.read = mock.MagicMock(return_value="answer")
    comm.sendcmd = mock.MagicMock()

    assert comm._query("mock") == "answer"
    comm.sendcmd.assert_called_with("mock")
    comm.read.assert_called_with(-1)

    comm._query("mock", size=10)
    comm.read.assert_called_with(10)
예제 #17
0
def test_socketcomm_flush_input():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm.read = mock.MagicMock()
    comm.flush_input()

    comm.read.assert_called_with(-1)
예제 #18
0
def test_socketcomm_query():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm.read = mock.MagicMock(return_value="answer")
    comm.sendcmd = mock.MagicMock()

    eq_(comm._query("mock"), "answer")
    comm.sendcmd.assert_called_with("mock")
    comm.read.assert_called_with(-1)

    comm._query("mock", size=10)
    comm.read.assert_called_with(10)
예제 #19
0
    def open_tcpip(cls, host, port):
        """
        Opens an instrument, connecting via TCP/IP to a given host and TCP port.

        :param str host: Name or IP address of the instrument.
        :param int port: TCP port on which the insturment is listening.

        :rtype: `Instrument`
        :return: Object representing the connected instrument.

        .. seealso::
            `~socket.socket.connect` for description of `host` and `port`
            parameters in the TCP/IP address family.
        """
        conn = socket.socket()
        conn.connect((host, port))
        return cls(SocketCommunicator(conn))
예제 #20
0
def test_socketcomm_read_raw():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()
    comm._conn.recv = mock.MagicMock(side_effect=[b"a", b"b", b"c", b"\n"])

    assert comm.read_raw() == b"abc"
    comm._conn.recv.assert_has_calls([mock.call(1)] * 4)
    assert comm._conn.recv.call_count == 4

    comm._conn.recv = mock.MagicMock()
    comm.read_raw(10)
    comm._conn.recv.assert_called_with(10)
예제 #21
0
def test_socketcomm_write_raw():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm.write_raw(b"mock")
    comm._conn.sendall.assert_called_with(b"mock")
예제 #22
0
def test_socketcomm_address_setting():
    with pytest.raises(NotImplementedError):
        comm = SocketCommunicator(socket.socket())
        comm.address = "foobar"
예제 #23
0
def test_socketcomm_init_wrong_filelike():
    _ = SocketCommunicator("derp")
예제 #24
0
def test_socketcomm_init_wrong_filelike():
    with pytest.raises(TypeError):
        _ = SocketCommunicator("derp")
예제 #25
0
def test_socketcomm_sendcmd():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm._sendcmd("mock")
    comm._conn.sendall.assert_called_with(b"mock\n")
예제 #26
0
def test_socketcomm_address_setting():
    comm = SocketCommunicator(socket.socket())
    comm.address = "foobar"
예제 #27
0
def test_socketcomm_init():
    socket_object = socket.socket()
    comm = SocketCommunicator(socket_object)
    assert isinstance(comm._conn, socket.socket) is True
    assert comm._conn == socket_object
예제 #28
0
def test_socketcomm_tell():
    with pytest.raises(NotImplementedError):
        comm = SocketCommunicator(socket.socket())
        comm.tell()
예제 #29
0
def test_socketcomm_tell():
    with pytest.raises(NotImplementedError):
        comm = SocketCommunicator(socket.socket())
        comm.tell()
예제 #30
0
def test_socketcomm_tell():
    comm = SocketCommunicator(socket.socket())
    comm.tell()
예제 #31
0
def test_socketcomm_address_setting():
    with pytest.raises(NotImplementedError):
        comm = SocketCommunicator(socket.socket())
        comm.address = "foobar"
예제 #32
0
def test_socketcomm_write_raw():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm.write_raw(b"mock")
    comm._conn.sendall.assert_called_with(b"mock")
예제 #33
0
def test_socketcomm_sendcmd():
    comm = SocketCommunicator(socket.socket())
    comm._conn = mock.MagicMock()

    comm._sendcmd("mock")
    comm._conn.sendall.assert_called_with(b"mock\n")
예제 #34
0
def test_socketcomm_seek():
    comm = SocketCommunicator(socket.socket())
    comm.seek(1)