-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtestservices.py
More file actions
55 lines (47 loc) · 1.74 KB
/
Copy pathtestservices.py
File metadata and controls
55 lines (47 loc) · 1.74 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
"""Servers that library examples expect to find on this machine, run for as
long as the tests do."""
import shutil, socketserver, subprocess, threading
# TestSerialPackager_TCPIP (Modelica_DeviceDrivers) and ExampleClientLoop
# (AixLib) are clients of the echo server their libraries ship as Windows code.
TCP_ECHO_PORT = 27015
# TestSerialPackager_MQTT (Modelica_DeviceDrivers) talks to test.mosquitto.org,
# which the job's container resolves to this machine.
MQTT_PORT = 1883
class EchoHandler(socketserver.BaseRequestHandler):
def handle(self):
while True:
data = self.request.recv(4096)
if not data:
return
self.request.sendall(data)
class EchoServer(socketserver.ThreadingTCPServer):
allow_reuse_address = True
daemon_threads = True
def startEchoServer():
try:
server = EchoServer(("127.0.0.1", TCP_ECHO_PORT), EchoHandler)
except OSError as e:
print("Not starting the TCP echo server on port %d: %s" % (TCP_ECHO_PORT, e))
return None
threading.Thread(target=server.serve_forever, daemon=True).start()
return server
def startMqttBroker():
mosquitto = shutil.which("mosquitto") or shutil.which("mosquitto", path="/usr/sbin:/usr/local/sbin")
if not mosquitto:
print("No mosquitto, so no local MQTT broker")
return None
return subprocess.Popen([mosquitto, "-p", str(MQTT_PORT)], stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def start():
return (startEchoServer(), startMqttBroker())
def stop(services):
(echo, mqtt) = services
if echo is not None:
echo.shutdown()
echo.server_close()
if mqtt is not None:
mqtt.terminate()
try:
mqtt.wait(timeout=10)
except subprocess.TimeoutExpired:
mqtt.kill()