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

View File

@ -127,7 +127,6 @@ jobs:
cd tutorials/code/blog cd tutorials/code/blog
../../../v . ../../../v .
# Alpine docker pre-built container # Alpine docker pre-built container
alpine-docker-musl-gcc: alpine-docker-musl-gcc:
name: alpine-musl name: alpine-musl
@ -489,7 +488,6 @@ jobs:
# - name: V self compilation with -autofree # - name: V self compilation with -autofree
# run: ./v -o v2 -autofree cmd/v && ./v2 -o v3 -autofree cmd/v && ./v3 -o v4 -autofree cmd/v # run: ./v -o v2 -autofree cmd/v && ./v2 -o v3 -autofree cmd/v && ./v3 -o v4 -autofree cmd/v
# Ubuntu docker pre-built container # Ubuntu docker pre-built container
ubuntu-musl: ubuntu-musl:
name: ubuntu-musl name: ubuntu-musl
@ -671,7 +669,6 @@ jobs:
# - name: v2 self compilation # - name: v2 self compilation
# run: .\v.exe -o v2.exe cmd/v && .\v2.exe -o v3.exe cmd/v # run: .\v.exe -o v2.exe cmd/v && .\v2.exe -o v3.exe cmd/v
compilable-v-c-and-v-win-c: compilable-v-c-and-v-win-c:
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
timeout-minutes: 30 timeout-minutes: 30
@ -781,8 +778,8 @@ jobs:
run: docker exec autobahn_client "/src/v" "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss.v" run: docker exec autobahn_client "/src/v" "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss.v"
- name: Run client wss test - name: Run client wss test
run: docker exec autobahn_client "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss" run: docker exec autobahn_client "/src/vlib/x/websocket/tests/autobahn/autobahn_client_wss"
# - name: Run server test - name: Run server test
# run: docker exec autobahn_server "wstest" "-m" "fuzzingclient" "-s" "/config/fuzzingclient.json" run: docker exec autobahn_server "wstest" "-m" "fuzzingclient" "-s" "/config/fuzzingclient.json"
- name: Copy reports - name: Copy reports
run: docker cp autobahn_server:/reports ${{github.workspace}}/reports run: docker cp autobahn_server:/reports ${{github.workspace}}/reports
- name: Copy reports wss - name: Copy reports wss
@ -802,11 +799,11 @@ jobs:
with: with:
name: client name: client
path: ${{github.workspace}}/reports/clients/index.html path: ${{github.workspace}}/reports/clients/index.html
# - name: Publish report server - name: Publish report server
# uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
# with: with:
# name: server name: server
# path: ${{github.workspace}}/reports/servers/index.html path: ${{github.workspace}}/reports/servers/index.html
- name: Publish all reports WSS - name: Publish all reports WSS
uses: actions/upload-artifact@v2 uses: actions/upload-artifact@v2
with: with:

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
} }