redis_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package redis
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/library/container/pool"
  7. xtime "go-common/library/time"
  8. )
  9. var p *Pool
  10. var config *Config
  11. func init() {
  12. config = getConfig()
  13. p = NewPool(config)
  14. }
  15. func getConfig() (c *Config) {
  16. c = &Config{
  17. Name: "test",
  18. Proto: "tcp",
  19. Addr: "172.16.33.54:6379",
  20. DialTimeout: xtime.Duration(time.Second),
  21. ReadTimeout: xtime.Duration(time.Second),
  22. WriteTimeout: xtime.Duration(time.Second),
  23. }
  24. c.Config = &pool.Config{
  25. Active: 20,
  26. Idle: 2,
  27. IdleTimeout: xtime.Duration(90 * time.Second),
  28. }
  29. return
  30. }
  31. func TestRedis(t *testing.T) {
  32. testSet(t, p)
  33. testSend(t, p)
  34. testGet(t, p)
  35. testErr(t, p)
  36. if err := p.Close(); err != nil {
  37. t.Errorf("redis: close error(%v)", err)
  38. }
  39. conn, err := NewConn(config)
  40. if err != nil {
  41. t.Errorf("redis: new conn error(%v)", err)
  42. }
  43. if err := conn.Close(); err != nil {
  44. t.Errorf("redis: close error(%v)", err)
  45. }
  46. }
  47. func testSet(t *testing.T, p *Pool) {
  48. var (
  49. key = "test"
  50. value = "test"
  51. conn = p.Get(context.TODO())
  52. )
  53. defer conn.Close()
  54. if reply, err := conn.Do("set", key, value); err != nil {
  55. t.Errorf("redis: conn.Do(SET, %s, %s) error(%v)", key, value, err)
  56. } else {
  57. t.Logf("redis: set status: %s", reply)
  58. }
  59. }
  60. func testSend(t *testing.T, p *Pool) {
  61. var (
  62. key = "test"
  63. value = "test"
  64. expire = 1000
  65. conn = p.Get(context.TODO())
  66. )
  67. defer conn.Close()
  68. if err := conn.Send("SET", key, value); err != nil {
  69. t.Errorf("redis: conn.Send(SET, %s, %s) error(%v)", key, value, err)
  70. }
  71. if err := conn.Send("EXPIRE", key, expire); err != nil {
  72. t.Errorf("redis: conn.Send(EXPIRE key(%s) expire(%d)) error(%v)", key, expire, err)
  73. }
  74. if err := conn.Flush(); err != nil {
  75. t.Errorf("redis: conn.Flush error(%v)", err)
  76. }
  77. for i := 0; i < 2; i++ {
  78. if _, err := conn.Receive(); err != nil {
  79. t.Errorf("redis: conn.Receive error(%v)", err)
  80. return
  81. }
  82. }
  83. t.Logf("redis: set value: %s", value)
  84. }
  85. func testGet(t *testing.T, p *Pool) {
  86. var (
  87. key = "test"
  88. conn = p.Get(context.TODO())
  89. )
  90. defer conn.Close()
  91. if reply, err := conn.Do("GET", key); err != nil {
  92. t.Errorf("redis: conn.Do(GET, %s) error(%v)", key, err)
  93. } else {
  94. t.Logf("redis: get value: %s", reply)
  95. }
  96. }
  97. func testErr(t *testing.T, p *Pool) {
  98. conn := p.Get(context.TODO())
  99. if err := conn.Close(); err != nil {
  100. t.Errorf("memcache: close error(%v)", err)
  101. }
  102. if err := conn.Err(); err == nil {
  103. t.Errorf("redis: err not nil")
  104. } else {
  105. t.Logf("redis: err: %v", err)
  106. }
  107. }
  108. func BenchmarkMemcache(b *testing.B) {
  109. b.ResetTimer()
  110. b.RunParallel(func(pb *testing.PB) {
  111. for pb.Next() {
  112. conn := p.Get(context.TODO())
  113. if err := conn.Close(); err != nil {
  114. b.Errorf("memcache: close error(%v)", err)
  115. }
  116. }
  117. })
  118. if err := p.Close(); err != nil {
  119. b.Errorf("memcache: close error(%v)", err)
  120. }
  121. }