redis_queue_test.go 981 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package redis
  2. import (
  3. "fmt"
  4. "github.com/gomodule/redigo/redis"
  5. "github.com/spf13/viper"
  6. "testing"
  7. "time"
  8. )
  9. func TestRedisQueue(t *testing.T) {
  10. // 配置
  11. viper.SetDefault("redis.host", "127.0.0.1:6379") // single node or master/slave
  12. //viper.SetDefault("redis.nodes", []string{"192.168.101.68:6379"}) // cluster or master/slave with sentinel
  13. //viper.SetDefault("redis.master", "mymaster") // master name config for sentinel
  14. // 初始化
  15. q := NewRedisQueue("myList", "backList", 3000, 5*time.Second)
  16. q.IsIdempotent = true
  17. q.Clean()
  18. q.Recycle()
  19. // 消费
  20. for i := 0; i < 4; i++ {
  21. go func(idx int, q *Queue) {
  22. q.Pop(func(reply interface{}) error {
  23. content, _ := redis.String(reply, nil)
  24. fmt.Printf("Receiver %d get:%+v\n", idx, content)
  25. return nil
  26. })
  27. }(i, q)
  28. }
  29. // 生产
  30. go func(q *Queue) {
  31. for i := 0; i < 20; i++ {
  32. q.Push(fmt.Sprintf("Message %d", i+1))
  33. time.Sleep(1 * time.Second)
  34. }
  35. }(q)
  36. quit := make(chan bool)
  37. <-quit
  38. }