-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |