-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_pick_winner.py
More file actions
50 lines (41 loc) · 1.38 KB
/
test_pick_winner.py
File metadata and controls
50 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
import asyncio
from p2p import peer
def test_pick_winner_ignores_round_without_eligible_validator():
async def scenario():
original_sleep = peer.asyncio.sleep
real_sleep = asyncio.sleep
try:
async def fast_sleep(_):
await real_sleep(0)
peer.asyncio.sleep = fast_sleep
peer.validators.clear()
peer.tempblocks.clear()
peer.Blockchain.clear()
queue = asyncio.Queue()
peer.tempblocks.append(
{
"Validator": "missing",
"Index": 1,
"BPM": 60,
"Timestamp": "t",
"PrevHash": "h",
"Hash": "h2",
}
)
task = asyncio.create_task(peer.pick_winner(queue))
for _ in range(100):
if peer.tempblocks == []:
break
await real_sleep(0.001)
assert not task.done()
assert queue.empty()
assert peer.tempblocks == []
task.cancel()
await asyncio.gather(task, return_exceptions=True)
finally:
peer.asyncio.sleep = original_sleep
peer.validators.clear()
peer.tempblocks.clear()
peer.Blockchain.clear()
asyncio.run(scenario())