update tests

This commit is contained in:
Gered 2022-01-02 15:09:25 -05:00
parent 1fd154c4f6
commit 257c8caf10
2 changed files with 90 additions and 38 deletions

View file

@ -7,42 +7,56 @@
(deftest session-expiry (deftest session-expiry
(testing "Test session expiry." (testing "Test session expiry."
; store where entries should expire after 1 second
(let [as (aging-memory-store :events [(event/expires-after 1)])] (let [as (aging-memory-store :events [(event/expires-after 1)])]
(write-session as "mykey" {:foo 1}) (write-session as "mykey" {:foo 1})
(. Thread (sleep 1500)) (is (= (read-session as "mykey") {:foo 1})
(is (nil? (read-session as "mykey")))))) "session entry was written")
(Thread/sleep 1500)
(is (nil? (read-session as "mykey"))
"session entry should no longer be present"))))
(deftest session-expiry-by-sweep (deftest session-expiry-by-sweep
(testing "Test session expiry sweep." (testing "Test session expiry sweep."
(let [as (aging-memory-store (let [as (aging-memory-store
:events [(event/expires-after 1)] :events [(event/expires-after 1)] ; expire after 1 second
:sweep-every 5 :sweep-every 5 ; only trigger sweep after 5 writes
:sweep-delay 1000)] :sweep-delay 1000 ; sweep thread tries to run every 1 second
)]
(write-session as "mykey" {:foo 1}) (write-session as "mykey" {:foo 1})
(. Thread (sleep 1500)) (Thread/sleep 1500)
; key should still exist, even though it's expired ; key should still exist, even though it's expired (not enough writes have occurred)
(is (not (nil? (read-timestamp as "mykey")))) (is (integer? (read-timestamp as "mykey"))
"session entry should still be present even though it has expired")
; key should exist for three more writes ; key should exist for three more writes
(write-session as "other-key" {:foo 1}) (write-session as "other-key" {:foo 1})
(is (not (nil? (read-timestamp as "mykey")))) (is (integer? (read-timestamp as "mykey"))
"session entry should still be present even though it has expired")
(write-session as "other-key" {:foo 1}) (write-session as "other-key" {:foo 1})
(is (not (nil? (read-timestamp as "mykey")))) (is (integer? (read-timestamp as "mykey"))
"session entry should still be present even though it has expired")
(write-session as "other-key" {:foo 1}) (write-session as "other-key" {:foo 1})
(is (not (nil? (read-timestamp as "mykey")))) (is (integer? (read-timestamp as "mykey"))
"session entry should still be present even though it has expired")
; on the fifth write and after 30s, key should not exist ; on the fifth write and after 1 second, key should not exist
(write-session as "other-key" {:foo 1}) (write-session as "other-key" {:foo 1})
(. Thread (sleep 2000)) (Thread/sleep 2000) ; allow time for sweeper thread to run
(is (nil? (read-timestamp as "mykey")))))) (is (nil? (read-timestamp as "mykey"))
"session entry should have been removed now"))))
(deftest refresh-on-read-nonexistant-key-then-sweep (deftest refresh-on-read-nonexistant-key-then-sweep
(testing "Test an empty session read (with refresh-on-read enabled) then check that the expiry sweep still works" (testing "Test an empty session read (with refresh-on-read enabled) then check that the expiry sweep still works"
(let [as (aging-memory-store (let [as (aging-memory-store
:events [(event/expires-after 1)] :events [(event/expires-after 1)] ; expire after 1 second
:refresh-on-read true :refresh-on-read true
:sweep-every 1 :sweep-every 1 ; sweep runs after every write
:sweep-delay 1000)] :sweep-delay 1000) ; sweep thread tries to run every 1 second
(is (nil? (read-session as "foo"))) ]
(is (nil? (read-session as "foo"))
"no session entry present for this key")
(Thread/sleep 1500)
; read again to trigger the sweep ; read again to trigger the sweep
(is (nil? (read-session as "foo")))))) (is (nil? (read-session as "foo"))
"still no session entry present for this key"))))

View file

