annotate tests/test_storage.nim @ 986:5db7721395fc

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