Skip to content

Commit e62942c

Browse files
authored
Merge pull request #591 from superannotateai/async_threads
Added tests for the async functions in thread
2 parents 189b8e0 + 9080ca1 commit e62942c

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import copy
33
import io
44
import json
5+
import logging
56
import os
67
import sys
78
import warnings
@@ -63,7 +64,10 @@
6364
from lib.core.types import Project
6465
from lib.infrastructure.utils import extract_project_folder
6566
from lib.infrastructure.validators import wrap_error
66-
import logging
67+
68+
import nest_asyncio
69+
70+
nest_asyncio.apply()
6771

6872
logger = logging.getLogger("sa")
6973

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import nest_asyncio
21
from lib.core.usecases.annotations import * # noqa: F403 F401
32
from lib.core.usecases.classes import * # noqa: F403 F401
43
from lib.core.usecases.custom_fields import * # noqa: F403 F401
@@ -8,5 +7,3 @@
87
from lib.core.usecases.items import * # noqa: F403 F401
98
from lib.core.usecases.models import * # noqa: F403 F401
109
from lib.core.usecases.projects import * # noqa: F403 F401
11-
12-
nest_asyncio.apply()

tests/unit/test_async_functions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import concurrent.futures
23
from unittest import TestCase
34

45
from superannotate import SAClient
@@ -81,10 +82,26 @@ async def gather_test():
8182

8283
def test_upload_annotations_in_running_event_loop(self):
8384
async def _test():
84-
sa.attach_items(self.PROJECT_NAME, self.ATTACH_PAYLOAD)
8585
annotations = sa.upload_annotations(
8686
self.PROJECT_NAME, annotations=self.UPLOAD_PAYLOAD
8787
)
8888
assert len(annotations["succeeded"]) == 4
8989

9090
asyncio.run(_test())
91+
92+
def test_upload_in_threads(self):
93+
def _test():
94+
annotations = sa.upload_annotations(
95+
self.PROJECT_NAME, annotations=self.UPLOAD_PAYLOAD
96+
)
97+
assert len(annotations["succeeded"]) == 4
98+
return True
99+
100+
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
101+
futures = []
102+
for i in range(8):
103+
futures.append(executor.submit(_test))
104+
results = []
105+
for f in concurrent.futures.as_completed(futures):
106+
results.append(f.result())
107+
assert all(results)

0 commit comments

Comments
 (0)