11BEGIN ;
22-- Plan the tests.
3- SELECT plan(3 );
3+ SELECT plan(5 );
44
5- INSERT INTO rate_limit .sessions (id, name_, type_)
5+ INSERT INTO rate_limit .sessions (id, name_, type_, expires_at )
66SELECT
77 ' 00000000-0000-0000-0000-000000000000' ::uuid AS id,
88 ' dedicated-test' ::text AS name_,
9- ' aggregated' ::text AS type_
9+ ' aggregated' ::text AS type_,
10+ ' 2023-01-01 10:00:00+0' ::timestamptz AS expires_at
1011UNION
1112SELECT
1213 ' 00000000-1111-1111-1111-000000000000' ::uuid AS id,
1314 ' dedicated-test-2' ::text AS name_,
14- ' aggregated' ::text AS type_;;
15+ ' aggregated' ::text AS type_,
16+ ' 2023-01-01 10:00:00+0' AS expires_at
17+ UNION
18+ SELECT
19+ ' 00000000-2222-2222-2222-000000000000' ::uuid AS id,
20+ ' dedicated-test-expired' ::text AS name_,
21+ ' aggregated' ::text AS type_,
22+ ' 2023-01-01 08:00:00+0' AS expires_at;
1523
1624INSERT INTO rate_limit .records_aggregated (key, session_id)
1725SELECT
@@ -20,34 +28,59 @@ SELECT
2028
2129SELECT results_eq(
2230 $have$
23- SELECT agg_increment AS count FROM rate_limit .agg_increment (' new-key' , ' 00000000-0000-0000-0000-000000000000 ' )
31+ SELECT * FROM rate_limit .agg_increment (' new-key' , ' dedicated-test ' , 1000 , ' 2023-01-01 09:00:00+0 ' ) AS (count int , expires_at timestamptz );
2432 $have$,
2533 $want$
26- SELECT 1 ::int AS count;
34+ SELECT 1 ::int AS count, ' 2023-01-01 10:00:00+0 ' :: timestamptz as expires_at ;
2735 $want$,
2836 ' rate_limit.agg_increment returns correct count for new key'
2937);
3038
3139SELECT results_eq(
3240 $have$
33- SELECT agg_increment AS count FROM rate_limit .agg_increment (' new-key' , ' 00000000-1111-1111-1111-000000000000 ' )
41+ SELECT * FROM rate_limit .agg_increment (' new-key' , ' dedicated-test-2 ' , 1000 , ' 2023-01-01 09:00:00+0 ' ) AS (count int , expires_at timestamptz );
3442 $have$,
3543 $want$
36- SELECT 1 ::int AS count;
44+ SELECT 1 ::int AS count, ' 2023-01-01 10:00:00+0 ' :: timestamptz as expires_at ;
3745 $want$,
3846 ' rate_limit.agg_increment returns correct count for new key on different session'
3947);
4048
4149SELECT results_eq(
4250 $have$
43- SELECT agg_increment AS count FROM rate_limit .agg_increment (' existing-key' , ' 00000000-0000-0000-0000-000000000000 ' )
51+ SELECT * FROM rate_limit .agg_increment (' existing-key' , ' dedicated-test ' , 1000 , ' 2023-01-01 09:00:00+0 ' ) AS (count int , expires_at timestamptz );
4452 $have$,
4553 $want$
46- SELECT 2 ::int AS count;
54+ SELECT 2 ::int AS count, ' 2023-01-01 10:00:00+0 ' :: timestamptz as expires_at ;
4755 $want$,
4856 ' rate_limit.agg_increment returns correct count for existing key'
4957);
5058
59+ SELECT results_eq(
60+ $have$
61+ SELECT * FROM rate_limit .agg_increment (' existing-key' , ' dedicated-test-expired' , 1000 , ' 2023-01-01 09:00:00+0' ) AS (count int , expires_at timestamptz );
62+ $have$,
63+ $want$
64+ SELECT 1 ::int AS count, ' 2023-01-01 09:00:01+0' ::timestamptz as expires_at;
65+ $want$,
66+ ' rate_limit.agg_increment returns correct count for existing key for expired session'
67+ );
68+
69+ SELECT bag_hasnt(
70+ $have$
71+ SELECT id, name_, type_, expires_at FROM rate_limit .sessions
72+ $have$,
73+ $miss$
74+ SELECT
75+ ' 00000000-2222-2222-2222-000000000000' ::uuid AS id,
76+ ' dedicated-test-expired' ::text AS name_,
77+ ' aggregated' ::text AS type_,
78+ ' 2023-01-01 08:00:00+0' ::timestamptz as expires_at;
79+ $miss$,
80+ ' expired session should not be reset after increment invoke'
81+ );
82+
83+
5184-- Finish the tests and clean up.
5285SELECT finish FROM finish();
5386ROLLBACK ;
0 commit comments