You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
4.0 KiB

  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. "reflect"
  18. "sync"
  19. "testing"
  20. "github.com/garyburd/redigo/redis"
  21. )
  22. func publish(channel, value interface{}) {
  23. c, err := dial()
  24. if err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28. defer c.Close()
  29. c.Do("PUBLISH", channel, value)
  30. }
  31. // Applications can receive pushed messages from one goroutine and manage subscriptions from another goroutine.
  32. func ExamplePubSubConn() {
  33. c, err := dial()
  34. if err != nil {
  35. fmt.Println(err)
  36. return
  37. }
  38. defer c.Close()
  39. var wg sync.WaitGroup
  40. wg.Add(2)
  41. psc := redis.PubSubConn{Conn: c}
  42. // This goroutine receives and prints pushed notifications from the server.
  43. // The goroutine exits when the connection is unsubscribed from all
  44. // channels or there is an error.
  45. go func() {
  46. defer wg.Done()
  47. for {
  48. switch n := psc.Receive().(type) {
  49. case redis.Message:
  50. fmt.Printf("Message: %s %s\n", n.Channel, n.Data)
  51. case redis.PMessage:
  52. fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data)
  53. case redis.Subscription:
  54. fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count)
  55. if n.Count == 0 {
  56. return
  57. }
  58. case error:
  59. fmt.Printf("error: %v\n", n)
  60. return
  61. }
  62. }
  63. }()
  64. // This goroutine manages subscriptions for the connection.
  65. go func() {
  66. defer wg.Done()
  67. psc.Subscribe("example")
  68. psc.PSubscribe("p*")
  69. // The following function calls publish a message using another
  70. // connection to the Redis server.
  71. publish("example", "hello")
  72. publish("example", "world")
  73. publish("pexample", "foo")
  74. publish("pexample", "bar")
  75. // Unsubscribe from all connections. This will cause the receiving
  76. // goroutine to exit.
  77. psc.Unsubscribe()
  78. psc.PUnsubscribe()
  79. }()
  80. wg.Wait()
  81. // Output:
  82. // Subscription: subscribe example 1
  83. // Subscription: psubscribe p* 2
  84. // Message: example hello
  85. // Message: example world
  86. // PMessage: p* pexample foo
  87. // PMessage: p* pexample bar
  88. // Subscription: unsubscribe example 1
  89. // Subscription: punsubscribe p* 0
  90. }
  91. func expectPushed(t *testing.T, c redis.PubSubConn, message string, expected interface{}) {
  92. actual := c.Receive()
  93. if !reflect.DeepEqual(actual, expected) {
  94. t.Errorf("%s = %v, want %v", message, actual, expected)
  95. }
  96. }
  97. func TestPushed(t *testing.T) {
  98. pc, err := redis.DialDefaultServer()
  99. if err != nil {
  100. t.Fatalf("error connection to database, %v", err)
  101. }
  102. defer pc.Close()
  103. sc, err := redis.DialDefaultServer()
  104. if err != nil {
  105. t.Fatalf("error connection to database, %v", err)
  106. }
  107. defer sc.Close()
  108. c := redis.PubSubConn{Conn: sc}
  109. c.Subscribe("c1")
  110. expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1})
  111. c.Subscribe("c2")
  112. expectPushed(t, c, "Subscribe(c2)", redis.Subscription{Kind: "subscribe", Channel: "c2", Count: 2})
  113. c.PSubscribe("p1")
  114. expectPushed(t, c, "PSubscribe(p1)", redis.Subscription{Kind: "psubscribe", Channel: "p1", Count: 3})
  115. c.PSubscribe("p2")
  116. expectPushed(t, c, "PSubscribe(p2)", redis.Subscription{Kind: "psubscribe", Channel: "p2", Count: 4})
  117. c.PUnsubscribe()
  118. expectPushed(t, c, "Punsubscribe(p1)", redis.Subscription{Kind: "punsubscribe", Channel: "p1", Count: 3})
  119. expectPushed(t, c, "Punsubscribe()", redis.Subscription{Kind: "punsubscribe", Channel: "p2", Count: 2})
  120. pc.Do("PUBLISH", "c1", "hello")
  121. expectPushed(t, c, "PUBLISH c1 hello", redis.Message{Channel: "c1", Data: []byte("hello")})
  122. c.Ping("hello")
  123. expectPushed(t, c, `Ping("hello")`, redis.Pong{Data: "hello"})
  124. c.Conn.Send("PING")
  125. c.Conn.Flush()
  126. expectPushed(t, c, `Send("PING")`, redis.Pong{})
  127. }