]> git.saurik.com Git - redis.git/commitdiff
initial basic pub/sub tests
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Tue, 15 Jun 2010 20:40:21 +0000 (22:40 +0200)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Wed, 16 Jun 2010 09:01:42 +0000 (11:01 +0200)
tests/support/redis.tcl
tests/test_helper.tcl
tests/unit/pubsub.tcl [new file with mode: 0644]

index 338dcf77e07c3e654bd24bca67431490c6f48e10..7c7c65c6564f2cb1847d695b53d35a0f78c685f5 100644 (file)
@@ -41,7 +41,7 @@ array set ::redis::multibulkarg {}
 
 # Flag commands requiring last argument as a bulk write operation
 foreach redis_bulk_cmd {
-    set setnx rpush lpush rpushx lpushx linsert lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby append zrank zrevrank hget hdel hexists setex
+    set setnx rpush lpush rpushx lpushx linsert lset lrem sadd srem sismember echo getset smove zadd zrem zscore zincrby append zrank zrevrank hget hdel hexists setex publish
 } {
     set ::redis::bulkarg($redis_bulk_cmd) {}
 }
index dac91bef3974196f7c04dd29a1e269f7cca50a01..59470e6473b8780c918b1791f3fbbfd03e6010c7 100644 (file)
@@ -88,6 +88,7 @@ proc main {} {
     execute_tests "unit/cas"
     execute_tests "integration/replication"
     execute_tests "integration/aof"
+    execute_tests "unit/pubsub"
 
     # run tests with VM enabled
     set ::global_overrides {vm-enabled yes}
diff --git a/tests/unit/pubsub.tcl b/tests/unit/pubsub.tcl
new file mode 100644 (file)
index 0000000..10af858
--- /dev/null
@@ -0,0 +1,45 @@
+start_server {tags {"pubsub"}} {
+    test "PUBLISH when no one is listening" {
+        assert_equal 0 [r publish chan hello]
+    }
+
+    test "SUBSCRIBE basics" {
+        set rd1 [redis_deferring_client]
+        set rd2 [redis_deferring_client]
+
+        # subscribe first client to two channels
+        $rd1 subscribe chan1 chan2
+        assert_equal {subscribe chan1 1} [$rd1 read]
+        assert_equal {subscribe chan2 2} [$rd1 read]
+
+        # publish on both channels
+        assert_equal 1 [r publish chan1 hello]
+        assert_equal 1 [r publish chan2 world]
+        assert_equal {message chan1 hello} [$rd1 read]
+        assert_equal {message chan2 world} [$rd1 read]
+
+        # subscribe second client to one channel
+        $rd2 subscribe chan1
+        assert_equal {subscribe chan1 1} [$rd2 read]
+
+        # publish on channel with two subscribers
+        assert_equal 2 [r publish chan1 hello]
+        assert_equal {message chan1 hello} [$rd1 read]
+        assert_equal {message chan1 hello} [$rd2 read]
+
+        # unsubscribe first client from all channels
+        $rd1 unsubscribe
+        set msg [$rd1 read]
+        assert_equal "unsubscribe" [lindex $msg 0]
+        assert_match "chan*" [lindex $msg 1]
+        assert_match 1 [lindex $msg 2]
+        set msg [$rd1 read]
+        assert_equal "unsubscribe" [lindex $msg 0]
+        assert_match "chan*" [lindex $msg 1]
+        assert_match 0 [lindex $msg 2]
+
+        # publish on channel with only remaining subscriber
+        assert_equal 1 [r publish chan1 hello]
+        assert_equal {message chan1 hello} [$rd2 read]
+    }
+}
\ No newline at end of file