diff options
author | Christoph Egger <christoph@debian.org> | 2017-05-28 14:30:29 +0200 |
---|---|---|
committer | Christoph Egger <christoph@debian.org> | 2017-05-28 14:30:29 +0200 |
commit | 9f30a9497bb1e812acfdf1c98e3b5cf35ddd8e39 (patch) | |
tree | 1c84647324d5e1ddf43a46b9e3630849ddd9b334 /test_client.py | |
parent | 122bb8ba0b15478112fec10ebb3c0abf546d7fee (diff) |
New upstream version 0.0~vcs.2017.05.26.git
Diffstat (limited to 'test_client.py')
-rw-r--r-- | test_client.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/test_client.py b/test_client.py new file mode 100644 index 0000000..a75bd36 --- /dev/null +++ b/test_client.py @@ -0,0 +1,87 @@ +import enet +import random +import sys + +try: + random.seed(sys.argv[1]) +except IndexError: + pass + +SHUTDOWN_MSG = b"SHUTDOWN" +MSG_NUMBER = 10 + +host = enet.Host(None, 1, 0, 0, 0) +peer = host.connect(enet.Address(b"localhost", 54301), 1) + +counter = 0 +run = True +while run: + event = host.service(1000) + if event.type == enet.EVENT_TYPE_CONNECT: + print("%s: CONNECT" % event.peer.address) + elif event.type == enet.EVENT_TYPE_DISCONNECT: + print("%s: DISCONNECT" % event.peer.address) + run = False + continue + elif event.type == enet.EVENT_TYPE_RECEIVE: + print("%s: IN: %r" % (event.peer.address, event.packet.data)) + continue + msg = bytes(bytearray([random.randint(0,255) for i in range(40)])) + packet = enet.Packet(msg) + peer.send(0, packet) + + counter += 1 + if counter >= MSG_NUMBER: + msg = SHUTDOWN_MSG + peer.send(0, enet.Packet(msg)) + host.service(100) + peer.disconnect() + + print("%s: OUT: %r" % (peer.address, msg)) + + +# Part of the test to do with intercept callback and socket.send +peer = host.connect(enet.Address(b"localhost", 54301), 1) +shutdown_scheduled = False +run = True + +def receive_callback(address, data): + global shutdown_scheduled + + if shutdown_scheduled: + return + + if data == b"\xff\xff\xff\xffstatusResponse\n": + # if the test gets to this point, it means it has passed. Disconnect is a clean up + shutdown_scheduled = True + else: + # error messages are not propagating + # through cython + print("data != statusResponse. Instead of expected, got %r" % data) + assert(False) + +while run: + event = host.service(1000) + if event.type == enet.EVENT_TYPE_CONNECT: + print("%s: CONNECT" % event.peer.address) + msg = bytes(bytearray([random.randint(0,255) for i in range(40)])) + packet = enet.Packet(msg) + peer.send(0, packet) + + host.intercept = receive_callback + elif event.type == enet.EVENT_TYPE_DISCONNECT: + print("%s: DISCONNECT" % event.peer.address) + run = False + continue + elif event.type == enet.EVENT_TYPE_RECEIVE: + print("%s: IN: %r" % (event.peer.address, event.packet.data)) + continue + + if shutdown_scheduled: + msg = SHUTDOWN_MSG + peer.send(0, enet.Packet(msg)) + host.service(100) + peer.disconnect() + continue + + host.socket.send(peer.address, b"\xff\xff\xff\xffgetstatus\x00") |