Repository navigation
Expand file tree
/
Copy pathtest_future.py
More file actions
206 lines (180 loc) · 6.99 KB
/
Copy pathtest_future.py
File metadata and controls
206 lines (180 loc) · 6.99 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import asyncio
import os
import sys
# Assuming the structure of test_shared_methods based on common unittest methods
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
sys.path.append(root)
from ccxt import ExchangeClosedByUser
from ccxt.async_support.base.ws.future import Future
# Helper functions
async def resolve_later(future, result, delay):
await asyncio.sleep(delay)
future.resolve(result)
async def cancel_later(future, delay):
await asyncio.sleep(delay)
future.cancel()
async def reject_later(future, err, delay):
await asyncio.sleep(delay)
future.reject(err)
async def test_resolve_before():
print("test_resolve")
future = Future()
expected_result = "test"
future.resolve(expected_result)
assert future.done(), "Future is not marked as done"
assert future.result() == expected_result, f"Expected result '{expected_result}', got '{future.result()}'"
async def test_reject():
print("test_reject")
future = Future()
test_exception = Exception("test error")
future.reject(test_exception)
assert future.done(), "Future is not marked as done"
try:
future.result()
assert False, "Expected an exception but none was raised"
except Exception as e:
assert str(e) == "test error", f"Expected 'test error', got '{str(e)}'"
async def test_race_success_before():
print("test_race_success")
future1 = Future()
future2 = Future()
race_future = Future.race([future1, future2])
future1.resolve("first")
result = await race_future
future2.cancel()
assert result == "first", f"Expected 'first', got '{result}'"
async def test_race_success_after():
print("test_race_success")
future1 = Future()
future2 = Future()
race_future = Future.race([future1, future2])
asyncio.create_task(resolve_later(future1, "first", 0.01))
result = await race_future
future2.cancel()
assert result == "first", f"Expected 'first', got '{result}'"
async def test_race_return_first_exception():
print("test_race_return_first_exception")
future1 = Future()
race_future = Future.race([future1])
future1.reject(Exception("Error in future1"))
try:
await race_future
assert False, "Expected an exception but none was raised"
except Exception as e:
assert str(e) == "Error in future1", f"Expected 'Error in future1', got '{str(e)}'"
async def test_await_canceled_future():
print("test_await_canceled_future")
future = Future()
try:
future.cancel()
await future
assert False, "Expected an exception but none was raised"
except asyncio.CancelledError as e:
assert isinstance(e, asyncio.CancelledError), "Expected asyncio.CancelledError"
async def test_cancel():
print("test_cancel")
future = Future()
asyncio.create_task(cancel_later(future, 0.1))
try:
await future
assert False, "Expected an exception but none was raised"
except asyncio.CancelledError as e:
assert isinstance(e, asyncio.CancelledError), "Expected asyncio.CancelledError"
async def test_race_cancel():
print("test_race_cancel")
try:
future1 = Future()
future2 = Future()
race_future = Future.race([future1, future2])
race_future.cancel()
future1.resolve("success")
await race_future
assert False, "Expected a cancelledError"
except asyncio.CancelledError:
assert True
async def test_race_mixed_outcomes():
print("test_race_mixed_outcome")
future1 = Future()
future2 = Future()
race_future = Future.race([future1, future2])
future1.resolve("first")
task = asyncio.create_task(reject_later(future2, Exception("Error in future2"), 0.1))
result = await race_future
assert result == "first", f"Expected 'first', got '{result}'"
task.cancel()
future2.cancel()
async def test_race_with_wait_for_timeout():
print("test_race_with_wait_for_timeout")
future1 = Future()
task = asyncio.create_task(resolve_later(future1, "completed first", 2))
# Attempt to race the futures with a timeout shorter than their resolution time
try:
race_future = Future.race([future1])
await asyncio.wait_for(race_future, timeout=1) # Timeout is set deliberately short
assert False, "Expected a timeout but race_future completed"
except asyncio.TimeoutError:
# Expected outcome, the race_future should not complete within the timeout
assert True
await task
async def test_race_with_wait_for_completion():
print("test_race_with_wait_for_completion")
future1 = Future()
future2 = Future()
task = asyncio.create_task(resolve_later(future1, "completed first", 0.1))
# Race the futures with a timeout longer than necessary
try:
race_future = Future.race([future1, future2])
result = await asyncio.wait_for(race_future, timeout=1)
assert result == "completed first", f"Unexpected race result: {result}"
except asyncio.TimeoutError:
assert False, "Did not expect a timeout"
await task
async def test_race_with_precompleted_future():
print("test_race_with_precompleted_future")
future1 = Future()
future2 = Future()
future1.resolve("immediate success")
# Immediately resolved future before race call
race_future = Future.race([future1, future2])
result = await race_future
assert result == "immediate success", "Race did not correctly prioritize already completed future."
async def test_closed_by_user():
print("test_closed_by_user")
future1 = Future()
future2 = Future()
race_future = Future.race([future1, future2])
task1 = asyncio.create_task(reject_later(future1, ExchangeClosedByUser(), 0.1))
task2 = asyncio.create_task(reject_later(future2, ExchangeClosedByUser(), 0.1))
try:
await race_future
assert False, "Expected an ExchangeClosedByUser"
except ExchangeClosedByUser:
assert True
assert task1.done()
assert task2.done()
except Exception as e:
assert False, f"Received Exception {e}"
async def test_reject_with_non_exception():
print("test_reject_with_non_exception")
future = Future()
future.reject("test error")
assert future.done(), "Future is not marked as done"
try:
future.result()
assert False, "Expected an exception but none was raised"
except Exception as e:
assert str(e) == "test error", f"Expected 'test error', got '{str(e)}'"
async def test_ws_future():
await test_resolve_before()
await test_reject()
await test_race_success_before()
await test_race_return_first_exception()
await test_cancel()
await test_await_canceled_future()
await test_race_cancel()
await test_race_mixed_outcomes()
await test_race_with_wait_for_timeout()
await test_race_with_wait_for_completion()
await test_race_with_precompleted_future()
await test_closed_by_user()
await test_reject_with_non_exception()