Blame view

devices_channel/test/test_client_gemini.py 2.24 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('..')
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
9
10
11
from src_device.server.server_udp_or_tcp import get_SocketServer_UDP_TCP
#from src_socket.client.socket_client_telescope_gemini import SocketClientTelescopeGEMINI
import src_device.client.telescope_controller_gemini as gemini 
ca2504aa   Etienne Pallier   Nouvelle version ...
12

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

class TestClient(unittest.TestCase):

c252db76   Etienne Pallier   Test maintenant s...
19

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

c252db76   Etienne Pallier   Test maintenant s...
23
24
25
    def server(self):
        with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver:
            myserver.serve_forever()
fc6ea9bc   Etienne Pallier   socket client pou...
26
        
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
27
28
29
30
31
32
33
        
    def test_run_doctests(self):
        import doctest
        doctest.testmod(gemini)


    def test_run_unittests(self):
fc6ea9bc   Etienne Pallier   socket client pou...
34
        
eb6306cf   Etienne Pallier   test ok (includin...
35
        q_a = [
ca2504aa   Etienne Pallier   Nouvelle version ...
36
            (':toto#', 'TOTO'),
6861c77e   Etienne Pallier   ACK implemented (...
37
            (':GD#', '+12:28'),
fc6ea9bc   Etienne Pallier   socket client pou...
38
            #(':GD#', '+12:28')
eb6306cf   Etienne Pallier   test ok (includin...
39
            ('6', 'G'),
fc6ea9bc   Etienne Pallier   socket client pou...
40
41
        ]
        
c252db76   Etienne Pallier   Test maintenant s...
42
43
44
45
46
47
48
        #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
54cb16da   Etienne Pallier   implementation du...
49
        #tele_client = SocketClient_UDP_TCP(HOST, PORT, "UDP")
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
50
        with gemini.TelescopeControllerGEMINI(HOST, PORT) as tele_client:
fc6ea9bc   Etienne Pallier   socket client pou...
51
            # Only useful for TCP (does nothing for UDP)
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
52
            tele_client._connect_to_device()
fc6ea9bc   Etienne Pallier   socket client pou...
53
54
55
            
            # 1) SEND REQUEST data to server
            
eb6306cf   Etienne Pallier   test ok (includin...
56
            for data in q_a:
fc6ea9bc   Etienne Pallier   socket client pou...
57
                question,answer = data
54cb16da   Etienne Pallier   implementation du...
58
                tele_client.send_data(question)
fc6ea9bc   Etienne Pallier   socket client pou...
59
60
        
                # 2) RECEIVE REPLY data from server
54cb16da   Etienne Pallier   implementation du...
61
                data_received = tele_client.receive_data()
fc6ea9bc   Etienne Pallier   socket client pou...
62
63
                self.assertEqual(data_received, answer)
            
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
64
65
            radec = tele_client.get_radec()
            print("ra-dec is", radec)
ff4e8d44   Etienne Pallier   improved telescop...
66
            #self.assertEqual(radec,  ('15:01:48', '+12:28'))
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
67
            self.assertEqual(radec.txt, '15:01:48,+12:28')
fc6ea9bc   Etienne Pallier   socket client pou...
68
            
54cb16da   Etienne Pallier   implementation du...
69
            #tele_client.close()
c252db76   Etienne Pallier   Test maintenant s...
70
71
72
73
            
        #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() !!!
62e41b8f   Etienne Pallier   BIG REORGANIZATIO...
74

fc6ea9bc   Etienne Pallier   socket client pou...
75
76
77

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