@ -5,51 +5,89 @@
[aging-session.memory :refer :all])) [aging-session.memory :refer :all]))
(deftest basic-read-empty (deftest basic-read-empty
(testing "Test session reads." (testing "Test session reads when there is no session value for that key."
(let [as (aging-memory-store)] (let [as (aging-memory-store)]
(is (nil? (read-session as "mykey")))))) (is (nil? (read-session as "mykey"))
"returns nil for non-existent session read"))))
(deftest basic-write (deftest basic-write
(testing "Test session writes and reads." (testing "Test session writes and reads."
(let [as (aging-memory-store)] (let [as (aging-memory-store)]
(write-session as "mykey" {:a 1}) (write-session as "mykey" {:a 1})
(is (= (read-session as "mykey") {:a 1})) (is (= (read-session as "mykey") {:a 1})
"session value was written")
(write-session as "mykey" {:a 2}) (write-session as "mykey" {:a 2})
(is (= (read-session as "mykey") {:a 2}))))) (is (= (read-session as "mykey") {:a 2})
"session value was updated"))))
(deftest basic-delete (deftest basic-delete
(testing "Test session delete." (testing "Test session delete."
(let [as (aging-memory-store)] (let [as (aging-memory-store)]
(write-session as "mykey" {:a 1}) (write-session as "mykey" {:a 1})
(is (= (read-session as "mykey") {:a 1})
"session value was written")
(delete-session as "mykey") (delete-session as "mykey")
(is (nil? (read-session as "mykey")))))) (is (nil? (read-session as "mykey"))
"session value is no longer present"))))
(deftest timestamp-on-creation (deftest timestamp-on-creation
(testing "Test the behaviour where each entry's timestamp is set only on session creation." (testing "Test the behaviour where each entry's timestamp is set only on session creation."
(let [as (aging-memory-store)] (let [as (aging-memory-store)]
(write-session as "mykey" {:foo 1}) (write-session as "mykey" {:foo 1})
(let [ts1 (read-timestamp as "mykey")] (let [ts1 (read-timestamp as "mykey")]
(is (integer? ts1)) (is (integer? ts1)
"timestamp was set on session write")
(write-session as "mykey" {:foo 2}) (write-session as "mykey" {:foo 2})
(is (= ts1 (read-timestamp as "mykey"))) (Thread/sleep 10)
(is (= (read-session as "mykey") {:foo 2})))))) (is (= ts1 (read-timestamp as "mykey"))
"timestamp is unchanged for this session entry after it was updated")
(is (= (read-session as "mykey") {:foo 2})
"updated session entry value was written successfully")))))
(deftest timestamp-on-write (deftest timestamp-on-write-only
(testing "Test the behaviour where each entry's timestamp is refreshed on write." (testing "Test the behaviour where each entry's timestamp is refreshed on write (not read)."
(let [as (aging-memory-store :refresh-on-write true)] (let [as (aging-memory-store :refresh-on-write true)]
(write-session as "mykey" {:foo 1}) (write-session as "mykey" {:foo 1})
(let [ts1 (read-timestamp as "mykey")] (let [ts1 (read-timestamp as "mykey")]
(. Thread (sleep 10)) (is (integer? ts1)
"timestamp was set on session write")
(is (= (read-session as "mykey") {:foo 1})
"session value can be read")
(Thread/sleep 10)
(is (= ts1 (read-timestamp as "mykey"))
"reading the session value did not update its timestamp")
(write-session as "mykey" {:foo 2}) (write-session as "mykey" {:foo 2})
(is (not (= ts1 (read-timestamp as "mykey")))) (Thread/sleep 10)
(is (= (read-session as "mykey") {:foo 2})))))) (is (not (= ts1 (read-timestamp as "mykey")))
"timestamp of the session entry was updated after its value was updated")
(is (= (read-session as "mykey") {:foo 2})
"session value was updated successfully")))))
(deftest timestamp-on-read (deftest timestamp-on-read-only
(testing "Test the behaviour where each entry's timestamp is refreshed on read." (testing "Test the behaviour where each entry's timestamp is refreshed on read (not write)."
(let [as (aging-memory-store :refresh-on-read true)] (let [as (aging-memory-store :refresh-on-read true)]
(write-session as "mykey" {:foo 1}) (write-session as "mykey" {:foo 1})
(let [ts1 (read-timestamp as "mykey")] (let [ts1 (read-timestamp as "mykey")]
(. Thread (sleep 10)) (is (integer? ts1)
(is (= (read-session as "mykey") {:foo 1})) "timestamp was set on session write")
(is (not (= ts1 (read-timestamp as "mykey")))) (Thread/sleep 10)
(is (= (read-session as "mykey") {:foo 1})))))) (is (= (read-session as "mykey") {:foo 1})
"session value can be read")
(let [ts2 (read-timestamp as "mykey")]
(is (not (= ts1 ts2))
"timestamp of the session entry was updated after its value was read")
(is (= (read-session as "mykey") {:foo 1})
"session value can still be read successfully")
(Thread/sleep 10)
(let [ts3 (read-timestamp as "mykey")]
(write-session as "mykey" {:foo 2})
(Thread/sleep 10)
(is (= ts3 (read-timestamp as "mykey"))
"timestamp of the session entry was not updated after its value was written")
(is (= (read-session as "mykey") {:foo 2})
"session value was updated successfully")
(Thread/sleep 10)
(is (not (= ts3 (read-timestamp as "mykey")))
"timestamp of the session entry was updated after its new value was read")))))))
#_(run-tests)