Skip to content

Commit

Permalink
add related test
Browse files Browse the repository at this point in the history
  • Loading branch information
windmgc committed Feb 27, 2023
1 parent 538ec3f commit 0b431f9
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions spec/02-integration/03-db/20-ttl-cleanup_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
local helpers = require "spec.helpers"

for _, strategy in helpers.each_strategy() do
local postgres_only = strategy == "postgres" and describe or pending
postgres_only("postgres ttl cleanup logic", function()
describe("cleanup_expired_rows_in_table function", function ()
local bp, db, consumer1
lazy_setup(function()
bp, db = helpers.get_db_utils("postgres", {
"consumers",
"keyauth_credentials"
})

consumer1 = bp.consumers:insert {
username = "conumer1"
}
end)

lazy_teardown(function()
db:truncate()
db:close()
end)

it("cleanup expired rows should work as expected #test", function ()
local cleanup_func = db.connector._cleanup_expired_rows_in_table

local kauth_cred = bp.keyauth_credentials:insert({
key = "secret1",
consumer = { id = consumer1.id },
}, {ttl = 1})

local ok, err = db.connector:query("SELECT * FROM keyauth_credentials")
assert.is_nil(err)
assert.same(1, #ok)
helpers.wait_until(function()
-- wait until the keyauth credential expired
local kauth_cred2 = db.keyauth_credentials:select{id = kauth_cred.id}
return kauth_cred2 == nil
end, 3)
cleanup_func(db.connector.config, "keyauth_credentials")
ok, err = db.connector:query("SELECT * FROM keyauth_credentials")
assert.is_nil(err)
assert.same(0, #ok)
end)
end)

describe("ttl cleanup timer #postgres", function()
local bp, db, consumer1
lazy_setup(function()
bp, db = helpers.get_db_utils("postgres", {
"routes",
"services",
"plugins",
"consumers",
"keyauth_credentials"
})

consumer1 = bp.consumers:insert {
username = "conumer1"
}

assert(helpers.start_kong({
pg_expired_rows_cleanup_interval = 10,
database = strategy,
}))
end)

lazy_teardown(function()
helpers.stop_kong()
db:truncate()
end)

it("init_worker should run ttl cleanup in background timer", function ()
helpers.clean_logfile()
local names_of_table_with_ttl = db.connector._get_topologically_sorted_table_names(db.strategies)
assert.truthy(#names_of_table_with_ttl > 0)
for _, name in ipairs(names_of_table_with_ttl) do
assert.errlog().has.line([[cleaning up expired rows from table ']] .. name .. [[' took \d+\.\d+ seconds]], false, 60)
end

local _ = bp.keyauth_credentials:insert({
key = "secret1",
consumer = { id = consumer1.id },
}, {ttl = 3})
helpers.clean_logfile()

helpers.wait_until(function()
return assert.errlog().has.line([[cleaning up expired rows from table ']] .. "keyauth_credentials" .. [[' took \d+\.\d+ seconds]], false, 12)
end, 20)

local ok, err = db.connector:query("SELECT * FROM keyauth_credentials")
assert.is_nil(err)
assert.same(0, #ok)
end)
end)
end)
end

0 comments on commit 0b431f9

Please sign in to comment.