test/test_zache.rb in zache-0.2.0 vs test/test_zache.rb in zache-0.3.0

- old
+ new

@@ -50,6 +50,80 @@ cache = Zache.new Threads.new(10).assert(100) do cache.get(:hey, lifetime: 0.0001) { Random.rand } end end + + def test_key_exists + cache = Zache.new + cache.get(:hey) { Random.rand } + exists_result = cache.exists?(:hey) + not_exists_result = cache.exists?(:bye) + assert(exists_result == true) + assert(not_exists_result == false) + end + + def test_remove_key + cache = Zache.new + cache.get(:hey) { Random.rand } + cache.get(:wey) { Random.rand } + assert(cache.exists?(:hey) == true) + assert(cache.exists?(:wey) == true) + cache.remove(:hey) + assert(cache.exists?(:hey) == false) + assert(cache.exists?(:wey) == true) + end + + def test_remove_key_with_sync_false + cache = Zache.new(sync: false) + cache.get(:hey) { Random.rand } + cache.get(:wey) { Random.rand } + assert(cache.exists?(:hey) == true) + assert(cache.exists?(:wey) == true) + cache.remove(:hey) + assert(cache.exists?(:hey) == false) + assert(cache.exists?(:wey) == true) + end + + def test_clean_with_threads + cache = Zache.new + Threads.new(300).assert(3000) do + cache.get(:hey) { Random.rand } + cache.get(:bye, lifetime: 0.01) { Random.rand } + sleep 0.1 + cache.clean + end + assert(cache.exists?(:hey) == true) + assert(cache.exists?(:bye) == false) + end + + def test_clean + cache = Zache.new + cache.get(:hey) { Random.rand } + cache.get(:bye, lifetime: 0.01) { Random.rand } + sleep 0.1 + cache.clean + assert(cache.exists?(:hey) == true) + assert(cache.exists?(:bye) == false) + end + + def test_clean_with_sync_false + cache = Zache.new(sync: false) + cache.get(:hey) { Random.rand } + cache.get(:bye, lifetime: 0.01) { Random.rand } + sleep 0.1 + cache.clean + assert(cache.exists?(:hey) == true) + assert(cache.exists?(:bye) == false) + end + + def test_remove_absent_key + cache = Zache.new + cache.remove(:hey) + end + + def test_check_and_remove + cache = Zache.new + cache.get(:hey, lifetime: 0) { Random.rand } + assert(!cache.exists?(:hey)) + end end