comparison 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
comparison
equal deleted inserted replaced
1095:b6427706c7b1 1096:9809bd9e2cdb
1 import std/os 1 import std/os
2 import std/strformat
2 3
3 import semicongine 4 import semicongine
4 5
5 proc testSimple(storage: StorageType) = 6 proc testSimple(storage: StorageType) =
6 const TEST_VALUE = 42 7 const TEST_VALUE = 42
7 const KEY = "test" 8 const KEY = "test"
8 9
9 # get default 10 # get default
10 var promise1 = load[int](storage, KEY) 11 var future1 = load[int](storage, KEY)
11 assert promise1.awaitResult() == default(type(TEST_VALUE)) 12 assert future1.awaitResult() == default(type(TEST_VALUE))
12 13
13 # save and load custom 14 # save and load custom
14 var promise2 = store(storage, KEY, TEST_VALUE) 15 var future2 = store(storage, KEY, TEST_VALUE)
15 promise2.awaitStored() 16 future2.awaitStored()
16 promise1 = load[int](storage, KEY) 17 future1 = load[int](storage, KEY)
17 assert promise1.awaitResult() == TEST_VALUE 18 assert future1.awaitResult() == TEST_VALUE
18 19
19 proc testBusyWait(storage: StorageType) = 20 proc testBusyWait(storage: StorageType) =
20 const TEST_VALUE = "43" 21 const TEST_VALUE = "43"
21 const KEY = "test2" 22 const KEY = "test2"
22 23
23 # get default 24 # get default
24 var promise1 = load[string](storage, KEY) 25 var future1 = load[string](storage, KEY)
25 while not promise1.hasResult(): 26 while not future1.hasResult():
26 sleep(1) 27 sleep(1)
27 assert promise1.getResult() == default(type(TEST_VALUE)) 28 assert future1.getResult() == default(type(TEST_VALUE))
28 29
29 # save and load custom 30 # save and load custom
30 var promise2 = store(storage, KEY, TEST_VALUE) 31 var future2 = store(storage, KEY, TEST_VALUE)
31 while not promise2.isStored(): 32 while not future2.isStored():
32 sleep(1) 33 sleep(1)
33 promise1 = load[string](storage, KEY) 34 future1 = load[string](storage, KEY)
34 while not promise1.hasResult(): 35 while not future1.hasResult():
35 sleep(1) 36 sleep(1)
36 assert promise1.awaitResult() == TEST_VALUE 37 assert future1.awaitResult() == TEST_VALUE
38
39 proc stressTest(storage: StorageType) =
40 for i in 1 .. 10000:
41 let key = &"key-{i}"
42 var p = store(storage, key, i)
43 p.awaitStored()
44 var p1 = load[int](storage, key)
45 assert p1.awaitResult() == i
46
47 proc concurrentStressTest(storage: StorageType) =
48 var storeFutures: seq[StoreFuture[int]]
49
50 for i in 1 .. 10000:
51 let key = &"key-{i}"
52 echo key
53 storeFutures.add store(storage, key, i)
54
55 for i in 1 .. 10000:
56 echo i
57 let key = &"key-{i}"
58 storeFutures[i - 1].awaitStored()
59 var p1 = load[int](storage, key)
60 assert p1.awaitResult() == i
37 61
38 proc main() = 62 proc main() =
63 SystemStorage.purge()
39 echo "SystemStorage: Testing simple store/load" 64 echo "SystemStorage: Testing simple store/load"
40 SystemStorage.testSimple() 65 SystemStorage.testSimple()
41 echo "SystemStorage: Testing store/load with busy wait" 66 echo "SystemStorage: Testing store/load with busy wait"
42 SystemStorage.testBusyWait() 67 SystemStorage.testBusyWait()
43 68
44 UserStorage.purge() 69 UserStorage.purge()
45 echo "UserStorage: Testing simple store/load" 70 echo "UserStorage: Testing simple store/load"
46 UserStorage.testSimple() 71 UserStorage.testSimple()
47 echo "UserStorage: Testing store/load with busy wait" 72 echo "UserStorage: Testing store/load with busy wait"
48 UserStorage.testBusyWait() 73 UserStorage.testBusyWait()
74
75 echo "Stress test with 10'000 saves/loads"
76 SystemStorage.stressTest()
77
78 SystemStorage.purge()
49 UserStorage.purge() 79 UserStorage.purge()
80
81 # TODO: fails currently, but is likely not too important
82 # echo "Stress test with 10'000 saves/loads and a little concurrency"
83 # SystemStorage.concurrentStressTest()
50 84
51 when isMainModule: 85 when isMainModule:
52 main() 86 main()