Skip to content

Instantly share code, notes, and snippets.

@mccraigmccraig
Created March 31, 2020 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccraigmccraig/ce81f2dca70414ade20352895b17d462 to your computer and use it in GitHub Desktop.
Save mccraigmccraig/ce81f2dca70414ade20352895b17d462 to your computer and use it in GitHub Desktop.
(ns er-model.util.timeout-test
(:require
#?(:clj [prpr.test :refer [deftest test-async is testing]])
#?(:cljs [prpr.test :refer-macros [deftest test-async is testing]])
[taoensso.timbre :refer [debug info warn]]
[prpr.promise :as promise :refer [ddo]]
[cats.core :refer [return]]
[er-model.util.timeout :as sut]))
(deftest singleton-timeout-test
(test-async
(testing "executes a timeout"
(ddo [:let [calls (atom #{})]
r (promise/factory-pr
(fn [resolve reject]
(sut/singleton-timeout
:foo1
(fn []
(swap! calls conj "foo1")
(resolve "foo1"))
10)))]
(return
(do
(is (= @calls #{"foo1"}))
(is (= r "foo1"))))))
(testing "executes only last registered timeout for some key"
(ddo [:let [calls (atom #{})]
r (promise/factory-pr
(fn [resolve reject]
(sut/singleton-timeout
:foo2
(fn []
(swap! calls conj "foo2-1")
(resolve "foo2-1"))
10)
(sut/singleton-timeout
:foo2
(fn []
(swap! calls conj "foo2-2")
(resolve "foo2-2"))
10)))]
(return
(do
(is (= @calls #{"foo2-2"}))
(is (= r "foo2-2"))))))
(testing "executes multiple timeouts with different keys"
(ddo [:let [calls (atom #{})]
foo-r (promise/factory-pr
(fn [resolve reject]
(sut/singleton-timeout
:foo3
(fn []
(swap! calls conj "foo3")
(resolve "foo3"))
10)))
bar-r (promise/factory-pr
(fn [resolve reject]
(sut/singleton-timeout
:bar1
(fn []
(swap! calls conj "bar1")
(resolve "bar1"))
10)))]
(return
(do
(is (= @calls #{"foo3" "bar1"}))
(is (= foo-r "foo3"))
(is (= bar-r "bar1"))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment