Skip to content

Commit 41d2aab

Browse files
committed
refactor SimpleOp
1 parent 499fe42 commit 41d2aab

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

test/asynchronous/test_mongos_load_balancing.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,34 @@
3333

3434
_IS_SYNC = False
3535

36+
if _IS_SYNC:
37+
PARENT = threading.Thread
38+
else:
39+
PARENT = object
3640

37-
if not _IS_SYNC:
3841

39-
class SimpleOp:
40-
def __init__(self, client):
41-
self.task = None
42-
self.client = client
43-
self.passed = False
42+
class SimpleOp(PARENT):
43+
def __init__(self, client):
44+
super().__init__()
45+
self.client = client
46+
self.passed = False
47+
self.task = None
4448

45-
async def run(self):
46-
await self.client.db.command("ping")
47-
self.passed = True # No exception raised.
49+
async def run(self):
50+
await self.client.db.command("ping")
51+
self.passed = True # No exception raised.
4852

49-
def start(self):
53+
def start(self):
54+
if _IS_SYNC:
55+
super().start()
56+
else:
5057
self.task = asyncio.create_task(self.run())
5158

52-
async def join(self):
59+
async def join(self):
60+
if _IS_SYNC:
61+
super().join()
62+
else:
5363
await self.task
54-
else:
55-
56-
class SimpleOp(threading.Thread):
57-
def __init__(self, client):
58-
super().__init__()
59-
self.client = client
60-
self.passed = False
61-
62-
def run(self):
63-
self.client.db.command("ping")
64-
self.passed = True # No exception raised.
6564

6665

6766
async def do_simple_op(client, ntasks):

test/test_mongos_load_balancing.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,34 @@
3333

3434
_IS_SYNC = True
3535

36+
if _IS_SYNC:
37+
PARENT = threading.Thread
38+
else:
39+
PARENT = object
3640

37-
if not _IS_SYNC:
3841

39-
class SimpleOp:
40-
def __init__(self, client):
41-
self.task = None
42-
self.client = client
43-
self.passed = False
42+
class SimpleOp(PARENT):
43+
def __init__(self, client):
44+
super().__init__()
45+
self.client = client
46+
self.passed = False
47+
self.task = None
4448

45-
def run(self):
46-
self.client.db.command("ping")
47-
self.passed = True # No exception raised.
49+
def run(self):
50+
self.client.db.command("ping")
51+
self.passed = True # No exception raised.
4852

49-
def start(self):
53+
def start(self):
54+
if _IS_SYNC:
55+
super().start()
56+
else:
5057
self.task = asyncio.create_task(self.run())
5158

52-
def join(self):
59+
def join(self):
60+
if _IS_SYNC:
61+
super().join()
62+
else:
5363
self.task
54-
else:
55-
56-
class SimpleOp(threading.Thread):
57-
def __init__(self, client):
58-
super().__init__()
59-
self.client = client
60-
self.passed = False
61-
62-
def run(self):
63-
self.client.db.command("ping")
64-
self.passed = True # No exception raised.
6564

6665

6766
def do_simple_op(client, ntasks):

0 commit comments

Comments
 (0)