Mercurial > games > semicongine
diff tests/test_storage.nim @ 1096:9809bd9e2cdb
add: tests and did some simplification of code
author | sam <sam@basx.dev> |
---|---|
date | Sun, 07 Apr 2024 01:17:40 +0700 |
parents | b6427706c7b1 |
children | 46778940c1d7 |
line wrap: on
line diff
--- a/tests/test_storage.nim Sun Apr 07 00:35:45 2024 +0700 +++ b/tests/test_storage.nim Sun Apr 07 01:17:40 2024 +0700 @@ -1,4 +1,5 @@ import std/os +import std/strformat import semicongine @@ -7,35 +8,59 @@ const KEY = "test" # get default - var promise1 = load[int](storage, KEY) - assert promise1.awaitResult() == default(type(TEST_VALUE)) + var future1 = load[int](storage, KEY) + assert future1.awaitResult() == default(type(TEST_VALUE)) # save and load custom - var promise2 = store(storage, KEY, TEST_VALUE) - promise2.awaitStored() - promise1 = load[int](storage, KEY) - assert promise1.awaitResult() == TEST_VALUE + var future2 = store(storage, KEY, TEST_VALUE) + future2.awaitStored() + future1 = load[int](storage, KEY) + assert future1.awaitResult() == TEST_VALUE proc testBusyWait(storage: StorageType) = const TEST_VALUE = "43" const KEY = "test2" # get default - var promise1 = load[string](storage, KEY) - while not promise1.hasResult(): + var future1 = load[string](storage, KEY) + while not future1.hasResult(): sleep(1) - assert promise1.getResult() == default(type(TEST_VALUE)) + assert future1.getResult() == default(type(TEST_VALUE)) # save and load custom - var promise2 = store(storage, KEY, TEST_VALUE) - while not promise2.isStored(): + var future2 = store(storage, KEY, TEST_VALUE) + while not future2.isStored(): + sleep(1) + future1 = load[string](storage, KEY) + while not future1.hasResult(): sleep(1) - promise1 = load[string](storage, KEY) - while not promise1.hasResult(): - sleep(1) - assert promise1.awaitResult() == TEST_VALUE + assert future1.awaitResult() == TEST_VALUE + +proc stressTest(storage: StorageType) = + for i in 1 .. 10000: + let key = &"key-{i}" + var p = store(storage, key, i) + p.awaitStored() + var p1 = load[int](storage, key) + assert p1.awaitResult() == i + +proc concurrentStressTest(storage: StorageType) = + var storeFutures: seq[StoreFuture[int]] + + for i in 1 .. 10000: + let key = &"key-{i}" + echo key + storeFutures.add store(storage, key, i) + + for i in 1 .. 10000: + echo i + let key = &"key-{i}" + storeFutures[i - 1].awaitStored() + var p1 = load[int](storage, key) + assert p1.awaitResult() == i proc main() = + SystemStorage.purge() echo "SystemStorage: Testing simple store/load" SystemStorage.testSimple() echo "SystemStorage: Testing store/load with busy wait" @@ -46,7 +71,16 @@ UserStorage.testSimple() echo "UserStorage: Testing store/load with busy wait" UserStorage.testBusyWait() + + echo "Stress test with 10'000 saves/loads" + SystemStorage.stressTest() + + SystemStorage.purge() UserStorage.purge() + # TODO: fails currently, but is likely not too important + # echo "Stress test with 10'000 saves/loads and a little concurrency" + # SystemStorage.concurrentStressTest() + when isMainModule: main()