11from os import environ
2+ import socket
3+
24import pytest
35from serial import Serial
46from geocompy .communication import (
57 get_dummy_logger ,
68 open_serial ,
9+ open_socket ,
710 SerialConnection ,
11+ SocketConnection ,
812 crc16_bitwise ,
913 crc16_bytewise
1014)
1115
1216
17+ @pytest .fixture
18+ def sock () -> socket .socket :
19+ return socket .socket (
20+ socket .AF_INET ,
21+ socket .SOCK_STREAM ,
22+ socket .IPPROTO_TCP
23+ )
24+
25+
1326portname = environ .get ("GEOCOMPY_TEST_PORT_CLIENT" , "" )
1427if portname == "" : # pragma: no coverage
1528 raise ValueError (
2437 "'GEOCOMPY_TEST_PORT_FAULTY' environment variable"
2538 )
2639
40+ tcpport = environ .get ("GEOCOMPY_TEST_TCPPORT_SERVER" , "" )
41+ if faultyportname == "" : # pragma: no coverage
42+ raise ValueError (
43+ "Echo server tcp port must be set in "
44+ "'GEOCOMPY_TEST_TCPPORT_SERVER' environment variable"
45+ )
46+
2747
2848class TestDummyLogger :
2949 def test_get_dummy_logger (self ) -> None :
@@ -32,6 +52,80 @@ def test_get_dummy_logger(self) -> None:
3252 assert len (log .handlers ) == 1
3353
3454
55+ class TestSocketConnection :
56+ def test_init (self , sock : socket .socket ) -> None :
57+ sock .connect (("127.0.0.1" , int (tcpport )))
58+ with SocketConnection (sock ) as client :
59+ assert client .is_open ()
60+
61+ client .close ()
62+ assert not client .is_open ()
63+
64+ def test_open_socket (self ) -> None :
65+ with open_socket ("127.0.0.1" , int (tcpport ), "tcp" ) as soc :
66+ assert soc .is_open ()
67+
68+ with pytest .raises (Exception ):
69+ open_socket ("127.0.0.1" , int (tcpport ), "rfcomm" , timeout = 1 )
70+
71+ with pytest .raises (ValueError ):
72+ open_socket (
73+ "127.0.0.1" ,
74+ int (tcpport ),
75+ "mistake" , # type: ignore[arg-type]
76+ timeout = 1
77+ )
78+
79+ def test_messaging (self ) -> None :
80+ with open_socket (
81+ "127.0.0.1" ,
82+ int (tcpport ),
83+ "tcp"
84+ ) as soc :
85+ soc .is_open ()
86+ request = "Test"
87+ assert soc .exchange (request ) == request
88+
89+ soc .send ("ascii" )
90+ assert soc .receive () == "ascii"
91+
92+ assert soc .exchange_binary (b"00\r \n " ) == b"00"
93+
94+ soc .reset ()
95+
96+ with pytest .raises (ConnectionError ):
97+ soc .send ("closed" )
98+
99+ with pytest .raises (ConnectionError ):
100+ soc .receive ()
101+
102+ with open_socket (
103+ "127.0.0.1" ,
104+ int (tcpport ),
105+ "tcp" ,
106+ sync_after_timeout = True
107+ ) as soc :
108+ soc .send ("msg1" )
109+ soc .send ("msg2" )
110+ soc .send ("msg3" )
111+ soc ._timeout_counter = 3
112+ assert soc .exchange ("recovered" ) == "recovered"
113+
114+ with soc .timeout_override (1 ):
115+ with pytest .raises (TimeoutError ):
116+ soc .receive ()
117+
118+ assert soc ._timeout_counter == 1
119+
120+ with pytest .raises (TimeoutError ):
121+ soc .receive ()
122+
123+ assert soc ._timeout_counter == 2
124+
125+ with pytest .raises (ConnectionError ):
126+ soc .receive ()
127+
128+
35129class TestSerialConnection :
36130 def test_init (self ) -> None :
37131 port = Serial (portname )
0 commit comments