2023-08-31 12:03:07 +00:00
|
|
|
# nim-raft
|
|
|
|
# Copyright (c) 2023 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
|
|
|
|
import ../raft/types
|
|
|
|
import basic_state_machine
|
|
|
|
|
2023-08-31 14:05:41 +00:00
|
|
|
proc basicStateMachineMain*() =
|
2023-08-31 12:03:07 +00:00
|
|
|
var
|
|
|
|
sm: RaftBasicSm
|
|
|
|
smCommandsLog: seq[SmCommand]
|
|
|
|
|
2023-08-31 14:05:41 +00:00
|
|
|
suite "Test Basic State Machine Implementation ":
|
2023-08-31 12:03:07 +00:00
|
|
|
|
|
|
|
test "Test Init":
|
|
|
|
RaftSmInit(sm)
|
|
|
|
|
|
|
|
check sm != nil and sm.state != nil and sm.state.len == 0
|
|
|
|
|
2023-08-31 16:18:29 +00:00
|
|
|
test "Init commands Log":
|
2023-08-31 12:03:07 +00:00
|
|
|
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"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "d", val: "d"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "e", val: "e"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "f", val: "f"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scDel, key: "a"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scDel, key: "a"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "a", val: "a"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scDel, key: "a"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "g", val: "g"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scDel, key: "d"))
|
|
|
|
smCommandsLog.add(SmCommand(cmd: scSet, key: "h", val: "h"))
|
|
|
|
|
|
|
|
check smCommandsLog.len == 13
|
|
|
|
|
2023-08-31 16:18:29 +00:00
|
|
|
test "Apply commands from the Log and check result":
|
2023-08-31 12:03:07 +00:00
|
|
|
for c in smCommandsLog:
|
|
|
|
RaftSmApply(sm, c)
|
|
|
|
|
|
|
|
check sm.state[] == {"b": "b", "c": "c", "e": "e", "f": "f", "g": "g", "h": "h"}.toTable
|
|
|
|
|
|
|
|
if isMainModule:
|
2023-08-31 14:05:41 +00:00
|
|
|
basicStateMachineMain()
|