pubsub_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/garyburd/redigo/internal/redistest"
  23. "github.com/garyburd/redigo/redis"
  24. )
  25. func publish(channel, value interface{}) {
  26. c, err := dial()
  27. if err != nil {
  28. panic(err)
  29. }
  30. defer c.Close()
  31. c.Do("PUBLISH", channel, value)
  32. }
  33. // Applications can receive pushed messages from one goroutine and manage subscriptions from another goroutine.
  34. func ExamplePubSubConn() {
  35. c, err := dial()
  36. if err != nil {
  37. panic(err)
  38. }
  39. defer c.Close()
  40. var wg sync.WaitGroup
  41. wg.Add(2)
  42. psc := redis.PubSubConn{Conn: c}
  43. // This goroutine receives and prints pushed notifications from the server.
  44. // The goroutine exits when the connection is unsubscribed from all
  45. // channels or there is an error.
  46. go func() {
  47. defer wg.Done()
  48. for {
  49. switch n := psc.Receive().(type) {
  50. case redis.Message:
  51. fmt.Printf("Message: %s %s\n", n.Channel, n.Data)
  52. case redis.PMessage:
  53. fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data)
  54. case redis.Subscription:
  55. fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count)
  56. if n.Count == 0 {
  57. return
  58. }
  59. case error:
  60. fmt.Printf("error: %v\n", n)
  61. return
  62. }
  63. }
  64. }()
  65. // This goroutine manages subscriptions for the connection.
  66. go func() {
  67. defer wg.Done()
  68. psc.Subscribe("example")
  69. psc.PSubscribe("p*")
  70. // The following function calls publish a message using another
  71. // connection to the Redis server.
  72. publish("example", "hello")
  73. publish("example", "world")
  74. publish("pexample", "foo")
  75. publish("pexample", "bar")
  76. // Unsubscribe from all connections. This will cause the receiving
  77. // goroutine to exit.
  78. psc.Unsubscribe()
  79. psc.PUnsubscribe()
  80. }()
  81. wg.Wait()
  82. // Output:
  83. // Subscription: subscribe example 1
  84. // Subscription: psubscribe p* 2
  85. // Message: example hello
  86. // Message: example world
  87. // PMessage: p* pexample foo
  88. // PMessage: p* pexample bar
  89. // Subscription: unsubscribe example 1
  90. // Subscription: punsubscribe p* 0
  91. }
  92. func expectPushed(t *testing.T, c redis.PubSubConn, message string, expected interface{}) {
  93. actual := c.Receive()
  94. if !reflect.DeepEqual(actual, expected) {
  95. t.Errorf("%s = %v, want %v", message, actual, expected)
  96. }
  97. }
  98. func TestPushed(t *testing.T) {
  99. pc, err := redistest.Dial()
  100. if err != nil {
  101. t.Fatalf("error connection to database, %v", err)
  102. }
  103. defer pc.Close()
  104. nc, err := net.Dial("tcp", ":6379")
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. defer nc.Close()
  109. nc.SetReadDeadline(time.Now().Add(4 * time.Second))
  110. c := redis.PubSubConn{Conn: redis.NewConn(nc, 0, 0)}
  111. c.Subscribe("c1")
  112. expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1})
  113. c.Subscribe("c2")
  114. expectPushed(t, c, "Subscribe(c2)", redis.Subscription{Kind: "subscribe", Channel: "c2", Count: 2})
  115. c.PSubscribe("p1")
  116. expectPushed(t, c, "PSubscribe(p1)", redis.Subscription{Kind: "psubscribe", Channel: "p1", Count: 3})
  117. c.PSubscribe("p2")
  118. expectPushed(t, c, "PSubscribe(p2)", redis.Subscription{Kind: "psubscribe", Channel: "p2", Count: 4})
  119. c.PUnsubscribe()
  120. expectPushed(t, c, "Punsubscribe(p1)", redis.Subscription{Kind: "punsubscribe", Channel: "p1", Count: 3})
  121. expectPushed(t, c, "Punsubscribe()", redis.Subscription{Kind: "punsubscribe", Channel: "p2", Count: 2})
  122. pc.Do("PUBLISH", "c1", "hello")
  123. expectPushed(t, c, "PUBLISH c1 hello", redis.Message{Channel: "c1", Data: []byte("hello")})
  124. c.Ping("hello")
  125. expectPushed(t, c, `Ping("hello")`, redis.Pong{"hello"})
  126. c.Conn.Send("PING")
  127. c.Conn.Flush()
  128. expectPushed(t, c, `Send("PING")`, redis.Pong{})
  129. }