   (let [inventory-response (promise)
         inventory-update (atom (promise))]
     (with-redefs [puppetlabs.pcp.broker.inventory/batch-update-interval-ms 10
-                  server/on-connect (fn [_ ws] (websockets-client/send! ws (message/encode inventory-subscribe)))
+                  server/on-connect (fn [_ ws]
+                    (try
+                      (websockets-client/send! ws (message/encode inventory-subscribe))
+                      (catch Exception e (println "controllers-subscribe exception:" (.getMessage e)))))
                   server/on-text (fn [_ ws text]
                     (let [msg (message/decode text)]
                       (case (:message_type msg)

(deftest controller-agent-connected-test
  (let [inventory-response (promise)
        agent-response (promise)]
    (with-redefs [server/on-connect (fn [_ ws] (websockets-client/send! ws (message/encode inventory-request)))
                  server/on-text (fn [_ ws text]
                    (let [msg (message/decode text)]
                      (if (= (:message_type msg) "http://puppetlabs.com/inventory_response")

       server/on-connect (fn [_ ws]
                           ;; Only send subscribe the first time we connect
                           (when (zero? @server-message-sent)
                             (try
                               (websockets-client/send! ws (message/encode inventory-subscribe))
                               (catch Exception e (println "controllers-unsubscribe exception:" (.getMessage e))))))
       server/on-text (fn [_ ws text] (deliver inventory-response (message/decode text)))]
      (with-app-with-config app (conj broker-services server/mock-server) broker-config
        (let [broker (:broker (get-context app :BrokerService))]
