x.websocket: websocket server fix and bring back server autobahn (#8291)

pull/8298/head
Tomas Hellström 2021-01-23 16:56:38 +01:00 committed by GitHub
parent 4065a0327a
commit 39e5f6e9df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 713 additions and 694 deletions

1339
.github/workflows/ci.yml vendored

File diff suppressed because it is too large Load Diff

View File

@ -19,17 +19,17 @@ with open("/reports/clients/index.json") as f:
nr_of_client_tests = nr_of_client_tests + 1 nr_of_client_tests = nr_of_client_tests + 1
# with open("/reports/servers/index.json") as f: with open("/reports/servers/index.json") as f:
# data = json.load(f) data = json.load(f)
# for i in data["AutobahnServer"]: for i in data["AutobahnServer"]:
# if ( if (
# data["AutobahnServer"][i]["behavior"] == "FAILED" data["AutobahnServer"][i]["behavior"] == "FAILED"
# or data["AutobahnServer"][i]["behaviorClose"] == "FAILED" or data["AutobahnServer"][i]["behaviorClose"] == "FAILED"
# ): ):
# nr_of_server_errs = nr_of_server_errs + 1 nr_of_server_errs = nr_of_server_errs + 1
# nr_of_server_tests = nr_of_server_tests + 1 nr_of_server_tests = nr_of_server_tests + 1
if nr_of_client_errs > 0 or nr_of_server_errs > 0: if nr_of_client_errs > 0 or nr_of_server_errs > 0:
print( print(

View File

@ -73,8 +73,8 @@ fn (mut s Server) handle_ping() {
mut clients_to_remove := []string{} mut clients_to_remove := []string{}
for s.state == .open { for s.state == .open {
time.sleep(s.ping_interval) time.sleep(s.ping_interval)
for _, cli in s.clients { for i, _ in s.clients {
mut c := cli mut c := s.clients[i]
if c.client.state == .open { if c.client.state == .open {
c.client.ping() or { c.client.ping() or {
s.logger.debug('server-> error sending ping to client') s.logger.debug('server-> error sending ping to client')

View File

@ -1,6 +1,12 @@
import x.websocket import x.websocket
import time import time
struct WebsocketTestResults {
pub mut:
nr_messages int
nr_pong_received int
}
// tests with internal ws servers // tests with internal ws servers
fn test_ws() { fn test_ws() {
go start_server() go start_server()
@ -11,7 +17,8 @@ fn test_ws() {
fn start_server() ? { fn start_server() ? {
mut s := websocket.new_server(30000, '') mut s := websocket.new_server(30000, '')
// make that in execution test time give time to execute at least one time // make that in execution test time give time to execute at least one time
s.ping_interval = 100 s.ping_interval = 1
s.on_connect(fn (mut s websocket.ServerClient) ?bool { s.on_connect(fn (mut s websocket.ServerClient) ?bool {
// here you can look att the client info and accept or not accept // here you can look att the client info and accept or not accept
// just returning a true/false // just returning a true/false
@ -22,8 +29,12 @@ fn start_server() ? {
return true return true
}) ? }) ?
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? { s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
ws.write(msg.payload, msg.opcode) or { panic(err) } match msg.opcode {
.pong { ws.write_str('pong') }
else { ws.write(msg.payload, msg.opcode) or { panic(err) } }
}
}) })
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? { s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
// not used // not used
}) })
@ -33,10 +44,11 @@ fn start_server() ? {
// ws_test tests connect to the websocket server from websocket client // ws_test tests connect to the websocket server from websocket client
fn ws_test(uri string) ? { fn ws_test(uri string) ? {
eprintln('connecting to $uri ...') eprintln('connecting to $uri ...')
mut test_results := WebsocketTestResults{}
mut ws := websocket.new_client(uri) ? mut ws := websocket.new_client(uri) ?
ws.on_open(fn (mut ws websocket.Client) ? { ws.on_open(fn (mut ws websocket.Client) ? {
println('open!') ws.pong() ?
ws.pong()
assert true assert true
}) })
ws.on_error(fn (mut ws websocket.Client, err string) ? { ws.on_error(fn (mut ws websocket.Client, err string) ? {
@ -44,19 +56,26 @@ fn ws_test(uri string) ? {
// this can be thrown by internet connection problems // this can be thrown by internet connection problems
assert false assert false
}) })
ws.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
println('closed') ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut res WebsocketTestResults) ? {
})
ws.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
println('client got type: $msg.opcode payload:\n$msg.payload') println('client got type: $msg.opcode payload:\n$msg.payload')
if msg.opcode == .text_frame { if msg.opcode == .text_frame {
smessage := msg.payload.bytestr() smessage := msg.payload.bytestr()
println('Message: $smessage') match smessage {
assert smessage == 'a' 'pong' {
res.nr_pong_received++
}
'a' {
res.nr_messages++
}
else {
assert false
}
}
} else { } else {
println('Binary message: $msg') println('Binary message: $msg')
} }
}) }, test_results)
ws.connect() or { panic('fail to connect') } ws.connect() or { panic('fail to connect') }
go ws.listen() go ws.listen()
text := ['a'].repeat(2) text := ['a'].repeat(2)
@ -66,5 +85,8 @@ fn ws_test(uri string) ? {
time.sleep_ms(100) time.sleep_ms(100)
} }
// sleep to give time to recieve response before asserts // sleep to give time to recieve response before asserts
time.sleep_ms(500) time.sleep_ms(1500)
// We expect at least 2 pongs, one sent directly and one indirectly
assert test_results.nr_pong_received >= 2
assert test_results.nr_messages == 2
} }