 (def service3 nil)
 
 (defn- start-and-stop [f]
-  (alter-var-root #'service1 (fn [_] (beehive/service "1" 1 1)))
-  (alter-var-root #'service2 (fn [_] (beehive/service "2" 1 1)))
-  (alter-var-root #'service3 (fn [_] (beehive/service "3" 1 1)))
+  (alter-var-root #'service1 (fn [_] (beehive/service "1" 2 1)))
+  (alter-var-root #'service2 (fn [_] (beehive/service "2" 2 1)))
+  (alter-var-root #'service3 (fn [_] (beehive/service "3" 2 1)))
   (f)
   (service/shutdown service1)
   (service/shutdown service2)

  (:require [beehive.core :as beehive]
            [beehive.service :as service]
            [beehive.patterns :as patterns])
  (:import (java.util.concurrent CountDownLatch)))

(set! *warn-on-reflection* true)



(deftest shotgun
  (let [shotgun (patterns/shotgun {service1 {} service2 {} service3 {}} 2)
        all-done (atom (CountDownLatch. 2))
        action-blocking-latch (atom (CountDownLatch. 1))
        test-blocking-latch (atom (CountDownLatch. 1))
        counter (AtomicInteger. 0)
        action-fn (fn [_] (let [result (.incrementAndGet counter)]
                            (if (= 1 result)
                              (.await ^CountDownLatch @action-blocking-latch)
                              (.countDown ^CountDownLatch @test-blocking-latch))
                            (.countDown ^CountDownLatch @all-done)
                            result))]
    (testing "Actions submitted to multiple services"
      (.set counter 0)
      (reset! all-done (CountDownLatch. 2))
      (reset! action-blocking-latch (CountDownLatch. 1))
      (reset! test-blocking-latch (CountDownLatch. 1))
      (is (= 2 @(patterns/submit-action shotgun action-fn Long/MAX_VALUE)))
      (.countDown ^CountDownLatch @action-blocking-latch)
      (.await ^CountDownLatch @test-blocking-latch)
      (is (= 2 (.get counter)))
      (.await ^CountDownLatch @all-done))
    (testing "Nil returned if all services reject action"
      (.set counter 0)
      (reset! action-blocking-latch (CountDownLatch. 1))
      (reset! test-blocking-latch (CountDownLatch. 1))
      (let [shotgun (patterns/shotgun {service1 {}
