taoensso / carmine

Redis client + message queue for Clojure
https://www.taoensso.com/carmine
Eclipse Public License 1.0
1.15k stars 130 forks source link

Closing the listener produces at least one error message to handler, sometimes several. #311

Open RokLenarcic opened 3 weeks ago

RokLenarcic commented 3 weeks ago

I'm running this code here:

(ns memento.redis.listener3-test
  (:require [clojure.test :refer :all]
            [taoensso.carmine :as car]))

(deftest listener-subscription-test
  (testing "messages work"
    (let [p (promise)]
      (with-open [l (car/with-new-listener {}
                      (fn [[typ :as x] state]
                        (println x)
                        (when (= "message" typ)
                          (deliver p x))) {}
                      (car/subscribe "test-chan"))]
        (car/wcar {}
          (car/publish "test-chan" [1]))
        (is (= ["message" "test-chan" [1]] @p))))))

So this posts a message and closes listener and it awaits for the message to get back from redis.

First thing is that this always produces an error message to the listener:

[subscribe test-chan 1]
[message test-chan [1]]
[carmine carmine:listener:error {:error :conn-closed, :throwable ,  :listener{:type :end-test-var, :ns "memento.redis.listener3-test", :name "listener-subscription-test", :file "/Users/roklenarcic/clojure-projects/memento-redis/test/memento/redis/listener3_test.clj", :line 5, :column 1} #taoensso.carmine.Listener{:connection #taoensso.carmine.connections.Connection{:socket #object[java.net.Socket 0x7b641dbe Socket[addr=/127.0.0.1,port=6379,localport=64296]], :spec {:host 127.0.0.1, :port 6379, :listener? true}, :in #object[java.io.DataInputStream 0x3dca26c0 java.io.DataInputStream@3dca26c0], :out #object[java.io.BufferedOutputStream 0x10bf2d5d java.io.BufferedOutputStream@10bf2d5d]}, :handler #object[clojure.lang.Atom 0x53bfef3e {:status :ready, :val Ran 1 test containing 1 assertion.
... etc

But if I run this test in REPL a couple of times in a row I get runs that look like this:

[subscribe test-chan 1]
[message test-chan [1]]
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{...
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{...
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{...
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{...
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....
[carmine carmine:listener:error {:error :conn-closed, :throwable nil, :listener #taoensso.carmine.Listener{....

It instantly calls the listener handler fn with 11 instances of same error message.

ptaoussanis commented 3 weeks ago

Just to make sure that we're on the same page, could you please clarify exactly what your question is or what behaviour you're expecting?

Are you surprised that any error message is triggered, or that you're seeing multiple error messages? If the latter - how many times are you running the test in your REPL?

RokLenarcic commented 3 weeks ago

Both. So I am surprised that I get an error message at all, since closing the listener seems like a legitimate usecase. I guess it's not that hard to filter that out in the handler.

But the other thing is just confusing. I am running a single test and I get either 1 or 12 errors. I don't understand the mechanism behind that.

RokLenarcic commented 3 weeks ago

To be more specific, running the test code I pasted above in a fresh JVM usually produces 1 conn-closed error, but sometimes it produces 12 conn-closed errors. That's a single run in freshly started JVM. I am just trying to make sure I am not missing something here.

RokLenarcic commented 3 weeks ago

https://github.com/taoensso/carmine/assets/3322433/df3cfe28-fec4-49c0-a390-a77163bd17b7