forked from ChipaDevTeam/PocketOptionAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_persistent_connection.py
More file actions
278 lines (224 loc) Β· 10.8 KB
/
test_persistent_connection.py
File metadata and controls
278 lines (224 loc) Β· 10.8 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
Test script for persistent connection with keep-alive functionality
Demonstrates the enhanced connection management based on old API patterns
"""
import asyncio
import os
from loguru import logger
from pocketoptionapi_async import AsyncPocketOptionClient
async def test_persistent_connection():
"""Test persistent connection with automatic keep-alive"""
print("Testing Persistent Connection with Keep-Alive")
print("=" * 60)
print("This test demonstrates the enhanced connection management")
print("based on the old API's proven keep-alive patterns:")
print(" Automatic ping every 20 seconds")
print(" Automatic reconnection on disconnection")
print(" Multiple region fallback")
print(" Background task management")
print(" Connection health monitoring")
print("=" * 60)
print()
# Complete SSID format
complete_ssid = os.getenv(
"POCKET_OPTION_SSID",
r'42["auth",{"session":"n1p5ah5u8t9438rbunpgrq0hlq","isDemo":1,"uid":0,"platform":1}]',
)
if "n1p5ah5u8t9438rbunpgrq0hlq" in complete_ssid:
print(
" Using test SSID - connection will fail but demonstrates the keep-alive logic"
)
print(
" For real testing, set: export POCKET_OPTION_SSID='your_complete_ssid'"
)
print()
# Test 1: Regular connection (existing behavior)
print("Test 1: Regular Connection (with basic keep-alive)")
print("-" * 50)
try:
client_regular = AsyncPocketOptionClient(
ssid=complete_ssid,
is_demo=True,
persistent_connection=False, # Regular connection
auto_reconnect=True, # But with auto-reconnect
)
print("π Connecting with regular mode...")
success = await client_regular.connect()
if success:
print(" Regular connection established")
# Monitor for 30 seconds
print("π Monitoring regular connection for 30 seconds...")
for i in range(30):
await asyncio.sleep(1)
if i % 10 == 0:
stats = client_regular.get_connection_stats()
print(
f" Stats: Connected={client_regular.is_connected}, "
f"Pings sent={stats.get('messages_sent', 0)}, "
f"Reconnects={stats.get('total_reconnects', 0)}"
)
else:
print(" Regular connection failed (expected with test SSID)")
await client_regular.disconnect()
print(" Regular connection test completed")
except Exception as e:
print(f" Regular connection error (expected): {str(e)[:100]}...")
print()
# Test 2: Persistent connection (new enhanced behavior)
print("Test 2: Persistent Connection (enhanced keep-alive)")
print("-" * 50)
try:
client_persistent = AsyncPocketOptionClient(
ssid=complete_ssid,
is_demo=True,
persistent_connection=True, # Enhanced persistent mode
auto_reconnect=True,
)
# Add event handlers to monitor keep-alive events
connection_events = []
def on_connected(data):
connection_events.append(f"Connected: {data}")
print(f"π Event: Connected to {data}")
def on_reconnected(data):
connection_events.append(f"Reconnected: {data}")
print(f"Event: Reconnected after {data}")
def on_authenticated(data):
connection_events.append(f"Authenticated: {data}")
print(" Event: Authenticated")
client_persistent.add_event_callback("connected", on_connected)
client_persistent.add_event_callback("reconnected", on_reconnected)
client_persistent.add_event_callback("authenticated", on_authenticated)
print("π Connecting with persistent mode...")
success = await client_persistent.connect()
if success:
print(" Persistent connection established with keep-alive active")
# Monitor for 60 seconds to see keep-alive in action
print("π Monitoring persistent connection for 60 seconds...")
print(" (Watch for automatic pings every 20 seconds)")
for i in range(60):
await asyncio.sleep(1)
# Print stats every 15 seconds
if i % 15 == 0 and i > 0:
stats = client_persistent.get_connection_stats()
print(
f" Stats: Connected={client_persistent.is_connected}, "
f"Pings={stats.get('last_ping_time')}, "
f"Messages sent={stats.get('messages_sent', 0)}, "
f"Messages received={stats.get('messages_received', 0)}, "
f"Reconnects={stats.get('total_reconnects', 0)}, "
f"Uptime={stats.get('uptime', 'N/A')}"
)
# Send test message every 30 seconds
if i % 30 == 0 and i > 0:
print(" π€ Sending test message...")
await client_persistent.send_message('42["test"]')
# Show final statistics
final_stats = client_persistent.get_connection_stats()
print("\nπ Final Connection Statistics:")
print(f" Total connections: {final_stats.get('total_connections', 0)}")
print(
f" Successful connections: {final_stats.get('successful_connections', 0)}"
)
print(f" Total reconnects: {final_stats.get('total_reconnects', 0)}")
print(f" Messages sent: {final_stats.get('messages_sent', 0)}")
print(f" Messages received: {final_stats.get('messages_received', 0)}")
print(f" Connection uptime: {final_stats.get('uptime', 'N/A')}")
print(f" Last ping: {final_stats.get('last_ping_time', 'None')}")
print(f" Available regions: {final_stats.get('available_regions', 0)}")
print(f"\nπ Connection Events ({len(connection_events)} total):")
for event in connection_events[-5:]: # Show last 5 events
print(f" β’ {event}")
else:
print(" Persistent connection failed (expected with test SSID)")
await client_persistent.disconnect()
print(" Persistent connection test completed")
except Exception as e:
print(f" Persistent connection error (expected): {str(e)[:100]}...")
print()
# Test 3: Connection resilience simulation
print("Test 3: Connection Resilience Simulation")
print("-" * 50)
print("This would test automatic reconnection when connection drops")
print("(Requires real SSID for full testing)")
real_ssid = os.getenv("POCKET_OPTION_SSID")
if real_ssid and "n1p5ah5u8t9438rbunpgrq0hlq" not in real_ssid:
print("π Real SSID detected, testing with actual connection...")
try:
resilience_client = AsyncPocketOptionClient(
ssid=real_ssid,
is_demo=True,
persistent_connection=True,
auto_reconnect=True,
)
print("π Establishing resilient connection...")
success = await resilience_client.connect()
if success:
print(" Resilient connection established")
# Monitor for 2 minutes
print("π Monitoring resilient connection for 2 minutes...")
for i in range(120):
await asyncio.sleep(1)
if i % 30 == 0:
stats = resilience_client.get_connection_stats()
print(
f" Stats: Connected={resilience_client.is_connected}, "
f"Uptime={stats.get('uptime', 'N/A')}"
)
# Try to get balance to test API functionality
try:
balance = await resilience_client.get_balance()
print(f" Balance: ${balance.balance:.2f}")
except Exception as e:
print(f" Balance check failed: {e}")
await resilience_client.disconnect()
print(" Resilience test completed")
else:
print("Resilient connection failed")
except Exception as e:
print(f"Resilience test error: {e}")
else:
print(" Skipping resilience test (requires real SSID)")
print()
print("π All persistent connection tests completed!")
print()
print("π Summary of Enhanced Features:")
print(" Persistent connections with automatic keep-alive")
print(" Automatic reconnection with multiple region fallback")
print(" Background ping/pong handling (20-second intervals)")
print(" Connection health monitoring and statistics")
print(" Event-driven connection management")
print(" Graceful connection cleanup and resource management")
print()
print("π‘ Usage Tips:")
print("β’ Use persistent_connection=True for long-running applications")
print("β’ Set auto_reconnect=True for automatic recovery from disconnections")
print("β’ Monitor connection statistics with get_connection_stats()")
print("β’ Add event callbacks to handle connection events")
async def test_comparison_with_old_api():
"""Compare new API behavior with old API patterns"""
print("\nπ Comparison with Old API Patterns")
print("=" * 50)
print("Old API Features β New Async API Implementation:")
print("β’ daemon threads β asyncio background tasks")
print("β’ ping every 20s β async ping loop with '42[\"ps\"]'")
print("β’ auto reconnect β enhanced reconnection monitor")
print("β’ global_value tracking β connection statistics")
print("β’ websocket.run_forever() β persistent connection manager")
print("β’ manual error handling β automatic exception recovery")
print("β’ blocking operations β non-blocking async operations")
print()
print("Enhanced Features in New API:")
print("β¨ Type safety with Pydantic models")
print("β¨ Comprehensive error monitoring and health checks")
print("β¨ Event-driven architecture with callbacks")
print("β¨ Connection pooling and performance optimization")
print("β¨ Graceful shutdown and resource cleanup")
print("β¨ Modern async/await patterns")
print("β¨ Built-in rate limiting and message batching")
print("β¨ pandas DataFrame integration")
print("β¨ Rich logging and debugging information")
if __name__ == "__main__":
logger.info("Testing Enhanced Persistent Connection Functionality")
# Run tests
asyncio.run(test_persistent_connection())
asyncio.run(test_comparison_with_old_api())