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 |
|
---|
18 |
|
---|
19 | class DummyReporter(Reporter):
|
---|
20 | def log(self, level:int, msg:str, thrown:BaseException=None):
|
---|
21 | print(msg)
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | class WebSocketProfileConnectorTest(unittest.TestCase):
|
---|
26 |
|
---|
27 | profiletext = Path("test/resources/japantrip1.json").read_text("utf-8")
|
---|
28 | reporter=DummyReporter()
|
---|
29 |
|
---|
30 | def t1estConnect(self):
|
---|
31 | session=Mock(Session)
|
---|
32 | # this session does nothing, we manipulate it ourselves.
|
---|
33 |
|
---|
34 | #mock websocket
|
---|
35 | wsContainer = Mock(WebSocketContainer)
|
---|
36 | wsContainer.connectToServer.return_value=session
|
---|
37 |
|
---|
38 | profint= WebsocketProfileConnector("ws://blabla", self.reporter, wsContainer )
|
---|
39 |
|
---|
40 | def pumpevents():
|
---|
41 | profint.onOpen(session)
|
---|
42 | sleep(1)
|
---|
43 | profint.onMessage(self.profiletext)
|
---|
44 | print ("Message was sent")
|
---|
45 |
|
---|
46 | threading.Thread(target=pumpevents).start()
|
---|
47 | profint.close()
|
---|
48 |
|
---|
49 |
|
---|
50 | self.assertEqual("japantrip1", profint.getProfile().getName())
|
---|
51 | print("Received profile succesfully!")
|
---|
52 |
|
---|
53 | def t1estConnectTimeout(self):
|
---|
54 | session=Mock(Session)
|
---|
55 | #mock websocket
|
---|
56 | wsContainer = Mock(WebSocketContainer)
|
---|
57 | wsContainer.connectToServer.return_value=session
|
---|
58 |
|
---|
59 | profint= WebsocketProfileConnector("ws://blabla", self.reporter, wsContainer )
|
---|
60 |
|
---|
61 | # since we don't pump events, this should timeout.
|
---|
62 | self.assertRaises(IOError, lambda:profint.getProfile())
|
---|
63 | profint.close()
|
---|
64 |
|
---|
65 |
|
---|
66 | def testWithRealProfilesServer(self):
|
---|
67 | '''
|
---|
68 | TO RUN THIS, ENSURE YOU HAVE A RUNNING PROFILESSERVER ON LOCALHOST
|
---|
69 | '''
|
---|
70 | profint= WebsocketProfileConnector(URI("ws://localhost:8080/profilesserver/websocket/get/party/party1"), self.reporter, DefaultWebSocketContainer() )
|
---|
71 | sleep(1)
|
---|
72 | profile=profint.getProfile()
|
---|
73 | self.assertTrue(profile)
|
---|
74 | profint.close()
|
---|
75 |
|
---|
76 | |
---|