(use-modules (redis)
             (srfi srfi-64))

(define sub-conn (redis-connect)) ;; uses env REDIS_PORT
(define pub-conn (redis-connect))

(test-begin "pubsub")

(test-equal
    "subscribe"
  '(("news" . 1)) (redis-subscribe sub-conn '("news")))

(test-equal
    "publish"
  1 (redis-publish pub-conn "news" "hello"))

(call-with-values (lambda () (redis-subscribe-read sub-conn))
  (lambda (channel message)
    (test-equal "read next-message: channel" "news" channel)
    (test-equal "read next-message: message" "hello" message)))

(test-equal
    "unsubscribe"
  '(("news" . 0)) (redis-unsubscribe sub-conn))

(define fails (test-runner-fail-count (test-runner-current)))
(test-end "pubsub")

(redis-close pub-conn)
(redis-close sub-conn)

(exit (if (= fails 0) 0 1))
