[81] | 1 | import logging
|
---|
| 2 | from pathlib import Path # type:ignore
|
---|
| 3 | import threading
|
---|
| 4 | from time import sleep
|
---|
| 5 | from typing import Optional
|
---|
| 6 | import unittest
|
---|
| 7 | from unittest.mock import Mock
|
---|
| 8 |
|
---|
| 9 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
| 10 | from uri.uri import URI
|
---|
| 11 |
|
---|
| 12 | from geniusweb.profileconnection.FileProfileConnector import FileProfileConnector
|
---|
| 13 | from geniusweb.profileconnection.Session import Session
|
---|
| 14 | from geniusweb.profileconnection.WebSocketContainer import WebSocketContainer, \
|
---|
| 15 | DefaultWebSocketContainer
|
---|
| 16 | from geniusweb.profileconnection.WebsocketProfileConnector import WebsocketProfileConnector
|
---|
| 17 | from test.geniusweb.profileconnection.ProfileServerStub import ProfileServerStub
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | class DummyReporter(Reporter):
|
---|
| 21 | def log(self, level:int, msg:str, thrown:BaseException=None):
|
---|
| 22 | print(msg)
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | class WebSocketProfileConnectorTest(unittest.TestCase):
|
---|
| 27 |
|
---|
| 28 | profiletext = Path("test/resources/japantrip1.json").read_text("utf-8")
|
---|
| 29 | reporter=DummyReporter()
|
---|
| 30 |
|
---|
| 31 | def t1estConnect(self):
|
---|
| 32 | session=Mock(Session)
|
---|
| 33 | # this session does nothing, we manipulate it ourselves.
|
---|
| 34 |
|
---|
| 35 | #mock websocket
|
---|
| 36 | wsContainer = Mock(WebSocketContainer)
|
---|
| 37 | wsContainer.connectToServer.return_value=session
|
---|
| 38 |
|
---|
| 39 | profint= WebsocketProfileConnector("ws://blabla", self.reporter, wsContainer )
|
---|
| 40 |
|
---|
| 41 | def pumpevents():
|
---|
| 42 | profint.onOpen(session)
|
---|
| 43 | sleep(1)
|
---|
| 44 | profint.onMessage(self.profiletext)
|
---|
| 45 | print ("Message was sent")
|
---|
| 46 |
|
---|
| 47 | threading.Thread(target=pumpevents).start()
|
---|
| 48 | profint.close()
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | self.assertEqual("japantrip1", profint.getProfile().getName())
|
---|
| 52 | print("Received profile succesfully!")
|
---|
| 53 |
|
---|
| 54 | def t1estConnectTimeout(self):
|
---|
| 55 | session=Mock(Session)
|
---|
| 56 | #mock websocket
|
---|
| 57 | wsContainer = Mock(WebSocketContainer)
|
---|
| 58 | wsContainer.connectToServer.return_value=session
|
---|
| 59 |
|
---|
| 60 | profint= WebsocketProfileConnector("ws://blabla", self.reporter, wsContainer )
|
---|
| 61 |
|
---|
| 62 | # since we don't pump events, this should timeout.
|
---|
| 63 | self.assertRaises(IOError, lambda:profint.getProfile())
|
---|
| 64 | profint.close()
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | def testWithRealProfilesServer(self):
|
---|
| 68 | '''
|
---|
| 69 | TO RUN THIS, ENSURE YOU HAVE A RUNNING PROFILESSERVER ON LOCALHOST
|
---|
| 70 | '''
|
---|
| 71 | server=ProfileServerStub()
|
---|
| 72 | server.start()
|
---|
| 73 | try:
|
---|
| 74 | profint= WebsocketProfileConnector(URI("ws://localhost:8080/profilesserver/websocket/get/party/party1"), self.reporter, DefaultWebSocketContainer() )
|
---|
| 75 | sleep(1)
|
---|
| 76 | profile=profint.getProfile()
|
---|
| 77 | self.assertTrue(profile)
|
---|
| 78 | profint.close()
|
---|
| 79 | finally:
|
---|
| 80 | server.stop()
|
---|
| 81 | |
---|