Blame view

sockets_tele/test/test_client_gemini.py 1.96 KB
fc6ea9bc   Etienne Pallier   socket client pou...
1
2
#!/usr/bin/env python3

c252db76   Etienne Pallier   Test maintenant s...
3
4
import threading

fc6ea9bc   Etienne Pallier   socket client pou...
5
6
7
8
import unittest

import sys
sys.path.append('..')
c252db76   Etienne Pallier   Test maintenant s...
9
from src.server.server_udp_or_tcp import get_SocketServer_UDP_TCP
ca2504aa   Etienne Pallier   Nouvelle version ...
10
11
from src.client.socket_client_telescope_gemini import SocketClientTelescopeGEMINI

c252db76   Etienne Pallier   Test maintenant s...
12
13
14
#HOST, PORT = "localhost", 9999
#HOST, PORT = "localhost", 20001
HOST, PORT = "localhost", 11110
fc6ea9bc   Etienne Pallier   socket client pou...
15
16
17

class TestClient(unittest.TestCase):

c252db76   Etienne Pallier   Test maintenant s...
18

fc6ea9bc   Etienne Pallier   socket client pou...
19
20
21
    def setUp(self):
        self.seq = range(10)

c252db76   Etienne Pallier   Test maintenant s...
22
23
24
    def server(self):
        with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver:
            myserver.serve_forever()
fc6ea9bc   Etienne Pallier   socket client pou...
25
        
c252db76   Etienne Pallier   Test maintenant s...
26
    def test_run(self):
fc6ea9bc   Etienne Pallier   socket client pou...
27
        
eb6306cf   Etienne Pallier   test ok (includin...
28
        q_a = [
ca2504aa   Etienne Pallier   Nouvelle version ...
29
            (':toto#', 'TOTO'),
6861c77e   Etienne Pallier   ACK implemented (...
30
            (':GD#', '+12:28'),
fc6ea9bc   Etienne Pallier   socket client pou...
31
            #(':GD#', '+12:28')
eb6306cf   Etienne Pallier   test ok (includin...
32
            ('6', 'G'),
fc6ea9bc   Etienne Pallier   socket client pou...
33
34
        ]
        
c252db76   Etienne Pallier   Test maintenant s...
35
36
37
38
39
40
41
        #TODO: RUN SERVER in a thread
        t_server = threading.Thread(target=self.server)
        #threads.append(t)
        t_server.start()
        #time.sleep(3)
        
        # RUN CLIENT to connect to server
fc6ea9bc   Etienne Pallier   socket client pou...
42
        #tsock = SocketClient_UDP_TCP(HOST, PORT, "UDP")
ca2504aa   Etienne Pallier   Nouvelle version ...
43
        with SocketClientTelescopeGEMINI(HOST, PORT) as tsock:
fc6ea9bc   Etienne Pallier   socket client pou...
44
45
46
47
48
            # Only useful for TCP (does nothing for UDP)
            tsock._connect_to_server()
            
            # 1) SEND REQUEST data to server
            
eb6306cf   Etienne Pallier   test ok (includin...
49
            for data in q_a:
fc6ea9bc   Etienne Pallier   socket client pou...
50
51
52
53
54
55
56
57
                question,answer = data
                tsock.send_data(question)
        
                # 2) RECEIVE REPLY data from server
                data_received = tsock.receive_data()
                self.assertEqual(data_received, answer)
            
            radec = tsock.get("RA-DEC")
ca2504aa   Etienne Pallier   Nouvelle version ...
58
59
            print("ra-dec is", radec)
            self.assertEqual(radec,  ['15:01:48', '+12:28'])
fc6ea9bc   Etienne Pallier   socket client pou...
60
61
            
            #tsock.close()
c252db76   Etienne Pallier   Test maintenant s...
62
63
64
65
66
            
        #TODO: Stop the server thread (t_server), how can we do that ?
        # cf https://www.oreilly.com/library/view/python-cookbook-2nd/0596007973/ch09s03.html
        #t_server.suicide_toi() !!!
        
fc6ea9bc   Etienne Pallier   socket client pou...
67
68
69

if __name__ == '__main__':
    unittest.main()