v2 / vlib / net / unix / use_net_and_net_unix_together_test.v
64 lines · 55 sloc · 1.58 KB · df2a8ab2ef95a1459f789c1fdd9741121abfbe54
Raw
1// vtest build: !gcc-windows // too flaky on these CIs
2// vtest retry: 3
3import os
4import time
5import net.unix
6import net as _
7
8const tfolder = os.join_path(os.temp_dir(), 'nuut_${os.getpid()}')
9const test_port = os.join_path(tfolder, 'domain_socket')
10
11fn testsuite_begin() {
12 os.mkdir_all(tfolder) or {}
13 spawn fn () {
14 // Normally this entire test should take less than a second,
15 // but sometimes it hangs on windows for hours. Instead of skipping it entirely,
16 // ensure the test will die, and later the test framework can restart it.
17 time.sleep(5 * time.second)
18 exit(1)
19 }()
20}
21
22fn testsuite_end() {
23 os.rmdir_all(tfolder) or {}
24}
25
26fn test_that_net_and_net_unix_can_be_imported_together_without_conflicts() {
27 mut l := unix.listen_stream(test_port) or { panic(err) }
28 spawn echo_server(mut l)
29 defer {
30 l.close() or {}
31 }
32
33 mut c := unix.connect_stream(test_port)!
34 defer {
35 c.close() or {}
36 }
37
38 data := 'Hello from vlib/net!'
39 c.write_string(data)!
40 mut buf := []u8{len: 100}
41 assert c.read(mut buf)! == data.len
42 eprintln('< client read back buf: |${buf[0..data.len].bytestr()}|')
43 assert buf[0..data.len] == data.bytes()
44}
45
46fn perror(s string) ! {
47 println(s)
48}
49
50fn handle_conn(mut c unix.StreamConn) ! {
51 for {
52 mut buf := []u8{len: 100, init: 0}
53 read := c.read(mut buf) or { return perror('Server: connection dropped') }
54 eprintln('> server read ${read:3}, buf: |${buf.bytestr()}|')
55 c.write(buf[..read]) or { return perror('Server: connection dropped') }
56 }
57}
58
59fn echo_server(mut l unix.StreamListener) ! {
60 for {
61 mut new_conn := l.accept() or { continue }
62 handle_conn(mut new_conn) or {}
63 }
64}
65