From e14c94121b426910e5df5939dbcc636c605376ef Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Wed, 24 Jul 2019 02:11:30 -0400 Subject: [PATCH] Makes concurrency test into server_test --- requirements.txt | 10 ------ tests/concurrent_write_test.py | 29 ---------------- tests/server_test.py | 61 ++++++++++++++++++++++++---------- 3 files changed, 43 insertions(+), 57 deletions(-) delete mode 100644 requirements.txt delete mode 100644 tests/concurrent_write_test.py diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 1290854..0000000 --- a/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -Faker>=1.0.7 -asyncio>=3.4.3 -aiohttp==3.5.4 -aiojobs==0.2.2 -ecdsa==0.13 -cryptography==2.5 -aiosqlite==0.10.0 -PyNaCl>=1.3.0 -requests -cython diff --git a/tests/concurrent_write_test.py b/tests/concurrent_write_test.py deleted file mode 100644 index ef631ab..0000000 --- a/tests/concurrent_write_test.py +++ /dev/null @@ -1,29 +0,0 @@ -from multiprocessing import Pool -import json -import requests - - -def make_comment(num): - return{ - 'jsonrpc': '2.0', - 'id': None, - 'method': 'create_comment', - 'params': { - 'comment': f'Comment #{num}', - 'claim_id': '6d266af6c25c80fa2ac6cc7662921ad2e90a07e7', - } - } - - -def send_comment_to_server(params): - with requests.post(params[0], json=params[1]) as req: - return req.json() - - -if __name__ == '__main__': - urls = [f'http://localhost:{port}/api' for port in range(5921, 5925)] - comments = [make_comment(i) for i in range(1, 5)] - inputs = list(zip(urls, comments)) - print(json.dumps(inputs, indent=2)) - with Pool(4) as pool: - print(json.dumps(pool.map(send_comment_to_server, inputs), indent=2)) diff --git a/tests/server_test.py b/tests/server_test.py index b50946d..1a5ab0e 100644 --- a/tests/server_test.py +++ b/tests/server_test.py @@ -1,4 +1,6 @@ import unittest +from multiprocessing.pool import Pool + import requests import re from itertools import * @@ -34,6 +36,22 @@ def nothing(): pass +replace = { + 'claim_id': fake.sha1, + 'comment': fake.text, + 'channel_id': fake.sha1, + 'channel_name': fake_lbryusername, + 'signature': fake.uuid4, + 'parent_id': fake.sha256 +} + + +def create_test_comments(values: iter, **default): + vars_combo = chain.from_iterable(combinations(values, r) for r in range(1, len(values) + 1)) + return [{k: replace[k]() if k in comb else v for k, v in default.items()} + for comb in vars_combo] + + class ServerTest(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -216,23 +234,30 @@ class ListCommentsTest(unittest.TestCase): self.assertEqual(response['total_pages'], response_one['total_pages']) +class ConcurrentWriteTest(unittest.TestCase): + @staticmethod + def make_comment(num): + return { + 'jsonrpc': '2.0', + 'id': num, + 'method': 'create_comment', + 'params': { + 'comment': f'Comment #{num}', + 'claim_id': '6d266af6c25c80fa2ac6cc7662921ad2e90a07e7', + } + } + @staticmethod + def send_comment_to_server(params): + with requests.post(params[0], json=params[1]) as req: + return req.json() - -replace = { - 'claim_id': fake.sha1, - 'comment': fake.text, - 'channel_id': fake.sha1, - 'channel_name': fake_lbryusername, - 'signature': fake.uuid4, - 'parent_id': fake.sha256 -} - - -def create_test_comments(values: iter, **default): - vars_combo = chain.from_iterable(combinations(values, r) for r in range(1, len(values) + 1)) - return [{k: replace[k]() if k in comb else v for k, v in default.items()} - for comb in vars_combo] - - - + def test01Concurrency(self): + urls = [f'http://localhost:{port}/api' for port in range(5921, 5925)] + comments = [self.make_comment(i) for i in range(1, 5)] + inputs = list(zip(urls, comments)) + with Pool(4) as pool: + results = pool.map(self.send_comment_to_server, inputs) + results = list(filter(lambda x: 'comment_id' in x['result'], results)) + self.assertIsNotNone(results) + self.assertEqual(len(results), len(inputs))