Add support for scenario parameters

This commit is contained in:
Tanguy 2022-08-03 15:56:42 +02:00
parent a927e0fa15
commit 935ae9e2c4
No known key found for this signature in database
GPG Key ID: 7DD8EC6B6CE6C45E
4 changed files with 35 additions and 8 deletions

View File

@ -16,6 +16,6 @@ This is not stable in any shape or form. Features:
- [X] Network configuration
- [X] Signal, barrier
- [X] PubSub
- [ ] Run configuration
- [X] Run configuration
- [ ] Multiple scenario in a single plan
- [ ] ..

View File

@ -18,20 +18,27 @@ testground(client):
await client.waitForBarrier("network_setup", client.testInstanceCount)
const payload = "Hello playground!"
let
payload = client.param(string, "payload")
count = client.param(int, "count")
printResult = client.param(bool, "printResult")
if myId == 1: # server
let
server = createStreamServer(initTAddress(myIp & ":5050"), flags = {ReuseAddr})
connection = await server.accept()
doAssert (await connection.write(payload.toBytes())) == payload.len
for _ in 0 ..< count:
doAssert (await connection.write(payload.toBytes())) == payload.len
connection.close()
else: # client
let connection = await connect(initTAddress(serverIp & ":5050"))
var buffer: array[payload.len, byte]
var buffer = newSeq[byte](payload.len)
await connection.readExactly(addr buffer[0], payload.len)
for _ in 0 ..< count:
await connection.readExactly(addr buffer[0], payload.len)
doAssert string.fromBytes(buffer) == payload
connection.close()
doAssert string.fromBytes(buffer) == payload
client.recordMessage("Hourray " & $myId & "!")
if printResult:
client.recordMessage("Hourray " & $myId & "!")

View File

@ -12,3 +12,8 @@ enabled = true
[[testcases]]
name= "simple_tcp_ping"
instances = { min = 2, default = 2, max = 2 }
[testcases.params]
payload = { type = "string", default = "Hello!" }
count = { type = "int", default = 2 }
printResult = { type = "bool", default = true }

View File

@ -1,5 +1,5 @@
import
std/[tables, parseutils, strutils, os],
std/[tables, parseutils, strutils, os, sequtils],
chronos, websock/websock, chronicles, stew/byteutils
# workaround https://github.com/status-im/nim-serialization/issues/43
@ -7,6 +7,7 @@ from serialization import serializedFieldName
from json_serialization import Reader, init, readValue, handleReadException
import json_serialization/std/options
export sequtils, strutils, os, tables
export chronos, options, chronicles, websock
type
@ -205,6 +206,20 @@ proc publish*(c: Client, topic, content: string) {.async.} =
)
))
proc param*[T](c: Client, _: type[T], name: string): T =
let params = getEnv("TEST_INSTANCE_PARAMS").split("|").mapIt(it.split("=", 2)).mapIt((it[0], it[1])).toTable()
let param = params[name]
when T is string:
param
elif T is bool:
param == "true"
elif T is Ordinal:
parseInt(param)
else:
{.error: "Unsupported type for param".}
proc runner(todo: proc(c: Client): Future[void]) {.async.} =
let
c = Client(