annotate tests/test_storage.nim @ 1095:b6427706c7b1

add: final (for now) storage API
author sam <sam@basx.dev>
date Sun, 07 Apr 2024 00:35:45 +0700
parents
children 49d73ed5a1ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
2
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
4
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
5 proc testSimple(storage: StorageType) =
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
6 const TEST_VALUE = 42
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
7 const KEY = "test"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
8
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
9 # get default
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
10 var promise1 = load[int](storage, KEY)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
11 assert promise1.awaitResult() == default(type(TEST_VALUE))
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
12
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
13 # save and load custom
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
14 var promise2 = store(storage, KEY, TEST_VALUE)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
15 promise2.awaitStored()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
16 promise1 = load[int](storage, KEY)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
17 assert promise1.awaitResult() == TEST_VALUE
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
18
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
19 proc testBusyWait(storage: StorageType) =
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
20 const TEST_VALUE = "43"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
21 const KEY = "test2"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
22
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
23 # get default
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
24 var promise1 = load[string](storage, KEY)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
25 while not promise1.hasResult():
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
26 sleep(1)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
27 assert promise1.getResult() == default(type(TEST_VALUE))
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
28
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
29 # save and load custom
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
30 var promise2 = store(storage, KEY, TEST_VALUE)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
31 while not promise2.isStored():
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
32 sleep(1)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
33 promise1 = load[string](storage, KEY)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
34 while not promise1.hasResult():
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
35 sleep(1)
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
36 assert promise1.awaitResult() == TEST_VALUE
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
37
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
38 proc main() =
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
39 echo "SystemStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
40 SystemStorage.testSimple()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
41 echo "SystemStorage: Testing store/load with busy wait"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
42 SystemStorage.testBusyWait()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
43
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
44 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
45 echo "UserStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
46 UserStorage.testSimple()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
47 echo "UserStorage: Testing store/load with busy wait"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
48 UserStorage.testBusyWait()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
49 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
50
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
51 when isMainModule:
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
52 main()