42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""One login, then several requests with the same Bearer token through the LB: each line shows the serving replica."""
|
|
import argparse
|
|
import sys
|
|
import uuid
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
|
from client import Client, check, run # noqa: E402
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument('--base-url', default=None)
|
|
p.add_argument('--requests', type=int, default=6)
|
|
args = p.parse_args()
|
|
|
|
c = Client(args.base_url)
|
|
alice = c.user('alice')
|
|
chat = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
|
body={'title': 'X-Instance-Id', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
|
print('login + chat via LB %s: one session token for user %s (token not printed)' % (c.base, alice['id']))
|
|
served = Counter()
|
|
for i in range(args.requests):
|
|
if i % 2:
|
|
r = c.message(alice, chat, 'instance check %d' % i, key='inst-' + uuid.uuid4().hex)
|
|
what = 'POST /api/v1/chats/{id}/messages'
|
|
else:
|
|
r = c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
|
check(r['json']['id'] == alice['id'], 'session resolved to another user')
|
|
what = 'GET /api/v1/users/me'
|
|
instance = r['headers']['X-Instance-Id']
|
|
served[instance] += 1
|
|
print('%d. %s -> %d X-Instance-Id=%s' % (i + 1, what, r['status'], instance))
|
|
check(len(served) >= 2, 'all requests hit one replica; LB does not balance')
|
|
print('PASS: same session served by %d replicas without re-login %s' % (len(served), dict(served)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|