nim-chronos/tests/testbugs.nim

46 lines
1.4 KiB
Nim
Raw Normal View History

# Chronos Test Suite
# (c) Copyright 2018-Present
2018-08-06 18:13:44 +00:00
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
import unittest
import ../chronos
2018-08-06 18:13:44 +00:00
suite "Asynchronous issues test suite":
const HELLO_PORT = 45679
const TEST_MSG = "testmsg"
const MSG_LEN = TEST_MSG.len()
2018-08-06 18:13:44 +00:00
type
CustomData = ref object
test: string
2018-08-06 18:13:44 +00:00
proc udp4DataAvailable(transp: DatagramTransport,
remote: TransportAddress): Future[void] {.async, gcsafe.} =
var udata = getUserData[CustomData](transp)
var expect = TEST_MSG
var data: seq[byte]
var datalen: int
transp.peekMessage(data, datalen)
if udata.test == "CHECK" and datalen == MSG_LEN and
equalMem(addr data[0], addr expect[0], datalen):
udata.test = "OK"
transp.close()
2018-08-06 18:13:44 +00:00
proc issue6(): Future[bool] {.async.} =
var myself = initTAddress("127.0.0.1:" & $HELLO_PORT)
var data = CustomData()
data.test = "CHECK"
var dsock4 = newDatagramTransport(udp4DataAvailable, udata = data,
local = myself)
await dsock4.sendTo(myself, TEST_MSG, MSG_LEN)
await dsock4.join()
if data.test == "OK":
result = true
2018-08-06 18:13:44 +00:00
test "Issue #6":
var res = waitFor(issue6())
check res == true