1
- // vtest flaky: true
2
- // vtest retry: 8
3
1
import net
4
- import os
5
2
6
3
const test_port = 45123
7
4
8
- fn handle_conn (mut c net.TcpConn) {
5
+ fn handle_conn (mut c net.TcpConn) ! {
9
6
for {
10
7
mut buf := []u8 {len: 100 , init: 0 }
11
8
read := c.read (mut buf) or {
12
- println ('Server: connection dropped' )
13
- return
9
+ eprintln ('Server: connection dropped' )
10
+ return err
14
11
}
15
12
c.write (buf[..read]) or {
16
- println ('Server: connection dropped' )
17
- return
13
+ eprintln ('Server: connection dropped' )
14
+ return err
18
15
}
19
16
}
20
17
}
@@ -24,40 +21,45 @@ fn one_shot_echo_server(mut l net.TcpListener, ch_started chan int) ! {
24
21
ch_started < - 1
25
22
mut new_conn := l.accept () or { return error ('could not accept' ) }
26
23
eprintln (' > new_conn: ${new_conn} ' )
27
- handle_conn (mut new_conn)
28
- new_conn.close () or {}
24
+ handle_conn (mut new_conn)!
25
+ new_conn.close ()!
29
26
}
30
27
31
28
fn echo (address string ) ! {
32
29
mut c := net.dial_tcp (address)!
33
- defer {
34
- c.close () or {}
35
- }
36
30
37
- println ('local: ' + c.addr ()! .str ())
38
- println (' peer: ' + c.peer_addr ()! .str ())
31
+ eprintln ('local: ' + c.addr ()! .str ())
32
+ eprintln (' peer: ' + c.peer_addr ()! .str ())
39
33
40
34
data := 'Hello from vlib/net!'
41
35
c.write_string (data)!
42
36
mut buf := []u8 {len: 4096 }
43
- read := c.read (mut buf) or { panic (err) }
37
+ read := c.read (mut buf)!
44
38
assert read == data.len
45
39
for i := 0 ; i < read; i++ {
46
40
assert buf[i] == data[i]
47
41
}
48
- println ( 'Got " ${buf.bytestr()} "' )
42
+ c. close () !
49
43
}
50
44
51
45
fn test_tcp_ip6 () {
52
46
eprintln ('\n >>> ${@FN} ' )
53
- address := 'localhost :${test_port} '
54
- mut l := net.listen_tcp (.ip6 , ': ${test_port} ' ) or { panic (err) }
47
+ address := ':${test_port} '
48
+ mut l := net.listen_tcp (.ip6 , address) !
55
49
dump (l)
56
50
start_echo_server (mut l)
57
- echo (address) or { panic (err) }
58
- l.close () or {}
59
- // ensure there is at least one new socket created before the next test
60
- l = net.listen_tcp (.ip6 , ':${test_port + 1} ' ) or { panic (err) }
51
+ echo (address)!
52
+ l.close ()!
53
+ }
54
+
55
+ fn test_tcp_ip6_localhost () {
56
+ eprintln ('\n >>> ${@FN} ' )
57
+ address := '[::1]:${test_port} '
58
+ mut l := net.listen_tcp (.ip6 , address)!
59
+ dump (l)
60
+ start_echo_server (mut l)
61
+ echo (address)!
62
+ l.close ()!
61
63
}
62
64
63
65
fn start_echo_server (mut l net.TcpListener) {
@@ -68,31 +70,31 @@ fn start_echo_server(mut l net.TcpListener) {
68
70
69
71
fn test_tcp_ip () {
70
72
eprintln ('\n >>> ${@FN} ' )
71
- address := 'localhost :${test_port} '
72
- mut l := net.listen_tcp (.ip, address) or { panic (err) }
73
+ address := ':${test_port} '
74
+ mut l := net.listen_tcp (.ip, address)!
73
75
dump (l)
74
76
start_echo_server (mut l)
75
- echo (address) or { panic (err) }
76
- l.close () or {}
77
+ echo (address)!
78
+ l.close ()!
77
79
}
78
80
79
- fn test_tcp_unix () {
81
+ fn test_tcp_ip_localhost () {
80
82
eprintln ('\n >>> ${@FN} ' )
81
- // TODO(emily):
82
- // whilst windows supposedly supports unix sockets
83
- // this doesnt work (wsaeopnotsupp at the call to bind() )
84
- $if ! windows {
85
- address := os. real_path ( 'tcp-test.sock' )
86
- // address := 'tcp-test.sock'
87
- println ( ' ${address} ' )
83
+ address := '127.0.0.1: ${test_port} '
84
+ mut l := net. listen_tcp (.ip, address) !
85
+ dump (l )
86
+ start_echo_server ( mut l)
87
+ echo (address) !
88
+ l. close () !
89
+ }
88
90
89
- mut l := net. listen_tcp (.unix, address) or { panic (err) }
90
- start_echo_server ( mut l )
91
- echo ( address) or { panic (err) }
92
- l. close () or {}
91
+ fn test_tcp_unix () {
92
+ eprintln ( ' \n >>> ${@FN} ' )
93
+ address := 'tcp-test.sock'
94
+ eprintln ( ' ${address} ' )
93
95
94
- os. rm ( address) or { panic ( 'failed to remove socket file' ) }
95
- }
96
+ mut l := net. listen_tcp (.unix, address) or { return }
97
+ assert false
96
98
}
97
99
98
100
fn testsuite_end () {
0 commit comments