| 1 | // vtest build: !gcc-windows // too flaky on these CIs |
| 2 | // vtest retry: 3 |
| 3 | import os |
| 4 | import time |
| 5 | import net.unix |
| 6 | import net as _ |
| 7 | |
| 8 | const tfolder = os.join_path(os.temp_dir(), 'nuut_${os.getpid()}') |
| 9 | const test_port = os.join_path(tfolder, 'domain_socket') |
| 10 | |
| 11 | fn 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 | |
| 22 | fn testsuite_end() { |
| 23 | os.rmdir_all(tfolder) or {} |
| 24 | } |
| 25 | |
| 26 | fn 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 | |
| 46 | fn perror(s string) ! { |
| 47 | println(s) |
| 48 | } |
| 49 | |
| 50 | fn 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 | |
| 59 | fn 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 | |