Beispiel #1
0
 def test_close01(self) -> None:
     """A ServerWebSocket.BaseWebSocket.close() must close its raw websocket."""
     sec_interval = 0.1
     logger = logging.Logger("testing")
     rawws = DummyWebsocket(sec_interval, None)
     bs = ServerWebSocket.BaseWebSocket(rawws, logger)
     bs.close()
     assert not rawws._isopen, "raw websocket is not closed!"
Beispiel #2
0
def rfid_pinger(rawws: websocket):
    # print("bla before '{}'".format(rawws))
    ws = ServerWebSocket.JSONWebSocket(rawws, app.logger)
    my_server = stockyserver.RfidPingServer(app.logger, "RFIDPinger")
    print("goo: got a websocket")
    my_server.set_websocket(ws)
    print("goo: entering mainloop")
    my_server.mainloop()
    print("goo: exited mainloop")
Beispiel #3
0
def goo(rawws: websocket):
    print("bla before '{}'".format(rawws))
    ws = ServerWebSocket.JSONWebSocket(rawws, app.logger)
    print("goo: got a websocket")
    if THE_MAIN is not None:
        THE_MAIN.set_websocket(ws)
        print("goo: entering mainloop")
        THE_MAIN.mainloop()
        print("goo: exited mainloop")
    else:
        print('THE_MAIN is None!')
Beispiel #4
0
 def test_notimplemented01(self) -> None:
     """Not implemented methods in ServerWebSocket.BaseWebSocket must
     raise NotImplementedError."""
     sec_interval = 0.1
     logger = logging.Logger("testing")
     rawws = DummyWebsocket(sec_interval, None)
     bs = ServerWebSocket.BaseWebSocket(rawws, logger)
     dummy_msg = "hello"
     with pytest.raises(NotImplementedError):
         bs.decodeMSG(dummy_msg)
     with pytest.raises(NotImplementedError):
         bs.encodeMSG(dummy_msg)
Beispiel #5
0
 def test_wsreader01(self):
     """The WebSocketReader must behave sensibly when it reads
     junk from the websocket, and also produce a message on good data.
     """
     rawws = DummyWebsocket(self.sec_interval, None)
     ws = ServerWebSocket.JSONWebSocket(rawws, self.logger)
     wsr = Taskmeister.WebSocketReader(self.msgq,
                                       self.logger,
                                       ws,
                                       sec_interval=1.0,
                                       do_activate=False)
     assert wsr is not None, "wsr is None!"
     ok_dct = {'msg': CommonMSG.MSG_SV_RAND_NUM, 'data': 'dolly'}
     extra_dct = {
         'msg': CommonMSG.MSG_SV_RAND_NUM,
         'data': 'dolly',
         'extra': 'message'
     }
     ok_msg = CommonMSG(ok_dct['msg'], ok_dct['data'])
     for faulty_data, doraw, exp_val in [({
             'bla': 'blu'
     }, True, None), ({
             'msg': 'hello'
     }, True, None), ([1, 2, 3], True, None), (b'[1, 2}', False, None),
                                         ({
                                             'msg': 'hello',
                                             'data': 'dolly'
                                         }, True, None),
                                         ([1, 2, 3], True, None),
                                         (extra_dct, True, ok_msg),
                                         (ok_dct, True, ok_msg)]:
         if doraw:
             rawws = DummyWebsocket(self.sec_interval, faulty_data)
         else:
             rawws = DummyWebsocket(self.sec_interval, None)
             rawws._set_json_val(faulty_data)
         ws = ServerWebSocket.JSONWebSocket(rawws, self.logger)
         wsr.ws = ws
         retmsg = wsr.generate_msg()
         print("after sleep exp: {}, got {}".format(exp_val, retmsg))
Beispiel #6
0
 def test_sendmsg01(self) -> None:
     """ServerWebSocket.JSONWebSocket.sendMSG() must call the underlying
     rawsocket send method.
     """
     sec_interval = 0.1
     logger = logging.Logger("testing")
     rawws = DummyWebsocket(sec_interval, None)
     bs = ServerWebSocket.JSONWebSocket(rawws, logger)
     orgmsg = dict(hello='funny', world='strawberry')
     bs.sendMSG(orgmsg)
     msglst = rawws._sendlst
     assert len(msglst) == 1, "message no sent"
     encoded_msg = msglst[0]
     gotmsg = bs.decodeMSG(encoded_msg)
     assert gotmsg == orgmsg, "unexpected message received!"
Beispiel #7
0
 def test_wsreader02(self):
     """The WebSocketReader must behave sensibly when websocket.read()
     raises an exception.
     We expect an EOF message when the underlying websocket raises an exception on read.
     """
     rawws = ExceptionDummyWebsocket(self.sec_interval, None)
     ws = ServerWebSocket.JSONWebSocket(rawws, self.logger)
     wsr = Taskmeister.WebSocketReader(self.msgq,
                                       self.logger,
                                       ws,
                                       sec_interval=1.0,
                                       do_activate=False)
     exp_msg_val = CommonMSG.MSG_WC_EOF
     retmsg = wsr.generate_msg()
     print("after sleep exp: {}, got {}".format(exp_msg_val, retmsg))
     assert exp_msg_val == retmsg.msg, "unexpected retmsg"