Fixed tests

This commit is contained in:
Fabio Manganiello 2018-01-29 16:34:00 +01:00
parent 0ff5adc4c1
commit 75fac37984
3 changed files with 19 additions and 13 deletions

View file

@ -4,7 +4,6 @@ import logging
import json import json
import os import os
import time import time
import websockets
from threading import Thread from threading import Thread
from multiprocessing import Process from multiprocessing import Process
@ -24,12 +23,14 @@ class HttpBackend(Backend):
-d '{"type":"request","target":"nodename","action":"tts.say","args": {"phrase":"This is a test"}}' \ -d '{"type":"request","target":"nodename","action":"tts.say","args": {"phrase":"This is a test"}}' \
http://localhost:8008/execute """ http://localhost:8008/execute """
def __init__(self, port=8008, websocket_port=8009, token=None, **kwargs): def __init__(self, port=8008, websocket_port=8009, disable_websocket=False,
token=None, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.port = port self.port = port
self.websocket_port = websocket_port self.websocket_port = websocket_port
self.token = token self.token = token
self.server_proc = None self.server_proc = None
self.disable_websocket = disable_websocket
self.websocket_thread = None self.websocket_thread = None
self.active_websockets = set() self.active_websockets = set()
@ -47,6 +48,8 @@ class HttpBackend(Backend):
def notify_web_clients(self, event): def notify_web_clients(self, event):
import websockets
async def send_event(websocket): async def send_event(websocket):
await websocket.send(str(event)) await websocket.send(str(event))
@ -110,6 +113,8 @@ class HttpBackend(Backend):
def websocket(self): def websocket(self):
import websockets
async def register_websocket(websocket, path): async def register_websocket(websocket, path):
logging.info('New websocket connection from {}'.format(websocket.remote_address[0])) logging.info('New websocket connection from {}'.format(websocket.remote_address[0]))
self.active_websockets.add(websocket) self.active_websockets.add(websocket)
@ -139,13 +144,15 @@ class HttpBackend(Backend):
'debug':True, 'host':'0.0.0.0', 'port':self.port, 'use_reloader':False 'debug':True, 'host':'0.0.0.0', 'port':self.port, 'use_reloader':False
}) })
self.websocket_thread = Thread(target=self.websocket)
time.sleep(1) time.sleep(1)
self.server_proc.start() self.server_proc.start()
if not self.disable_websocket:
self.websocket_thread = Thread(target=self.websocket)
self.websocket_thread.start() self.websocket_thread.start()
self.server_proc.join() self.server_proc.join()
self.websocket_thread.join()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -1,3 +1,4 @@
backend.http: backend.http:
port: 8123 port: 8123
disable_websocket: True

View file

@ -47,14 +47,12 @@ class TestHttp(unittest.TestCase):
on_timeout=self.on_timeout('Receiver response timed out')) on_timeout=self.on_timeout('Receiver response timed out'))
response = requests.post( response = requests.post(
u'http://localhost:8123/', u'http://localhost:8123/execute',
data = { json = {
'msg': json.dumps({
'type': 'request', 'type': 'request',
'target': Config.get('device_id'), 'target': Config.get('device_id'),
'action': 'shell.exec', 'action': 'shell.exec',
'args': { 'cmd':'echo ping' } 'args': { 'cmd':'echo ping' }
})
} }
) )