pubsub_test.go 3.9 KB

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