net: improve socket_test (#6822)

pull/6819/head
Takahiro Yaota 2020-11-14 15:23:22 +09:00 committed by GitHub
parent 01579957b8
commit 5069be04a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 22 deletions

View File

@ -1,62 +1,106 @@
import net import net
fn setup() (net.Socket, net.Socket, net.Socket) { fn setup() (net.Socket, net.Socket, net.Socket) {
server := net.listen(0) or { panic(err) } server := net.listen(0) or {
panic(err)
}
server_port := server.get_port() server_port := server.get_port()
client := net.dial('127.0.0.1', server_port) or { panic(err) } client := net.dial('127.0.0.1', server_port) or {
socket := server.accept() or { panic(err) } panic(err)
}
socket := server.accept() or {
panic(err)
}
$if debug_peer_ip ? { $if debug_peer_ip ? {
ip := socket.peer_ip() or { '$err' } ip := socket.peer_ip() or {
'$err'
}
eprintln('socket peer_ip: $ip') eprintln('socket peer_ip: $ip')
} }
return server, client, socket return server, client, socket
} }
fn cleanup(server &net.Socket, client &net.Socket, socket &net.Socket) { fn cleanup(server &net.Socket, client &net.Socket, socket &net.Socket) {
server.close() or {} server.close() or { }
client.close() or {} client.close() or { }
socket.close() or {} socket.close() or { }
} }
fn test_socket() { fn test_socket() {
server, client, socket := setup() server, client, socket := setup()
defer {
cleanup(server, client, socket)
}
message := 'Hello World' message := 'Hello World'
socket.send(message.str, message.len) or { assert false } socket.send(message.str, message.len) or {
$if debug { println('message send: $message') } assert false
$if debug { println('send socket: $socket.sockfd') } }
$if debug {
println('message send: $message')
}
$if debug {
println('send socket: $socket.sockfd')
}
bytes, blen := client.recv(1024) bytes, blen := client.recv(1024)
received := tos(bytes, blen) received := tos(bytes, blen)
$if debug { println('message received: $received') } $if debug {
$if debug { println('client: $client.sockfd') } println('message received: $received')
}
$if debug {
println('client: $client.sockfd')
}
assert message == received assert message == received
}
fn test_socket_read_line() {
server, client, socket := setup()
defer {
cleanup(server, client, socket) cleanup(server, client, socket)
}
message1, message2 := 'message1', 'message2'
message := '$message1\n$message2'
socket.write(message) or {
assert false
}
line1, line2 := client.read_line(), client.read_line()
assert line1 != message1
assert line1.trim_space() == message1
assert line2 != message2
assert line2.trim_space() == message2
} }
fn test_socket_write() { fn test_socket_write() {
server, client, socket := setup() server, client, socket := setup()
defer {
cleanup(server, client, socket)
}
message1 := 'a message 1' message1 := 'a message 1'
socket.write(message1) or { assert false } socket.write(message1) or {
assert false
}
line1 := client.read_line() line1 := client.read_line()
assert line1 != message1 assert line1 != message1
assert line1.trim_space() == message1 assert line1.trim_space() == message1
cleanup(server, client, socket)
} }
fn test_socket_write_fail_without_panic() { fn test_socket_write_fail_without_panic() {
server, client, socket := setup() server, client, socket := setup()
defer {
cleanup(server, client, socket)
}
message2 := 'a message 2' message2 := 'a message 2'
// ensure that socket.write (i.e. done on the server side) // ensure that socket.write (i.e. done on the server side)
// continues to work, even when the client side has been disconnected // continues to work, even when the client side has been disconnected
// this test is important for a stable long standing server // this test is important for a stable long standing server
client.close() or {} client.close() or { }
$if solaris { return } // TODO: fix segfaulting on Solaris $if solaris {
for i:=0; i<3; i++ { return
}
// TODO: fix segfaulting on Solaris
for i := 0; i < 3; i++ {
socket.write(message2) or { socket.write(message2) or {
println('write to a socket without a recipient should produce an option fail: $err | $message2') println('write to a socket without a recipient should produce an option fail: $err | $message2')
assert true assert true
} }
} }
cleanup(server, client, socket)
} }