2021-02-21 15:32:02 +01:00
|
|
|
import os
|
2020-08-22 00:50:38 +02:00
|
|
|
import x.websocket
|
|
|
|
import time
|
2021-02-20 19:24:15 +01:00
|
|
|
import rand
|
2020-08-22 00:50:38 +02:00
|
|
|
|
2021-01-23 16:56:38 +01:00
|
|
|
struct WebsocketTestResults {
|
|
|
|
pub mut:
|
|
|
|
nr_messages int
|
|
|
|
nr_pong_received int
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:32:02 +01:00
|
|
|
const github_job = os.getenv('GITHUB_JOB')
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// tests with internal ws servers
|
2020-11-20 12:32:46 +01:00
|
|
|
fn test_ws() {
|
2021-02-21 15:32:02 +01:00
|
|
|
if github_job != '' && github_job != 'websocket_tests' {
|
|
|
|
// Do not run these tests everytime, since they are flaky.
|
|
|
|
// They have their own specialized CI runner.
|
|
|
|
return
|
|
|
|
}
|
2021-02-20 19:24:15 +01:00
|
|
|
port := 30000 + rand.intn(1024)
|
|
|
|
go start_server(port)
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(500 * time.millisecond)
|
2021-02-20 19:24:15 +01:00
|
|
|
ws_test('ws://localhost:$port') or { assert false }
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 19:24:15 +01:00
|
|
|
fn start_server(listen_port int) ? {
|
|
|
|
mut s := websocket.new_server(listen_port, '')
|
2020-12-04 01:52:26 +01:00
|
|
|
// make that in execution test time give time to execute at least one time
|
2021-01-23 16:56:38 +01:00
|
|
|
s.ping_interval = 1
|
|
|
|
|
2020-08-22 00:50:38 +02:00
|
|
|
s.on_connect(fn (mut s websocket.ServerClient) ?bool {
|
2020-12-04 01:52:26 +01:00
|
|
|
// here you can look att the client info and accept or not accept
|
2020-08-22 00:50:38 +02:00
|
|
|
// just returning a true/false
|
|
|
|
if s.resource_name != '/' {
|
2020-11-07 17:14:33 +01:00
|
|
|
panic('unexpected resource name in test')
|
2020-08-22 00:50:38 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2020-11-20 12:32:46 +01:00
|
|
|
}) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
2021-01-23 16:56:38 +01:00
|
|
|
match msg.opcode {
|
2021-03-20 17:25:51 +01:00
|
|
|
.pong { ws.write_string('pong') or { panic(err) } }
|
2021-03-01 00:18:14 +01:00
|
|
|
else { ws.write(msg.payload, msg.opcode) or { panic(err) } }
|
2021-01-23 16:56:38 +01:00
|
|
|
}
|
2020-08-22 00:50:38 +02:00
|
|
|
})
|
2021-01-23 16:56:38 +01:00
|
|
|
|
2020-08-22 00:50:38 +02:00
|
|
|
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
2020-12-04 01:52:26 +01:00
|
|
|
// not used
|
2020-08-22 00:50:38 +02:00
|
|
|
})
|
2021-03-06 20:04:51 +01:00
|
|
|
s.listen() or {}
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:52:26 +01:00
|
|
|
// ws_test tests connect to the websocket server from websocket client
|
2020-08-22 00:50:38 +02:00
|
|
|
fn ws_test(uri string) ? {
|
|
|
|
eprintln('connecting to $uri ...')
|
2021-01-23 16:56:38 +01:00
|
|
|
|
|
|
|
mut test_results := WebsocketTestResults{}
|
2020-11-20 12:32:46 +01:00
|
|
|
mut ws := websocket.new_client(uri) ?
|
2020-08-22 00:50:38 +02:00
|
|
|
ws.on_open(fn (mut ws websocket.Client) ? {
|
2021-01-23 16:56:38 +01:00
|
|
|
ws.pong() ?
|
2020-08-22 00:50:38 +02:00
|
|
|
assert true
|
|
|
|
})
|
|
|
|
ws.on_error(fn (mut ws websocket.Client, err string) ? {
|
|
|
|
println('error: $err')
|
|
|
|
// this can be thrown by internet connection problems
|
|
|
|
assert false
|
|
|
|
})
|
2021-01-23 16:56:38 +01:00
|
|
|
|
|
|
|
ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut res WebsocketTestResults) ? {
|
2020-08-22 00:50:38 +02:00
|
|
|
println('client got type: $msg.opcode payload:\n$msg.payload')
|
|
|
|
if msg.opcode == .text_frame {
|
2020-08-22 12:29:15 +02:00
|
|
|
smessage := msg.payload.bytestr()
|
2021-01-23 16:56:38 +01:00
|
|
|
match smessage {
|
|
|
|
'pong' {
|
|
|
|
res.nr_pong_received++
|
|
|
|
}
|
|
|
|
'a' {
|
|
|
|
res.nr_messages++
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 00:50:38 +02:00
|
|
|
} else {
|
|
|
|
println('Binary message: $msg')
|
|
|
|
}
|
2021-01-23 16:56:38 +01:00
|
|
|
}, test_results)
|
2020-12-28 06:25:46 +01:00
|
|
|
ws.connect() or { panic('fail to connect') }
|
2020-08-22 00:50:38 +02:00
|
|
|
go ws.listen()
|
|
|
|
text := ['a'].repeat(2)
|
|
|
|
for msg in text {
|
2020-12-28 06:25:46 +01:00
|
|
|
ws.write(msg.bytes(), .text_frame) or { panic('fail to write to websocket') }
|
2020-08-22 00:50:38 +02:00
|
|
|
// sleep to give time to recieve response before send a new one
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|
|
|
|
// sleep to give time to recieve response before asserts
|
2021-02-27 18:41:06 +01:00
|
|
|
time.sleep(1500 * time.millisecond)
|
2021-03-01 00:18:14 +01:00
|
|
|
// We expect at least 2 pongs, one sent directly and one indirectly
|
2021-01-23 16:56:38 +01:00
|
|
|
assert test_results.nr_pong_received >= 2
|
|
|
|
assert test_results.nr_messages == 2
|
2020-08-22 00:50:38 +02:00
|
|
|
}
|