-
Notifications
You must be signed in to change notification settings - Fork 629
/
test_p2p.py
executable file
·158 lines (129 loc) · 5.37 KB
/
test_p2p.py
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
#!/usr/bin/env python3
# Test script to run the p2p test. This runs three processes: the two clients,
# and the dummy signaling service.
#
# NOTE: You usually won't run this script from its original location. The
# makefiles will copy it into the same location as the tests and examples
import subprocess
import threading
import os
import sys
import copy
g_failed = False
# Thread class that runs a process and captures its output
class RunProcessInThread(threading.Thread):
def __init__( self, tag, cmdline, env, **popen_kwargs ):
threading.Thread.__init__( self, name=tag )
self.daemon = True
self.tag = tag
self.cmdline = cmdline
if env:
self.env = env
else:
self.env = dict( os.environ )
self.popen_kwargs = popen_kwargs
self.log = open( self.tag + ".log", "wt" )
def WriteLn( self, ln ):
print( "%s> %s" % (self.tag, ln ) )
self.log.write( "%s\n" % ln )
self.log.flush()
def run( self ):
# Set LD_LIBRARY_PATH
if os.name == 'posix':
LD_LIBRARY_PATH = self.env.get( 'LD_LIBRARY_PATH', '' )
if LD_LIBRARY_PATH: LD_LIBRARY_PATH += ';'
self.env['LD_LIBRARY_PATH'] = LD_LIBRARY_PATH + "."
self.WriteLn( "LD_LIBRARY_PATH = '%s'" % self.env['LD_LIBRARY_PATH'])
self.WriteLn( "Executing: " + ' '.join( self.cmdline ) )
self.process = subprocess.Popen( self.cmdline, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, env=self.env, **self.popen_kwargs )
self.process.stdin.close()
while True:
sOutput = self.process.stdout.readline()
if sOutput:
sOutput = str(sOutput, 'utf-8', 'ignore')
self.WriteLn( sOutput.rstrip() )
elif self.process.poll() is not None:
break
self.process.wait()
self.WriteLn( "Exitted with %d" % self.process.returncode )
if self.process.returncode != 0:
global g_failed
g_failed = True
# Wait for thread to shutdown. Nuke process if we don't exit in time
def join( self, timeout ):
threading.Thread.join( self, timeout )
if self.is_alive():
self.WriteLn( "Still running after %d seconds. Killing" % timeout )
global g_failed
g_failed = True
self.process.kill()
# Attempt graceful shutdown
def term( self ):
self.WriteLn( "Attempting graceful shutdown" )
self.process.terminate()
self.join( 5 )
def StartProcessInThread( tag, cmdline, env=None, **popen_kwargs ):
thread = RunProcessInThread( tag, cmdline, env, **popen_kwargs )
thread.start()
return thread
def StartClientInThread( role, local, remote ):
cmdline = [
"./test_p2p",
"--" + role,
"--identity-local", "str:"+local,
"--identity-remote", "str:"+remote,
"--signaling-server", "localhost:10000",
"--log", local + ".verbose.log"
]
env = dict( os.environ )
if os.name == 'nt' and not os.path.exists( 'steamnetworkingsockets.dll' ) and not os.path.exists( 'GameNetworkingSockets.dll' ):
bindir = os.path.abspath('../../../bin')
if not os.path.exists( bindir ):
print( "Can't find steamnetworkingsockets.dll" )
sys.exit(1)
env['PATH'] = os.path.join( bindir, 'win64' ) + ';' + os.path.join( bindir, 'win32' ) + ';' + env['PATH']
return StartProcessInThread( local, cmdline, env );
# Run a standard client/server connection-oriented case.
# where one peer is the "server" and "listens" and a "client" connects.
def ClientServerTest():
print( "Running basic socket client/server test" )
client1 = StartClientInThread( "server", "peer_server", "peer_client" )
client2 = StartClientInThread( "client", "peer_client", "peer_server" )
# Wait for clients to shutdown. Nuke them if necessary
client1.join( timeout=20 )
client2.join( timeout=20 )
def SymmetricTest():
print( "Running socket symmetric test" )
client1 = StartClientInThread( "symmetric", "alice", "bob" )
client2 = StartClientInThread( "symmetric", "bob", "alice" )
# Wait for clients to shutdown. Nuke them if necessary
client1.join( timeout=20 )
client2.join( timeout=20 )
#
# Main
#
# Start the signaling server
trivial_signaling_server = './trivial_signaling_server'
if os.name == 'nt' and not os.path.exists( 'trivial_signaling_server.exe' ):
trivial_signaling_server = '../examples/trivial_signaling_server.exe'
if not os.path.exists( trivial_signaling_server ):
print( "Can't find trivial_signaling_server.exe" )
sys.exit(1)
signaling = StartProcessInThread( "signaling", [ trivial_signaling_server ] )
# Run the tests
for test in [ ClientServerTest, SymmetricTest ]:
print( "=================================================================" )
print( "=================================================================" )
test()
print( "=================================================================" )
print( "=================================================================" )
if g_failed:
break
# Ignore any "failure" detected in signaling server shutdown.
really_failed = g_failed
# Shutdown signaling
signaling.term()
if really_failed:
print( "TEST FAILED" )
sys.exit(1)
print( "TEST SUCCEEDED" )