PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/redis/pubsub_test.go

https://gitlab.com/sika-forks/go-redis
Go | 83 lines | 52 code | 13 blank | 18 comment | 11 complexity | b6c41219877d5370286faed1723a4c3e MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright 2013 go-redis authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package redis
  15. import (
  16. "math/rand"
  17. "testing"
  18. "time"
  19. )
  20. // TODO: sort tests by dependency (set first, etc)
  21. // rc is the redis client handler used for all tests.
  22. // Make sure redis-server is running before starting the tests.
  23. var rcPubSub *Client
  24. func init() {
  25. rcPubSub = New("127.0.0.1:6379")
  26. rand.Seed(time.Now().UTC().UnixNano())
  27. }
  28. // Tests
  29. func TestPubSub(t *testing.T) {
  30. k := randomString(16)
  31. v := "pubsubis"
  32. ch := make(chan PubSubMessage)
  33. stop := make(chan bool)
  34. err := rcPubSub.Subscribe(k, ch, stop)
  35. if err != nil {
  36. t.Error(err)
  37. return
  38. }
  39. kids := make(chan bool)
  40. counter := 100
  41. go func() {
  42. run := true
  43. for run {
  44. select {
  45. case vv := <-ch:
  46. //fmt.Println(vv.Channel, vv.Value, vv.Error)
  47. if vv.Error != nil {
  48. run = false
  49. }
  50. }
  51. }
  52. kids <- true
  53. }()
  54. go func() {
  55. for i := counter; i > 0; i-- {
  56. err := rcPubSub.Publish(k, v)
  57. if err != nil {
  58. t.Error(err)
  59. return
  60. }
  61. counter--
  62. }
  63. stop <- true
  64. }()
  65. <-kids
  66. if counter > 0 {
  67. t.Error("Failed to parse PubSub messages ", counter)
  68. return
  69. }
  70. }