68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""100 parallel POSTs from 10 clients -> exactly 100 unique messages."""
|
|
import threading
|
|
from _bootstrap import TESTS_DIR # noqa: F401
|
|
|
|
from client import Client, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Student concurrency test')
|
|
c = Client(args.base_url, args.ca_file)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/' + chat['id']
|
|
|
|
workers, per_worker = 10, 10
|
|
barrier = threading.Barrier(workers)
|
|
ids_per_worker = [None] * workers
|
|
errors = [None] * workers
|
|
|
|
def worker(wid):
|
|
local = Client(args.base_url, args.ca_file, timeout=20)
|
|
barrier.wait()
|
|
collected = []
|
|
try:
|
|
for i in range(per_worker):
|
|
m = local.message(alice, chat, 'w%02d-%02d' % (wid, i))['json']
|
|
collected.append(m['id'])
|
|
except Exception as exc: # noqa: BLE001
|
|
errors[wid] = repr(exc)
|
|
ids_per_worker[wid] = collected
|
|
|
|
threads = [threading.Thread(target=worker, args=(i,)) for i in range(workers)]
|
|
for t in threads:
|
|
t.start()
|
|
for t in threads:
|
|
t.join()
|
|
|
|
failures = [e for e in errors if e]
|
|
check(not failures, 'worker errors: ' + '; '.join(failures))
|
|
|
|
all_ids = [mid for chunk in ids_per_worker for mid in chunk]
|
|
check(len(all_ids) == workers * per_worker,
|
|
'expected %d writes, got %d' % (workers * per_worker, len(all_ids)))
|
|
check(len(set(all_ids)) == len(all_ids), 'duplicate message ids returned')
|
|
|
|
# Read back the entire history
|
|
seen = set()
|
|
cursor = None
|
|
pages = 0
|
|
while True:
|
|
q = path + '/messages?limit=100'
|
|
if cursor:
|
|
q += '&cursor=' + cursor
|
|
page = c.call('GET', q, actor=bob, schema='MessagePage')['json']
|
|
for item in page['items']:
|
|
seen.add(item['id'])
|
|
pages += 1
|
|
if page['next_cursor'] is None or pages > 50:
|
|
break
|
|
cursor = page['next_cursor']
|
|
|
|
missing = [mid for mid in all_ids if mid not in seen]
|
|
check(not missing, 'history lost %d messages' % len(missing))
|
|
print('PASS: 100 messages from 10 clients, all unique and readable')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main) |