pubsub_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "reflect"
  17. "testing"
  18. "github.com/garyburd/redigo/redis"
  19. )
  20. func expectPushed(t *testing.T, c redis.PubSubConn, message string, expected interface{}) {
  21. actual := c.Receive()
  22. if !reflect.DeepEqual(actual, expected) {
  23. t.Errorf("%s = %v, want %v", message, actual, expected)
  24. }
  25. }
  26. func TestPushed(t *testing.T) {
  27. pc, err := redis.DialDefaultServer()
  28. if err != nil {
  29. t.Fatalf("error connection to database, %v", err)
  30. }
  31. defer pc.Close()
  32. sc, err := redis.DialDefaultServer()
  33. if err != nil {
  34. t.Fatalf("error connection to database, %v", err)
  35. }
  36. defer sc.Close()
  37. c := redis.PubSubConn{Conn: sc}
  38. c.Subscribe("c1")
  39. expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1})
  40. c.Subscribe("c2")
  41. expectPushed(t, c, "Subscribe(c2)", redis.Subscription{Kind: "subscribe", Channel: "c2", Count: 2})
  42. c.PSubscribe("p1")
  43. expectPushed(t, c, "PSubscribe(p1)", redis.Subscription{Kind: "psubscribe", Channel: "p1", Count: 3})
  44. c.PSubscribe("p2")
  45. expectPushed(t, c, "PSubscribe(p2)", redis.Subscription{Kind: "psubscribe", Channel: "p2", Count: 4})
  46. c.PUnsubscribe()
  47. expectPushed(t, c, "Punsubscribe(p1)", redis.Subscription{Kind: "punsubscribe", Channel: "p1", Count: 3})
  48. expectPushed(t, c, "Punsubscribe()", redis.Subscription{Kind: "punsubscribe", Channel: "p2", Count: 2})
  49. pc.Do("PUBLISH", "c1", "hello")
  50. expectPushed(t, c, "PUBLISH c1 hello", redis.Message{Channel: "c1", Data: []byte("hello")})
  51. c.Ping("hello")
  52. expectPushed(t, c, `Ping("hello")`, redis.Pong{Data: "hello"})
  53. c.Conn.Send("PING")
  54. c.Conn.Flush()
  55. expectPushed(t, c, `Send("PING")`, redis.Pong{})
  56. }