reply_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "github.com/garyburd/redigo/redis"
  18. "net"
  19. "reflect"
  20. "testing"
  21. "time"
  22. )
  23. func ExampleBool() {
  24. c, err := dial()
  25. if err != nil {
  26. panic(err)
  27. }
  28. defer c.Close()
  29. c.Do("SET", "foo", 1)
  30. exists, _ := redis.Bool(c.Do("EXISTS", "foo"))
  31. fmt.Printf("%#v\n", exists)
  32. // Output:
  33. // true
  34. }
  35. func ExampleInt() {
  36. c, err := dial()
  37. if err != nil {
  38. panic(err)
  39. }
  40. defer c.Close()
  41. c.Do("SET", "k1", 1)
  42. n, _ := redis.Int(c.Do("GET", "k1"))
  43. fmt.Printf("%#v\n", n)
  44. n, _ = redis.Int(c.Do("INCR", "k1"))
  45. fmt.Printf("%#v\n", n)
  46. // Output:
  47. // 1
  48. // 2
  49. }
  50. func ExampleString() {
  51. c, err := dial()
  52. if err != nil {
  53. panic(err)
  54. }
  55. defer c.Close()
  56. c.Do("SET", "hello", "world")
  57. s, err := redis.String(c.Do("GET", "hello"))
  58. fmt.Printf("%#v\n", s)
  59. // Output:
  60. // "world"
  61. }
  62. func ExampleNotification(c redis.Conn) {
  63. c.Send("SUBSCRIBE", "mychannel")
  64. for {
  65. n, err := redis.Notification(c.Receive())
  66. if err != nil {
  67. break
  68. }
  69. switch n := n.(type) {
  70. case redis.Message:
  71. fmt.Printf("%s: message: %s", n.Channel, n.Data)
  72. case redis.Subscription:
  73. fmt.Printf("%s: %s %d", n.Channel, n.Kind, n.Count)
  74. default:
  75. panic("unexpected")
  76. }
  77. }
  78. }
  79. func expectNotification(t *testing.T, c redis.Conn, message string, expected interface{}) {
  80. actual, err := redis.Notification(c.Receive())
  81. if err != nil {
  82. t.Errorf("%s returned error %v", message, err)
  83. return
  84. }
  85. if !reflect.DeepEqual(actual, expected) {
  86. t.Errorf("%s = %v, want %v", message, actual, expected)
  87. }
  88. }
  89. func TestNotification(t *testing.T) {
  90. pc, err := dial()
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer pc.Close()
  95. nc, err := net.Dial("tcp", ":6379")
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. defer nc.Close()
  100. nc.SetReadDeadline(time.Now().Add(4 * time.Second))
  101. c := redis.NewConn(nc)
  102. c.Send("SUBSCRIBE", "c1")
  103. expectNotification(t, c, "Subscribe(c1)", redis.Subscription{"subscribe", "c1", 1})
  104. c.Send("SUBSCRIBE", "c2")
  105. expectNotification(t, c, "Subscribe(c2)", redis.Subscription{"subscribe", "c2", 2})
  106. c.Send("PSUBSCRIBE", "p1")
  107. expectNotification(t, c, "PSubscribe(p1)", redis.Subscription{"psubscribe", "p1", 3})
  108. c.Send("PSUBSCRIBE", "p2")
  109. expectNotification(t, c, "PSubscribe(p2)", redis.Subscription{"psubscribe", "p2", 4})
  110. c.Send("PUNSUBSCRIBE")
  111. expectNotification(t, c, "Punsubscribe(p1)", redis.Subscription{"punsubscribe", "p1", 3})
  112. expectNotification(t, c, "Punsubscribe()", redis.Subscription{"punsubscribe", "p2", 2})
  113. pc.Do("PUBLISH", "c1", "hello")
  114. expectNotification(t, c, "PUBLISH c1 hello", redis.Message{"c1", []byte("hello")})
  115. }