|
| 1 | +BEGIN; |
| 2 | +-- Plan the tests. |
| 3 | +SELECT plan(5); |
| 4 | + |
| 5 | +INSERT INTO rate_limit.sessions (name_, type_) |
| 6 | +SELECT |
| 7 | + md5(random()::text) AS name_, |
| 8 | + 'individual' AS type_ |
| 9 | +FROM pg_catalog.generate_series(1, 1000); |
| 10 | + |
| 11 | +INSERT INTO rate_limit.sessions (id, name_, type_) |
| 12 | +SELECT |
| 13 | + '00000000-0000-0000-0000-000000000000'::uuid AS id, |
| 14 | + 'dedicated-test'::text AS name_, |
| 15 | + 'individual'::text AS type_; |
| 16 | + |
| 17 | +INSERT INTO rate_limit.individual_records (key, session_id) |
| 18 | +SELECT |
| 19 | + md5(random()::text) AS key_, |
| 20 | + session_.id AS session_id |
| 21 | +FROM rate_limit.sessions AS session_ |
| 22 | +INNER JOIN pg_catalog.generate_series(1, 1000) ON true; |
| 23 | + |
| 24 | +INSERT INTO rate_limit.individual_records (key, session_id) |
| 25 | +VALUES ('test-count', '00000000-0000-0000-0000-000000000000'::uuid), |
| 26 | +('test-decrement', '00000000-0000-0000-0000-000000000000'::uuid), |
| 27 | +('test-reset-key', '00000000-0000-0000-0000-000000000000'::uuid); |
| 28 | + |
| 29 | +PREPARE insert_individual_record as |
| 30 | +INSERT INTO rate_limit.individual_records (key, session_id) |
| 31 | +VALUES ('test', '00000000-0000-0000-0000-000000000000'::uuid); |
| 32 | + |
| 33 | +SELECT performs_ok( |
| 34 | + 'insert_individual_record', |
| 35 | + 250, |
| 36 | + 'inserting record should execute under 250ms' |
| 37 | +); |
| 38 | + |
| 39 | +PREPARE retrieve_count AS |
| 40 | +SELECT count(id) AS count |
| 41 | +FROM rate_limit.individual_records |
| 42 | +WHERE |
| 43 | + key = 'test-count' |
| 44 | + AND session_id = '00000000-0000-0000-0000-000000000000'::uuid; |
| 45 | + |
| 46 | +SELECT performs_ok( |
| 47 | + 'retrieve_count', |
| 48 | + 250, |
| 49 | + 'retrieving count should execute under 250ms' |
| 50 | +); |
| 51 | + |
| 52 | + |
| 53 | +PREPARE decrement_records as |
| 54 | +WITH |
| 55 | +rows_to_delete AS ( |
| 56 | + SELECT id FROM rate_limit.individual_records |
| 57 | + WHERE |
| 58 | + key = 'test-decrement' |
| 59 | + AND session_id = '00000000-0000-0000-0000-000000000000' |
| 60 | + ORDER BY event_time |
| 61 | + LIMIT 1 |
| 62 | +) |
| 63 | +DELETE FROM rate_limit.individual_records |
| 64 | +USING rows_to_delete WHERE individual_records.id = rows_to_delete.id; |
| 65 | + |
| 66 | +SELECT performs_ok( |
| 67 | + 'decrement_records', |
| 68 | + 250, |
| 69 | + 'decrementing query execute under 250ms' |
| 70 | +); |
| 71 | + |
| 72 | +PREPARE reset_key as |
| 73 | +DELETE FROM rate_limit.individual_records |
| 74 | +WHERE |
| 75 | + key = 'test-reset-key' |
| 76 | + AND session_id = '00000000-0000-0000-0000-000000000000'; |
| 77 | + |
| 78 | + |
| 79 | +SELECT performs_ok( |
| 80 | + 'reset_key', |
| 81 | + 250, |
| 82 | + 'resetting a key should execute under 250ms' |
| 83 | +); |
| 84 | + |
| 85 | +PREPARE reset_all as |
| 86 | +DELETE FROM rate_limit.individual_records |
| 87 | +WHERE session_id = '00000000-0000-0000-0000-000000000000'; |
| 88 | + |
| 89 | + |
| 90 | +SELECT performs_ok( |
| 91 | + 'reset_all', |
| 92 | + 250, |
| 93 | + 'resetting a session should execute under 250ms' |
| 94 | +); |
| 95 | + |
| 96 | +DEALLOCATE insert_individual_record; |
| 97 | +DEALLOCATE retrieve_count; |
| 98 | +DEALLOCATE decrement_records; |
| 99 | +DEALLOCATE reset_key; |
| 100 | +DEALLOCATE reset_all; |
| 101 | + |
| 102 | +-- Finish the tests and clean up. |
| 103 | +SELECT finish FROM finish(); |
| 104 | +ROLLBACK; |
0 commit comments