def test_client_release():
    '''
    If the previous client release the connection, further connection should be accepted. This will also test the server code
    '''
    echo_server = MessageServer((localhost, echo_port))
    echo_server.start()
    client = unrealcv.Client((localhost, echo_port))

    num_release_trial = 10
    print('Try to release client %d times', num_release_trial)
    for i in range(num_release_trial):
        msg = 'Trial %d' % i
        client.connect()
        assert client.isconnected() == True, msg

        # Do something
        req = 'ok'
        res = client.request(req)
        assert req == res, msg

        client.disconnect(
        )  # Make sure the server can correctly handle the disconnection signal
        assert client.isconnected() == False, msg
        print('Trial %d is finished.' % i)
    echo_server.shutdown()
def test_random_operation():
    ''' Randomly connect and disconnect the client, this is very likely to fail '''
    echo_server = MessageServer((localhost, echo_port))
    echo_server.start()
    client = unrealcv.Client((localhost, echo_port))

    num_random_trial = 10
    print('Try random operation %d times' % num_random_trial)
    for i in range(num_random_trial):
        msg = 'Trial %d' % i
        choice = random.randrange(2)
        if choice == 1:
            client.connect()
            time.sleep(0.1)
            assert client.isconnected() == True, msg
        elif choice == 0:
            client.disconnect()
            time.sleep(0.1)
            assert client.isconnected() == False, msg

    for i in range(10):
        client.connect()
        assert client.isconnected() == True
    for i in range(10):
        client.disconnect()
        assert client.isconnected() == False
    echo_server.shutdown()
Exemplo n.º 3
0
 def setUpClass(cls):
     cls.echo_port = 9010
     # Message sent to here will be echoed back
     cls.null_port = 9011
     # Message sent to here will not get any reply
     cls.no_port = 9012
     # No server is running on this port
     cls.host = 'localhost'
     cls.echo_server = MessageServer((cls.host, cls.echo_port))
     cls.echo_server.start()
     cls.null_server = NullServer((cls.host, cls.null_port))
     cls.null_server.start()
     unrealcv.client = unrealcv.Client((cls.host, cls.echo_port))
Exemplo n.º 4
0
 def test_release(self):
     '''
     Test whether resources are correctly released
     '''
     with port_lock:
         for i in range(10):
             server = MessageServer((self.host, self.port))
             server.start()
             server.shutdown()
Exemplo n.º 5
0
def test_release():
    '''
    Test whether resources are correctly released
    '''
    for i in range(10):
        server = MessageServer((host, port))
        server.start(
        )  # Make sure the port has been released, otherwith this will fail
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        server.shutdown()
Exemplo n.º 6
0
def test_server_close():
    ''' Test whether resources are correctly released '''
    assert is_port_open(port) == True
    server = MessageServer((host, port)) # This wil bind to this endpoint
    assert is_port_open(port) == False

    for i in range(10):
        logger.debug('Run trial %d' % i)
        server.start() # Make sure the port has been released, otherwith this will fail

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        s.close() # FIXME: It is important to close all clients first, otherwise I can not shutdown the server

        logger.debug('Stop server, trial %d' % i)
        server.stop() # TODO: If server is stopped, we can not connect to it, use unrealcv client to test it!
        # time.sleep(0.1) # Wait until the connection is started
    # server.socket.close() # Explicitly release the server socket
    server.shutdown()
def test_request():
    ''' Simple test for basic functions '''
    server = MessageServer((localhost, echo_port))
    server.start()
    client = unrealcv.Client((localhost, echo_port))
    cmds = ['hi', 'hello', 'asdf' * 70]
    client.connect()
    assert client.isconnected() == True
    for cmd in cmds:
        res = client.request(cmd)
        assert res == cmd
    client.disconnect()  # TODO: What if forgot to disconnect
    server.shutdown()
def test_message_handler():
    ''' Check message handler can correctly handle events from the server.
    And the thread is correctly handled that we can do something in the message_handler '''
    echo_server = MessageServer((localhost, echo_port))
    echo_server.start()
    client = unrealcv.Client((localhost, echo_port))

    def handle_message(msg):
        print('Got server message %s' % repr(msg))
        res = unrealcv.client.request('ok', 1)
        assert res == 'ok'
        print('Server response %s' % res)

    client.connect()
    assert client.isconnected() == True
    client.message_handler = handle_message

    res = client.request('ok')
    assert res == 'ok'

    echo_server.send('Hello from server')
    time.sleep(5)
    echo_server.shutdown()
Exemplo n.º 9
0
def test_client_side_close():
    '''
    Test whether the server can correctly detect client disconnection
    '''
    server = MessageServer((host, port))
    server.start()

    for i in range(10):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))

        unrealcv.SocketMessage.WrapAndSendPayload(s, 'hello')
        s.close()  # It will take some time to notify the server
        time.sleep(
            0.5)  # How long will the server should detect the client side loss

    server.shutdown()
Exemplo n.º 10
0
def test_multi_connection():
    '''
    Only one client is allowed for the server
    Make a second connection to the server, when one connection exists
    '''
    echo_server = MessageServer((localhost, echo_port))
    echo_server.start()
    client = unrealcv.Client((localhost, echo_port))
    client.connect(timeout=0.1)
    assert client.isconnected() == True
    response = client.request('hi')
    assert response == 'hi'
    for i in range(10):
        client = unrealcv.Client((localhost, echo_port))
        client.connect(0.1)
        # print client.connect()
        assert client.isconnected() == False
    client.disconnect()
    echo_server.shutdown()
Exemplo n.º 11
0
    def test_client_side_close(self):
        '''
        Test whether the server can correctly detect client disconnection
        '''
        with port_lock:
            server = MessageServer((self.host, self.port))
            server.start()

            for i in range(10):
                _L.info('Trial %d', i)
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.host, self.port))
                # s.sendall('Hello, world')
                # data = s.recv(1024)
                # How to know whether this s is closed by remote?
                unrealcv.SocketMessage.WrapAndSendPayload(s, 'hello')
                s.close()  # It will take some time to notify the server
                time.sleep(
                    0.5
                )  # How long will the server should detect the client side loss
                # self.assertEqual(MessageTCPHandler.connected, False) # Check any client is connected
                # TODO: fix this

            server.shutdown()
Exemplo n.º 12
0
def server():
    server = MessageServer((localhost, echo_port))
    server.start() # Wait until the server is started
    return server
Exemplo n.º 13
0
 def setUpClass(cls):
     cls.echo_server = MessageServer(('localhost', 9000))
     cls.echo_server.start()
Exemplo n.º 14
0
 def test_server(self):
     with port_lock:
         server = MessageServer((self.host, self.port))
         server.start()
         server.shutdown()
Exemplo n.º 15
0
def server():
    server = MessageServer((localhost, echo_port))
    server.start()  # Wait until the server is started
    return server
Exemplo n.º 16
0
def test_server():
    server = MessageServer((host, port))
    server.start()
    server.shutdown()
Exemplo n.º 17
0
from dev_server import MessageServer
import logging
L = logging.getLogger('unrealcv')
L.setLevel(logging.DEBUG)

logging.basicConfig()
server = MessageServer(('localhost', 9000))
server.start()
while (1):
    pass