summaryrefslogtreecommitdiffstats
path: root/tests/config/cache.test.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
commit830407e88f9d40d954356c3754f2647f91d5c06a (patch)
treed6a0ece6feea91f3c656166dbaa884ef8a29740e /tests/config/cache.test.lua
parentInitial commit. (diff)
downloadknot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.tar.xz
knot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.zip
Adding upstream version 5.6.0.upstream/5.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/config/cache.test.lua67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/config/cache.test.lua b/tests/config/cache.test.lua
new file mode 100644
index 0000000..33b87db
--- /dev/null
+++ b/tests/config/cache.test.lua
@@ -0,0 +1,67 @@
+-- SPDX-License-Identifier: GPL-3.0-or-later
+-- test if cache module properties work
+local function test_properties()
+ is(type(cache), 'table', 'cache module is loaded')
+ is(cache.count(), 0, 'cache is empty on startup')
+ local backends = cache.backends()
+ is(type(backends), 'table', 'cache provides a list of backends')
+ ok(backends['lmdb'], 'cache provides built-in lmdb backend')
+ is(cache.current_storage, 'lmdb://', 'cache starts with lmdb backend')
+ is(cache.current_size, 100 * MB, 'cache starts with default size limit')
+ is(cache.max_ttl(10), 10, 'allows setting maximum TTL')
+ is(cache.max_ttl(), 10, 'stored maximum TTL')
+ is(cache.min_ttl(1), 1, 'allows setting minimum TTL')
+ is(cache.min_ttl(), 1, 'stored minimum TTL')
+end
+
+-- test if the stats work with reopening the cache and operations fail with closed cache
+local function test_stats()
+ ok(cache.close(), 'cache can be closed')
+ boom(cache.open, {100 * MB, 'invalid://'}, 'cache cannot be opened with invalid backend')
+
+ boom(cache.clear, {}, '.clear() does not work on closed cache')
+ boom(cache.count, {}, '.count() does not work on closed cache')
+ boom(cache.get, { 'key' }, '.get(...) does not work on closed cache')
+
+ ok(cache.open(100 * MB), 'cache can be reopened')
+ local s = cache.stats()
+ is(type(s), 'table', 'stats returns a table')
+ -- Just checking the most useful fields
+ isnt(s.read and s.read_miss and s.write, nil, 'stats returns correct fields')
+end
+
+-- test if cache can be resized or shrunk
+local function test_resize()
+ ok(cache.open(200 * MB, 'lmdb://'), 'cache can be resized')
+ is(cache.current_size, 200 * MB, 'cache was resized')
+ ok(cache.open(50 * MB), 'cache can be shrunk')
+ is(cache.current_size, 50 * MB, 'cache was shrunk')
+end
+
+-- test access to cache through context
+local function test_context_cache()
+ local c = kres.context().cache
+ is(type(c), 'cdata', 'context has a cache object')
+ local s = c.stats
+ isnt(s.read and s.read_miss and s.write, 'context cache stats works')
+ -- insert A record into cache
+ local rdata = '\1\2\3\4'
+ local rr = kres.rrset('\3com\0', kres.type.A, kres.class.IN, 66)
+ rr:add_rdata(rdata, #rdata)
+ local s_write = s.write
+ ok(c:insert(rr, nil, 0, 0), 'cache insertion works (A)')
+ ok(c:commit(), 'cache commit works')
+ isnt(s.write, s_write, 'cache insertion increments counters')
+ -- insert NS record into cache
+ local rr_ns = kres.rrset('\3com\0', kres.type.NS, kres.class.IN, 66)
+ local rdata_ns = todname('c.gtld-servers.net')
+ ok(rr_ns:add_rdata(rdata_ns, #rdata_ns), 'adding rdata works')
+ ok(c:insert(rr_ns, nil, 0), 'cache insertion works (NS)')
+end
+
+return {
+ test_properties,
+ test_stats,
+ test_resize,
+ test_context_cache,
+}