1# -*- coding: utf-8 -*- 2from utils import session, server 3 4 5def test_text_message_of_length_1(session): 6 client, server = session 7 server.send_message_to_all('$') 8 assert client.recv() == '$' 9 10 11def test_text_message_of_length_125B(session): 12 client, server = session 13 msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 14 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 15 'abcdefghijklmnopqr125' 16 server.send_message_to_all(msg) 17 assert client.recv() == msg 18 19 20def test_text_message_of_length_126B(session): 21 client, server = session 22 msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 23 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 24 'abcdefghijklmnopqrs126' 25 server.send_message_to_all(msg) 26 assert client.recv() == msg 27 28 29def test_text_message_of_length_127B(session): 30 client, server = session 31 msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 32 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 33 'abcdefghijklmnopqrst127' 34 server.send_message_to_all(msg) 35 assert client.recv() == msg 36 37 38def test_text_message_of_length_208B(session): 39 client, server = session 40 msg = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 41 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 42 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 43 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw208' 44 server.send_message_to_all(msg) 45 assert client.recv() == msg 46 47 48def test_text_message_of_length_1251B(session): 49 client, server = session 50 msg = ('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 51 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'\ 52 'abcdefghijklmnopqr125'*10)+'1' 53 server.send_message_to_all(msg) 54 assert client.recv() == msg 55 56 57def test_text_message_of_length_68KB(session): 58 client, server = session 59 msg = '$'+('a'*67993)+'68000'+'^' 60 assert len(msg) == 68000 61 server.send_message_to_all(msg) 62 assert client.recv() == msg 63 64 65def test_text_message_of_length_1500KB(session): 66 """ An enormous message (well beyond 65K) """ 67 client, server = session 68 msg = '$'+('a'*1499991)+'1500000'+'^' 69 assert len(msg) == 1500000 70 server.send_message_to_all(msg) 71 assert client.recv() == msg 72 73 74def test_text_message_with_unicode_characters(session): 75 client, server = session 76 msg = '$äüö^' 77 server.send_message_to_all(msg) 78 assert client.recv() == msg 79 80 81def test_text_message_stress_bursts(session): 82 """ Scenario: server sends multiple different message to the same client 83 at once """ 84 from threading import Thread 85 NUM_THREADS = 100 86 MESSAGE_LEN = 1000 87 client, server = session 88 messages_received = [] 89 90 # Threads receing 91 threads_receiving = [] 92 for i in range(NUM_THREADS): 93 th = Thread( 94 target=lambda fn: messages_received.append(fn()), 95 args=(client.recv,) 96 ) 97 th.daemon = True 98 threads_receiving.append(th) 99 100 # Threads sending different characters each of them 101 threads_sending = [] 102 for i in range(NUM_THREADS): 103 message = chr(i)*MESSAGE_LEN 104 th = Thread( 105 target=server.send_message_to_all, 106 args=(message,) 107 ) 108 th.daemon = True 109 threads_sending.append(th) 110 111 # Run scenario 112 for th in threads_receiving: 113 th.start() 114 for th in threads_sending: 115 th.start() 116 117 # Wait for all threads to finish 118 print('WAITING FOR THREADS TO FINISH') 119 for th in threads_receiving: 120 th.join() 121 for th in threads_sending: 122 th.join() 123 124 for message in messages_received: 125 first_char = message[0] 126 assert message.count(first_char) == len(message) 127 128 print() 129