source: geniuswebcore/test/geniusweb/profileconnection/WebSocketProfileConnectorTest.py@ 65

Last change on this file since 65 was 59, checked in by Wouter Pasman, 3 years ago

#44 manual commit of first public release, because this will cause the dist directory to move

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