From e974a866b632761aeff93492f3ff2371656e34be Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 21 Sep 2021 09:35:12 +0200 Subject: [PATCH 1/4] Update to latest asynctest, with unittest2 support Also fixes a number of GcUnsafe2 warnings, by placing var declarations inside the suite. --- tests/asyncunit.nim | 32 ----------------------- tests/extensions/testcompression.nim | 2 +- tests/extensions/testextflow.nim | 2 +- tests/extensions/testexts.nim | 2 +- tests/testextutils.nim | 5 ++-- tests/testframes.nim | 10 +++++--- tests/testutf8.nim | 11 ++++---- tests/testwebsockets.nim | 38 +++++++++++++++++++++++----- websock.nimble | 2 +- 9 files changed, 50 insertions(+), 54 deletions(-) delete mode 100644 tests/asyncunit.nim diff --git a/tests/asyncunit.nim b/tests/asyncunit.nim deleted file mode 100644 index b87e5064dc..0000000000 --- a/tests/asyncunit.nim +++ /dev/null @@ -1,32 +0,0 @@ -## nim-websock -## Copyright (c) 2021 Status Research & Development GmbH -## Licensed under either of -## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) -## * MIT license ([LICENSE-MIT](LICENSE-MIT)) -## at your option. -## This file may not be copied, modified, or distributed except according to -## those terms. - -import unittest2 -export unittest2 except suite, test - -template suite*(name, body) = - suite name: - - template setup(setupBody) {.used.} = - setup: - let asyncproc = proc {.async.} = setupBody - waitFor asyncproc() - - template teardown(teardownBody) {.used.} = - teardown: - let asyncproc = proc {.async.} = teardownBody - waitFor asyncproc() - - let suiteproc = proc = body # Avoids GcUnsafe2 warnings with chronos - suiteproc() - -template test*(name, body) = - test name: - let asyncproc = proc {.async.} = body - waitFor asyncproc() diff --git a/tests/extensions/testcompression.nim b/tests/extensions/testcompression.nim index ec95db5695..68ecf6be3c 100644 --- a/tests/extensions/testcompression.nim +++ b/tests/extensions/testcompression.nim @@ -9,7 +9,7 @@ import std/os import pkg/[chronos, stew/byteutils, stew/io2] -import ../asyncunit +import pkg/asynctest/unittest2 import ../../websock/websock, ../helpers import ../../websock/extensions/compression/deflate diff --git a/tests/extensions/testextflow.nim b/tests/extensions/testextflow.nim index 4b95bb788d..fcc00e8481 100644 --- a/tests/extensions/testextflow.nim +++ b/tests/extensions/testextflow.nim @@ -9,9 +9,9 @@ import std/strutils import pkg/[chronos, stew/byteutils] +import pkg/asynctest/unittest2 import ../../ws/ws -import ../asyncunit type ExtHandler = proc(ext: Ext, frame: Frame): Future[Frame] {.raises: [Defect].} diff --git a/tests/extensions/testexts.nim b/tests/extensions/testexts.nim index 96be6289ec..fa7b1c3f69 100644 --- a/tests/extensions/testexts.nim +++ b/tests/extensions/testexts.nim @@ -8,7 +8,7 @@ ## those terms. import pkg/[chronos, stew/byteutils] -import ../asyncunit +import pkg/asynctest/unittest2 import ./base64ext, ./hexext import ../../websock/websock, ../helpers diff --git a/tests/testextutils.nim b/tests/testextutils.nim index 22143c2612..957378cac4 100644 --- a/tests/testextutils.nim +++ b/tests/testextutils.nim @@ -8,7 +8,8 @@ ## those terms. import - pkg/[asynctest, chronos], + pkg/chronos, + pkg/asynctest/unittest2, ../websock/extensions suite "extension parser": @@ -266,4 +267,4 @@ suite "extension parser": test "attreversed": var app: seq[AppExt] let res = parseExt("filename=foo.html; attachment", app) - check res == false \ No newline at end of file + check res == false diff --git a/tests/testframes.nim b/tests/testframes.nim index af49fa6a0c..cb0f506b6c 100644 --- a/tests/testframes.nim +++ b/tests/testframes.nim @@ -7,16 +7,18 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import pkg/[asynctest, stew/byteutils] +import pkg/stew/byteutils +import pkg/asynctest/unittest2 include ../websock/frame include ../websock/utils # TODO: Fix Test. -var maskKey: array[4, char] - suite "Test data frames": + + var maskKey: array[4, char] + test "# 7bit length text": check (await Frame( fin: false, @@ -252,6 +254,8 @@ suite "Test data frames": suite "Test control frames": + var maskKey: array[4, char] + test "Close": check (await Frame( fin: true, diff --git a/tests/testutf8.nim b/tests/testutf8.nim index 4443646101..184604092c 100644 --- a/tests/testutf8.nim +++ b/tests/testutf8.nim @@ -11,7 +11,7 @@ import std/[strutils], pkg/[ stew/byteutils, - asynctest, + asynctest/unittest2, chronos, chronicles ], @@ -78,12 +78,11 @@ proc waitForClose(ws: WSSession) {.async.} = except CatchableError: trace "Closing websocket" -# TODO: use new test framework from dryajov -# if it is ready. -var server: HttpServer -let address = initTAddress("127.0.0.1:8888") - suite "UTF-8 validator in action": + + var server: HttpServer + let address = initTAddress("127.0.0.1:8888") + teardown: server.stop() await server.closeWait() diff --git a/tests/testwebsockets.nim b/tests/testwebsockets.nim index d4d9487024..7b8f5df606 100644 --- a/tests/testwebsockets.nim +++ b/tests/testwebsockets.nim @@ -12,20 +12,20 @@ import pkg/[ httputils, chronos, chronicles, - stew/byteutils] + stew/byteutils, + asynctest/unittest2] import ../websock/websock -import ./asynctest import ./helpers -let - address* = initTAddress("127.0.0.1:8888") - -var - server: HttpServer +let address = initTAddress("127.0.0.1:8888") suite "Test handshake": + + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -98,6 +98,9 @@ suite "Test handshake": protocols = @["proto"]) suite "Test transmission": + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -163,6 +166,10 @@ suite "Test transmission": await waitForClose(session) suite "Test ping-pong": + + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -280,6 +287,10 @@ suite "Test ping-pong": await session.close() suite "Test framing": + + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -338,6 +349,10 @@ suite "Test framing": await waitForClose(session) suite "Test Closing": + + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -564,6 +579,9 @@ suite "Test Closing": await session.close(reason = "HH") suite "Test Payload": + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -789,6 +807,9 @@ suite "Test Payload": ws.binary == false suite "Test Binary message with Payload": + var + server: HttpServer + teardown: server.stop() await server.closeWait() @@ -953,6 +974,9 @@ suite "Test Binary message with Payload": ws.binary == true suite "Partial frames": + var + server: HttpServer + teardown: server.stop() await server.closeWait() diff --git a/websock.nimble b/websock.nimble index 2d5ec50e22..77de9705d3 100644 --- a/websock.nimble +++ b/websock.nimble @@ -19,7 +19,7 @@ requires "chronos >= 3.0.0" requires "httputils >= 0.2.0" requires "chronicles#ba2817f1" requires "stew >= 0.1.0" -requires "asynctest >= 0.2.0 & < 0.3.0" +requires "asynctest >= 0.3.0 & < 0.4.0" requires "nimcrypto" requires "bearssl" requires "https://github.com/status-im/nim-zlib" From 80f62e8fddcca8082d04d269946ea4042fd8c6b3 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 21 Sep 2021 09:44:07 +0200 Subject: [PATCH 2/4] Remove unused imports --- tests/extensions/base64ext.nim | 1 - tests/extensions/hexext.nim | 1 - tests/extensions/testcompression.nim | 4 ++-- tests/helpers.nim | 3 +-- tests/testwebsockets.nim | 2 +- websock/extensions.nim | 1 - websock/http.nim | 1 - websock/websock.nim | 3 +-- 8 files changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/extensions/base64ext.nim b/tests/extensions/base64ext.nim index a52d319f58..0ffd24fca0 100644 --- a/tests/extensions/base64ext.nim +++ b/tests/extensions/base64ext.nim @@ -8,7 +8,6 @@ ## those terms. import - std/[strutils], pkg/[stew/results, stew/base64, chronos, diff --git a/tests/extensions/hexext.nim b/tests/extensions/hexext.nim index 5507ff58e0..9da8f37ca3 100644 --- a/tests/extensions/hexext.nim +++ b/tests/extensions/hexext.nim @@ -8,7 +8,6 @@ ## those terms. import - std/[strutils], pkg/[stew/results, stew/byteutils, chronos, diff --git a/tests/extensions/testcompression.nim b/tests/extensions/testcompression.nim index 68ecf6be3c..72dc0d5c3e 100644 --- a/tests/extensions/testcompression.nim +++ b/tests/extensions/testcompression.nim @@ -8,9 +8,9 @@ ## those terms. import std/os -import pkg/[chronos, stew/byteutils, stew/io2] +import pkg/[chronos, stew/io2] import pkg/asynctest/unittest2 -import ../../websock/websock, ../helpers +import ../../websock/websock import ../../websock/extensions/compression/deflate const diff --git a/tests/helpers.nim b/tests/helpers.nim index 1a82a0a9db..bc473d282c 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -14,8 +14,7 @@ import pkg/[ chronos, chronos/streams/tlsstream, httputils, - chronicles, - stew/byteutils] + chronicles] import ../websock/websock import ./keys diff --git a/tests/testwebsockets.nim b/tests/testwebsockets.nim index 7b8f5df606..bbc28f9c4d 100644 --- a/tests/testwebsockets.nim +++ b/tests/testwebsockets.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import std/[strutils, random] +import std/strutils import pkg/[ httputils, chronos, diff --git a/websock/extensions.nim b/websock/extensions.nim index b8c84769fd..b5ad4588cd 100644 --- a/websock/extensions.nim +++ b/websock/extensions.nim @@ -7,7 +7,6 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import std/tables import ./extensions/extutils # import ./extensions/compression/compression diff --git a/websock/http.nim b/websock/http.nim index e78ead93d0..d076a2016a 100644 --- a/websock/http.nim +++ b/websock/http.nim @@ -9,7 +9,6 @@ import std/uri import pkg/[ - chronos, chronos/apps/http/httptable, chronos/streams/tlsstream, httputils] diff --git a/websock/websock.nim b/websock/websock.nim index 612d05327f..8af803f274 100644 --- a/websock/websock.nim +++ b/websock/websock.nim @@ -13,8 +13,7 @@ import std/[tables, strutils, strformat, sequtils, - uri, - parseutils] + uri] import pkg/[chronos, chronos/apps/http/httptable, From 6f3d5cdb2d1b2bc1ad90f0ca6f393b412832eda4 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 21 Sep 2021 09:45:09 +0200 Subject: [PATCH 3/4] Fix remaining GCUnsafe2 warnings Removes last use of globals in async tests. --- tests/helpers.nim | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/helpers.nim b/tests/helpers.nim index bc473d282c..fe408f428f 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -19,10 +19,6 @@ import pkg/[ import ../websock/websock import ./keys -let - WSSecureKey* = TLSPrivateKey.init(SecureKey) - WSSecureCert* = TLSCertificate.init(SecureCert) - const WSPath* = when defined secure: "/wss" else: "/ws" proc rndStr*(size: int): string = @@ -42,8 +38,8 @@ proc waitForClose*(ws: WSSession) {.async.} = proc createServer*( address = initTAddress("127.0.0.1:8888"), - tlsPrivateKey = WSSecureKey, - tlsCertificate = WSSecureCert, + tlsPrivateKey = TLSPrivateKey.init(SecureKey), + tlsCertificate = TLSCertificate.init(SecureCert), handler: HttpAsyncCallback = nil, flags: set[ServerFlags] = {ServerFlags.TcpNoDelay, ServerFlags.ReuseAddr}, tlsFlags: set[TLSFlags] = {}, From f4e5e6f7661434e30bf642f11b997aa58794050c Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 21 Sep 2021 09:48:36 +0200 Subject: [PATCH 4/4] Fix chronos warning --- tests/helpers.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/helpers.nim b/tests/helpers.nim index fe408f428f..930bee0002 100644 --- a/tests/helpers.nim +++ b/tests/helpers.nim @@ -69,7 +69,7 @@ proc createServer*( except TransportOsError as exc: error "Transport error", exc = exc.msg - asyncCheck accepts() + asyncSpawn accepts() else: server.handler = handler server.start()