Skip to content

Commit ad66966

Browse files
committed
add test for multi process sends methods
1 parent a09a025 commit ad66966

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

convex_api/convex_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def send(self, transaction, account, language=None):
5151

5252
hash_data = self._transaction_prepare(account.address, transaction, language)
5353
signed_data = account.sign(hash_data['hash'])
54-
result = self._transaction_submit(account.address, hash_data['hash'], signed_data)
54+
result = self._transaction_submit(account.address, hash_data['sequence_number'], hash_data['hash'], signed_data)
5555
return result
5656

5757
def query(self, transaction, address_account, language=None):
@@ -205,13 +205,14 @@ def _transaction_prepare(self, address, transaction, language=None):
205205

206206
return result
207207

208-
def _transaction_submit(self, address, hash_data, signed_data):
208+
def _transaction_submit(self, address, sequence_number, hash_data, signed_data):
209209
"""
210210
211211
"""
212212
submit_url = urljoin(self._url, '/api/v1/transaction/submit')
213213
data = {
214214
'address': remove_0x_prefix(address),
215+
'sequence_number': sequence_number,
215216
'hash': hash_data,
216217
'sig': remove_0x_prefix(signed_data)
217218
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
3+
Test Convex multi thread
4+
5+
"""
6+
import pytest
7+
import secrets
8+
from multiprocessing import Process
9+
10+
from convex_api.convex_api import ConvexAPI
11+
from convex_api.exceptions import ConvexAPIError
12+
13+
from tests.helpers import auto_topup_account
14+
15+
def process_on_convex(convex, test_account):
16+
values = []
17+
inc_values = []
18+
is_sent = False
19+
for index in range(secrets.randbelow(10) + 1):
20+
value = secrets.randbelow(1000)
21+
values.append(str(value))
22+
inc_values.append(value + 1)
23+
value_text = " ".join(values)
24+
while (not is_sent):
25+
try:
26+
result = convex.send(f'(map inc [{value_text}])', test_account)
27+
except ConvexAPIError as error:
28+
print('retrying again...')
29+
is_sent = False
30+
else:
31+
is_sent = True
32+
assert 'id' in result
33+
assert 'value' in result
34+
assert(result['value'] == inc_values)
35+
36+
37+
def test_convex_api_multi_thread(convex_url, test_account):
38+
39+
process_count = 4
40+
convex = ConvexAPI(convex_url)
41+
auto_topup_account(convex, test_account)
42+
process_list = []
43+
for index in range(process_count):
44+
proc = Process(target=process_on_convex, args=(convex, test_account))
45+
proc.start()
46+
process_list.append(proc)
47+
48+
for proc in process_list:
49+
proc.join()

0 commit comments

Comments
 (0)