Attempt to fix CI crash at Windows. (#465)

* Attempt to fix CI crash at Windows.
Remove all cast[string] and cast[seq[byte]] from the codebase.

* Address review comments.
This commit is contained in:
Eugene Kabanov 2023-11-13 13:14:21 +02:00 committed by GitHub
parent 0d55475c29
commit 9c93ab48de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 53 deletions

View File

@ -1125,14 +1125,18 @@ proc addTimer*(at: uint64, cb: CallbackFunc, udata: pointer = nil) {.
proc removeTimer*(at: Moment, cb: CallbackFunc, udata: pointer = nil) =
## Remove timer callback ``cb`` with absolute timestamp ``at`` from waiting
## queue.
let loop = getThreadDispatcher()
var list = cast[seq[TimerCallback]](loop.timers)
var index = -1
for i in 0..<len(list):
if list[i].finishAt == at and list[i].function.function == cb and
list[i].function.udata == udata:
index = i
break
let
loop = getThreadDispatcher()
index =
block:
var res = -1
for i in 0 ..< len(loop.timers):
if (loop.timers[i].finishAt == at) and
(loop.timers[i].function.function == cb) and
(loop.timers[i].function.udata == udata):
res = i
break
res
if index != -1:
loop.timers.del(index)

View File

@ -7,6 +7,7 @@
# MIT license (LICENSE-MIT)
import unittest2
import bearssl/[x509]
import stew/byteutils
import ".."/chronos/unittest2/asynctests
import ".."/chronos/streams/[tlsstream, chunkstream, boundstream]
@ -102,11 +103,11 @@ suite "AsyncStream test suite":
var transp = await connect(server.localAddress())
var rstream = newAsyncStreamReader(transp)
await rstream.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "0000000000"
check string.fromBytes(buffer) == "0000000000"
await rstream.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "1111111111"
check string.fromBytes(buffer) == "1111111111"
await rstream.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "2222222222"
check string.fromBytes(buffer) == "2222222222"
await rstream.closeWait()
await transp.closeWait()
await server.join()
@ -135,15 +136,15 @@ suite "AsyncStream test suite":
var r1 = await rstream.readUntil(addr buffer[0], len(buffer), sep)
check:
r1 == 13
cast[string](buffer) == "0000000000NNz"
string.fromBytes(buffer) == "0000000000NNz"
var r2 = await rstream.readUntil(addr buffer[0], len(buffer), sep)
check:
r2 == 13
cast[string](buffer) == "1111111111NNz"
string.fromBytes(buffer) == "1111111111NNz"
var r3 = await rstream.readUntil(addr buffer[0], len(buffer), sep)
check:
r3 == 13
cast[string](buffer) == "2222222222NNz"
string.fromBytes(buffer) == "2222222222NNz"
await rstream.closeWait()
await transp.closeWait()
@ -198,9 +199,9 @@ suite "AsyncStream test suite":
var transp = await connect(server.localAddress())
var rstream = newAsyncStreamReader(transp)
var buf1 = await rstream.read(10)
check cast[string](buf1) == "0000000000"
check string.fromBytes(buf1) == "0000000000"
var buf2 = await rstream.read()
check cast[string](buf2) == "11111111112222222222"
check string.fromBytes(buf2) == "11111111112222222222"
await rstream.closeWait()
await transp.closeWait()
await server.join()
@ -228,12 +229,12 @@ suite "AsyncStream test suite":
check:
res1 == 10
var buf1 = await rstream.read(10)
check cast[string](buf1) == "1111111111"
check string.fromBytes(buf1) == "1111111111"
var res2 = await rstream.consume(10)
check:
res2 == 10
var buf2 = await rstream.read(10)
check cast[string](buf2) == "3333333333"
check string.fromBytes(buf2) == "3333333333"
await rstream.closeWait()
await transp.closeWait()
await server.join()
@ -255,7 +256,7 @@ suite "AsyncStream test suite":
await wstream2.write("00000")
await wstream2.write(addr s1[0], len(s1))
await wstream2.write("11111")
await wstream2.write(cast[seq[byte]](s2))
await wstream2.write(s2.toBytes())
await wstream2.write("22222")
await wstream2.write(addr s3[0], len(s3))
@ -275,11 +276,11 @@ suite "AsyncStream test suite":
var rstream = newAsyncStreamReader(transp)
var rstream2 = newChunkedStreamReader(rstream)
await rstream2.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "0000000000"
check string.fromBytes(buffer) == "0000000000"
await rstream2.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "1111111111"
check string.fromBytes(buffer) == "1111111111"
await rstream2.readExactly(addr buffer[0], 10)
check cast[string](buffer) == "2222222222"
check string.fromBytes(buffer) == "2222222222"
# We need to consume all the stream with finish markers, but there will
# be no actual data.
@ -309,7 +310,7 @@ suite "AsyncStream test suite":
await wstream2.write("11111")
await wstream2.write(s2)
await wstream2.write("22222")
await wstream2.write(cast[seq[byte]](s3))
await wstream2.write(s3.toBytes())
await wstream2.finish()
await wstream.finish()
await wstream2.closeWait()
@ -330,15 +331,15 @@ suite "AsyncStream test suite":
var r1 = await rstream2.readUntil(addr buffer[0], len(buffer), sep)
check:
r1 == 13
cast[string](buffer) == "0000000000NNz"
string.fromBytes(buffer) == "0000000000NNz"
var r2 = await rstream2.readUntil(addr buffer[0], len(buffer), sep)
check:
r2 == 13
cast[string](buffer) == "1111111111NNz"
string.fromBytes(buffer) == "1111111111NNz"
var r3 = await rstream2.readUntil(addr buffer[0], len(buffer), sep)
check:
r3 == 13
cast[string](buffer) == "2222222222NNz"
string.fromBytes(buffer) == "2222222222NNz"
# We need to consume all the stream with finish markers, but there will
# be no actual data.
@ -411,7 +412,7 @@ suite "AsyncStream test suite":
var s3 = "2222222222"
await wstream2.write("0000000000")
await wstream2.write(s2)
await wstream2.write(cast[seq[byte]](s3))
await wstream2.write(s3.toBytes())
await wstream2.finish()
await wstream.finish()
await wstream2.closeWait()
@ -427,9 +428,9 @@ suite "AsyncStream test suite":
var rstream = newAsyncStreamReader(transp)
var rstream2 = newChunkedStreamReader(rstream)
var buf1 = await rstream2.read(10)
check cast[string](buf1) == "0000000000"
check string.fromBytes(buf1) == "0000000000"
var buf2 = await rstream2.read()
check cast[string](buf2) == "11111111112222222222"
check string.fromBytes(buf2) == "11111111112222222222"
# read() call will consume all the bytes and finish markers too, so
# we just check stream for EOF.
@ -452,7 +453,7 @@ suite "AsyncStream test suite":
var wstream2 = newChunkedStreamWriter(wstream)
var s1 = "00000"
var s2 = cast[seq[byte]]("11111")
var s2 = "11111".toBytes()
var s3 = "22222"
await wstream2.write("00000")
@ -482,12 +483,12 @@ suite "AsyncStream test suite":
check:
res1 == 10
var buf1 = await rstream2.read(10)
check cast[string](buf1) == "1111111111"
check string.fromBytes(buf1) == "1111111111"
var res2 = await rstream2.consume(10)
check:
res2 == 10
var buf2 = await rstream2.read(10)
check cast[string](buf2) == "3333333333"
check string.fromBytes(buf2) == "3333333333"
# We need to consume all the stream with finish markers, but there will
# be no actual data.
@ -596,7 +597,7 @@ suite "ChunkedStream test suite":
var rstream = newAsyncStreamReader(transp)
var rstream2 = newChunkedStreamReader(rstream)
var res = await rstream2.read()
var ress = cast[string](res)
var ress = string.fromBytes(res)
await rstream2.closeWait()
await rstream.closeWait()
await transp.closeWait()
@ -917,7 +918,7 @@ suite "TLSStream test suite":
await cwriter.closeWait()
await conn.closeWait()
await server.join()
return cast[string](res) == (testMessage & "\r\n")
return string.fromBytes(res) == (testMessage & "\r\n")
test "Simple server with RSA self-signed certificate":
let res = waitFor(checkSSLServer(SelfSignedRsaKey, SelfSignedRsaCert))
@ -961,7 +962,7 @@ suite "TLSStream test suite":
await cwriter.closeWait()
await conn.closeWait()
await server.join()
return cast[string](res)
return string.fromBytes(res)
let res = waitFor checkTrustAnchors("Some message")
check res == "Some message\r\n"

View File

@ -6,6 +6,7 @@
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
import std/[strutils, net]
import stew/byteutils
import ".."/chronos/unittest2/asynctests
import ".."/chronos
@ -474,7 +475,7 @@ suite "Datagram Transport test suite":
proc clientMark(transp: DatagramTransport,
raddr: TransportAddress): Future[void] {.async.} =
var bmsg = transp.getMessage()
var smsg = cast[string](bmsg)
var smsg = string.fromBytes(bmsg)
if smsg == expectMessage:
inc(res)
transp.close()
@ -486,7 +487,7 @@ suite "Datagram Transport test suite":
proc testAnyAddress(): Future[int] {.async.} =
var expectStr = "ANYADDRESS MESSAGE"
var expectSeq = cast[seq[byte]](expectStr)
var expectSeq = expectStr.toBytes()
let ta = initTAddress("0.0.0.0:0")
var res = 0
var event = newAsyncEvent()
@ -494,7 +495,7 @@ suite "Datagram Transport test suite":
proc clientMark1(transp: DatagramTransport,
raddr: TransportAddress): Future[void] {.async.} =
var bmsg = transp.getMessage()
var smsg = cast[string](bmsg)
var smsg = string.fromBytes(bmsg)
if smsg == expectStr:
inc(res)
event.fire()
@ -545,7 +546,7 @@ suite "Datagram Transport test suite":
proc process1(transp: DatagramTransport,
raddr: TransportAddress): Future[void] {.async.} =
var bmsg = transp.getMessage()
var smsg = cast[string](bmsg)
var smsg = string.fromBytes(bmsg)
if smsg == expectStr:
inc(res)
event.fire()

View File

@ -9,7 +9,7 @@ import std/[strutils, sha1]
import ".."/chronos/unittest2/asynctests
import ".."/chronos,
".."/chronos/apps/http/[httpserver, shttpserver, httpclient]
import stew/base10
import stew/[byteutils, base10]
{.used.}
@ -157,7 +157,7 @@ suite "HTTP client testing suite":
var req = HttpClientRequestRef.new(session, ha, item[0])
let response = await fetch(req)
if response.status == 200:
let data = cast[string](response.data)
let data = string.fromBytes(response.data)
if data == item[1]:
inc(counter)
await req.closeWait()
@ -173,7 +173,7 @@ suite "HTTP client testing suite":
var req = HttpClientRequestRef.new(session, ha, item[0])
let response = await fetch(req)
if response.status == 200:
let data = cast[string](response.data)
let data = string.fromBytes(response.data)
if data == item[1]:
inc(counter)
await req.closeWait()
@ -318,7 +318,7 @@ suite "HTTP client testing suite":
of "/test/big_request":
if request.hasBody():
let body = await request.getBody()
let digest = $secureHash(cast[string](body))
let digest = $secureHash(string.fromBytes(body))
return await request.respond(Http200, digest)
else:
return await request.respond(Http400, "Missing content body")
@ -348,7 +348,7 @@ suite "HTTP client testing suite":
session, ha, item[0], headers = headers
)
var expectDigest = $secureHash(cast[string](data))
var expectDigest = $secureHash(string.fromBytes(data))
# Sending big request by 1024bytes long chunks
var writer = await open(request)
var offset = 0
@ -364,7 +364,7 @@ suite "HTTP client testing suite":
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == expectDigest:
if string.fromBytes(res) == expectDigest:
inc(counter)
await response.closeWait()
await request.closeWait()
@ -388,7 +388,7 @@ suite "HTTP client testing suite":
of "/test/big_chunk_request":
if request.hasBody():
let body = await request.getBody()
let digest = $secureHash(cast[string](body))
let digest = $secureHash(string.fromBytes(body))
return await request.respond(Http200, digest)
else:
return await request.respond(Http400, "Missing content body")
@ -418,7 +418,7 @@ suite "HTTP client testing suite":
session, ha, item[0], headers = headers
)
var expectDigest = $secureHash(cast[string](data))
var expectDigest = $secureHash(string.fromBytes(data))
# Sending big request by 1024bytes long chunks
var writer = await open(request)
var offset = 0
@ -434,7 +434,7 @@ suite "HTTP client testing suite":
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == expectDigest:
if string.fromBytes(res) == expectDigest:
inc(counter)
await response.closeWait()
await request.closeWait()
@ -491,12 +491,12 @@ suite "HTTP client testing suite":
]
var request = HttpClientRequestRef.new(
session, ha, MethodPost, headers = headers,
body = cast[seq[byte]](PostRequests[0][1]))
body = PostRequests[0][1].toBytes())
var response = await send(request)
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == PostRequests[0][2]:
if string.fromBytes(res) == PostRequests[0][2]:
inc(counter)
await response.closeWait()
await request.closeWait()
@ -532,7 +532,7 @@ suite "HTTP client testing suite":
var response = await request.finish()
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == PostRequests[1][2]:
if string.fromBytes(res) == PostRequests[1][2]:
inc(counter)
await response.closeWait()
await request.closeWait()
@ -601,7 +601,7 @@ suite "HTTP client testing suite":
var response = await send(request)
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == PostRequests[0][3]:
if string.fromBytes(res) == PostRequests[0][3]:
inc(counter)
await response.closeWait()
await request.closeWait()
@ -634,7 +634,7 @@ suite "HTTP client testing suite":
let response = await request.finish()
if response.status == 200:
var res = await response.getBodyBytes()
if cast[string](res) == PostRequests[1][3]:
if string.fromBytes(res) == PostRequests[1][3]:
inc(counter)
await response.closeWait()
await request.closeWait()