Add raft.nimble code for testing

This commit is contained in:
Raycho Mukelov 2023-08-31 19:18:29 +03:00
parent 45523fd60d
commit 79e96d40a5
3 changed files with 32 additions and 5 deletions

View File

@ -1,2 +0,0 @@
* testbasicstatemachine - 1 millisecond, 288 microseconds, and 233 nanoseconds
* testbasictimers - 980 microseconds and 832 nanoseconds

View File

@ -16,9 +16,38 @@ description = "raft consensus in nim"
license = "Apache License 2.0"
skipDirs = @["tests"]
requires "nim >= 1.6.0"
requires "nim >= 1.6.14"
requires "stew >= 0.1.0"
requires "unittest2 >= 0.0.4"
requires "uuids >= 0.1.11"
proc buildBinary(name: string, srcDir = "./", params = "", lang = "c") =
if not dirExists "build":
mkDir "build"
# allow something like "nim nimbus --verbosity:0 --hints:off nimbus.nims"
# var extra_params = params
# for i in 2..<paramCount():
# extra_params &= " " & paramStr(i)
exec "nim " & lang & " --threads:on --out:build/" & name & " " & srcDir & name & ".nim"
proc test(path: string, name: string, params = "", lang = "c") =
# Verify stack usage is kept low by setting 750k stack limit in tests.
const stackLimitKiB = 750
when not defined(windows):
const (buildOption, runPrefix) = ("", "ulimit -s " & $stackLimitKiB & " && ")
else:
# No `ulimit` in Windows. `ulimit -s` in Bash is accepted but has no effect.
# See https://public-inbox.org/git/alpine.DEB.2.21.1.1709131448390.4132@virtualbox/
# Also, the command passed to NimScript `exec` on Windows is not a shell script.
# Instead, we can set stack size at link time.
const (buildOption, runPrefix) =
(" -d:windowsNoSetStack --passL:-Wl,--stack," & $(stackLimitKiB * 2048), "")
buildBinary name, (path & "/"), params & buildOption
exec runPrefix & "build/" & name
task test, "Run tests":
test "tests", "all_tests", "-d:chronicles_log_level=ERROR -d:unittest2DisableParamFiltering"
# Helper functions

View File

@ -23,7 +23,7 @@ proc basicStateMachineMain*() =
check sm != nil and sm.state != nil and sm.state.len == 0
test "Init commands":
test "Init commands Log":
smCommandsLog.add(SmCommand(cmd: scSet, key: "a", val: "a"))
smCommandsLog.add(SmCommand(cmd: scSet, key: "b", val: "b"))
smCommandsLog.add(SmCommand(cmd: scSet, key: "c", val: "c"))
@ -40,7 +40,7 @@ proc basicStateMachineMain*() =
check smCommandsLog.len == 13
test "Apply commands and check result":
test "Apply commands from the Log and check result":
for c in smCommandsLog:
RaftSmApply(sm, c)