Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test.py 6.5KB

il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
il y a 5 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import asyncio
  2. import itertools
  3. from pico import start_server, send_json, recv_json
  4. import websockets
  5. class Script:
  6. def __init__(self):
  7. self.events = []
  8. def __add__(self, event):
  9. self.events.append(('send', event))
  10. return self
  11. def __sub__(self, event):
  12. self.events.append(('recv', event))
  13. return self
  14. async def _make_client(path, timeout, script):
  15. state = {}
  16. error = False
  17. await asyncio.sleep(timeout)
  18. async with websockets.connect(path) as websocket:
  19. for kind, message in script.events:
  20. if kind == 'recv':
  21. A, B = message, await recv_json(websocket)
  22. B.pop('ts')
  23. if B['kind'] == 'login':
  24. username = B['value']
  25. state.update(message)
  26. if A != B:
  27. error = True
  28. print('-', A)
  29. print('+', B)
  30. print()
  31. elif kind == 'send':
  32. await send_json(websocket, **message)
  33. while 'online' in state:
  34. if state['online'][0] == username:
  35. break
  36. message = await recv_json(websocket)
  37. message.pop('ts')
  38. error = True
  39. print('+', message)
  40. print()
  41. state.update(message)
  42. if error:
  43. print('error')
  44. exit()
  45. async def _test(*clients):
  46. try:
  47. gather = asyncio.gather(
  48. start_server('localhost', 8642, 'TestServer'),
  49. *clients,
  50. )
  51. server, *results = await asyncio.wait_for(gather, timeout=2)
  52. server.close()
  53. except asyncio.TimeoutError:
  54. return
  55. return results
  56. def test_happy_path():
  57. client = _make_client('ws://localhost:8642/A', 0.1, Script()
  58. + {'kind': 'login', 'value': 'TestUser'}
  59. - {'kind': 'login', 'value': 'TestUser', 'online': ['TestUser']}
  60. - {'kind': 'post', 'value': 'Welcome to /A'}
  61. + {'kind': 'post', 'value': 'Hello World!'}
  62. - {'kind': 'post', 'value': 'Hello World!', 'source': 'TestUser'}
  63. )
  64. return _test(client)
  65. def test_name_taken():
  66. client1 = _make_client('ws://localhost:8642/', 0.10, Script()
  67. + {'kind': 'login', 'value': 'A'}
  68. - {'kind': 'login', 'value': 'A', 'online': ['A']}
  69. - {'kind': 'post', 'value': 'Welcome to /'}
  70. - {'kind': 'post', 'value': 'B joined', 'online': ['A', 'B']}
  71. )
  72. client2 = _make_client('ws://localhost:8642/', 0.11, Script()
  73. + {'kind': 'login', 'value': 'A'}
  74. - {'kind': 'error', 'value': 'Username taken'}
  75. )
  76. client3 = _make_client('ws://localhost:8642/', 0.12, Script()
  77. + {'kind': 'login', 'value': 'B'}
  78. - {'kind': 'login', 'value': 'B', 'online': ['A', 'B']}
  79. - {'kind': 'post', 'value': 'Welcome to /'}
  80. - {'kind': 'post', 'value': 'A left', 'online': ['B']}
  81. )
  82. return _test(client1, client2, client3)
  83. def test_interaction():
  84. client1 = _make_client('ws://localhost:8642/', 0.10, Script()
  85. + {'kind': 'login', 'value': 'Alice'}
  86. - {'kind': 'login', 'value': 'Alice', 'online': ['Alice']}
  87. - {'kind': 'post', 'value': 'Welcome to /'}
  88. - {'kind': 'post', 'value': 'Bob joined', 'online': ['Alice', 'Bob']}
  89. + {'kind': 'post', 'value': 'Hey Bob!'}
  90. - {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
  91. - {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
  92. )
  93. client2 = _make_client('ws://localhost:8642/', 0.11, Script()
  94. + {'kind': 'login', 'value': 'Bob'}
  95. - {'kind': 'login', 'value': 'Bob', 'online': ['Alice', 'Bob']}
  96. - {'kind': 'post', 'value': 'Welcome to /'}
  97. - {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
  98. + {'kind': 'post', 'value': 'Howdy!'}
  99. - {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
  100. - {'kind': 'post', 'value': 'Alice left', 'online': ['Bob']}
  101. )
  102. return _test(client1, client2)
  103. def test_rooms():
  104. client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
  105. + {'kind': 'login', 'value': 'Dandy'}
  106. - {'kind': 'login', 'value': 'Dandy', 'online': ['Dandy']}
  107. - {'kind': 'post', 'value': 'Welcome to /A'}
  108. + {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
  109. - {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
  110. )
  111. client2 = _make_client('ws://localhost:8642/B', 0.10, Script()
  112. + {'kind': 'login', 'value': 'Dandy'}
  113. - {'kind': 'login', 'value': 'Dandy', 'online': ['Dandy']}
  114. - {'kind': 'post', 'value': 'Welcome to /B'}
  115. + {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
  116. - {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
  117. )
  118. return _test(client1, client2)
  119. def test_private_message():
  120. client1 = _make_client('ws://localhost:8642/', 0.10, Script()
  121. + {'kind': 'login', 'value': 'Norman'}
  122. - {'kind': 'login', 'value': 'Norman', 'online': ['Norman']}
  123. - {'kind': 'post', 'value': 'Welcome to /'}
  124. - {'kind': 'post', 'value': 'Ray joined', 'online': ['Norman', 'Ray']}
  125. - {'kind': 'post', 'value': 'Emma joined', 'online': ['Norman', 'Ray', 'Emma']}
  126. + {'kind': 'post', 'value': 'なに?', 'target': 'Emma'}
  127. - {'kind': 'post', 'value': 'なに?', 'source': 'Norman'}
  128. )
  129. client2 = _make_client('ws://localhost:8642/', 0.11, Script()
  130. + {'kind': 'login', 'value': 'Ray'}
  131. - {'kind': 'login', 'value': 'Ray', 'online': ['Norman', 'Ray']}
  132. - {'kind': 'post', 'value': 'Welcome to /'}
  133. - {'kind': 'post', 'value': 'Emma joined', 'online': ['Norman', 'Ray', 'Emma']}
  134. - {'kind': 'post', 'value': '秘密!', 'source': 'Emma'}
  135. - {'kind': 'post', 'value': 'Norman left', 'online': ['Ray', 'Emma']}
  136. )
  137. client3 = _make_client('ws://localhost:8642/', 0.12, Script()
  138. + {'kind': 'login', 'value': 'Emma'}
  139. - {'kind': 'login', 'value': 'Emma', 'online': ['Norman', 'Ray', 'Emma']}
  140. - {'kind': 'post', 'value': 'Welcome to /'}
  141. - {'kind': 'post', 'value': 'なに?', 'source': 'Norman'}
  142. + {'kind': 'post', 'value': '秘密!', 'target': 'Ray'}
  143. - {'kind': 'post', 'value': '秘密!', 'source': 'Emma'}
  144. - {'kind': 'post', 'value': 'Norman left', 'online': ['Ray', 'Emma']}
  145. - {'kind': 'post', 'value': 'Ray left', 'online': ['Emma']}
  146. )
  147. return _test(client1, client2, client3)
  148. if __name__ == '__main__':
  149. loop = asyncio.get_event_loop()
  150. for fn_name, fn in list(locals().items()):
  151. if fn_name.startswith('test_'):
  152. loop.run_until_complete(fn())