annotate tests/test_storage.nim @ 1098:45cf94a6535c

did: undo complicated background storage API, sync is good enough for now
author sam <sam@basx.dev>
date Sun, 07 Apr 2024 21:56:43 +0700
parents bc3efccc2bf4
children 1705e005cdee
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
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
2 import std/strformat
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
3
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
5
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
6 proc testSimple(storage: StorageType) =
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
7 const TEST_VALUE = 42
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
8 const KEY = "test"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
9
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
10 # get default
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
11 assert load[int](storage, KEY) == default(type(TEST_VALUE))
1095
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
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
14 store(storage, KEY, TEST_VALUE)
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
15 assert load[int](storage, KEY) == TEST_VALUE
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
16
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
17 proc stressTest(storage: StorageType) =
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
18 for i in 1 .. 10000:
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
19 let key = &"key-{i}"
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
20 store(storage, key, i)
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
21 assert load[int](storage, key) == i
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
22
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
23 proc main() =
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
24 SystemStorage.purge()
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
25 echo "SystemStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
26 SystemStorage.testSimple()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
27
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
28 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
29 echo "UserStorage: Testing simple store/load"
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
30 UserStorage.testSimple()
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
31
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
32 echo "Stress test with 10'000 saves/loads"
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
33 SystemStorage.stressTest()
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
34
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
35 SystemStorage.purge()
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
36 UserStorage.purge()
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
37
1096
9809bd9e2cdb add: tests and did some simplification of code
sam <sam@basx.dev>
parents: 1095
diff changeset
38
1095
b6427706c7b1 add: final (for now) storage API
sam <sam@basx.dev>
parents:
diff changeset
39 when isMainModule:
1098
45cf94a6535c did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents: 1097
diff changeset
40 main()