pool_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Copyright 2011 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
  15. import (
  16. "context"
  17. "errors"
  18. "io"
  19. "reflect"
  20. "sync"
  21. "testing"
  22. "time"
  23. "go-common/library/container/pool"
  24. )
  25. type poolTestConn struct {
  26. d *poolDialer
  27. err error
  28. Conn
  29. }
  30. func (c *poolTestConn) Close() error {
  31. c.d.mu.Lock()
  32. c.d.open--
  33. c.d.mu.Unlock()
  34. return c.Conn.Close()
  35. }
  36. func (c *poolTestConn) Err() error { return c.err }
  37. func (c *poolTestConn) Do(commandName string, args ...interface{}) (interface{}, error) {
  38. if commandName == "ERR" {
  39. c.err = args[0].(error)
  40. commandName = "PING"
  41. }
  42. if commandName != "" {
  43. c.d.commands = append(c.d.commands, commandName)
  44. }
  45. return c.Conn.Do(commandName, args...)
  46. }
  47. func (c *poolTestConn) Send(commandName string, args ...interface{}) error {
  48. c.d.commands = append(c.d.commands, commandName)
  49. return c.Conn.Send(commandName, args...)
  50. }
  51. type poolDialer struct {
  52. mu sync.Mutex
  53. t *testing.T
  54. dialed int
  55. open int
  56. commands []string
  57. dialErr error
  58. }
  59. func (d *poolDialer) dial() (Conn, error) {
  60. d.mu.Lock()
  61. d.dialed++
  62. dialErr := d.dialErr
  63. d.mu.Unlock()
  64. if dialErr != nil {
  65. return nil, d.dialErr
  66. }
  67. c, err := DialDefaultServer()
  68. if err != nil {
  69. return nil, err
  70. }
  71. d.mu.Lock()
  72. d.open++
  73. d.mu.Unlock()
  74. return &poolTestConn{d: d, Conn: c}, nil
  75. }
  76. func (d *poolDialer) check(message string, p *Pool, dialed, open int) {
  77. d.mu.Lock()
  78. if d.dialed != dialed {
  79. d.t.Errorf("%s: dialed=%d, want %d", message, d.dialed, dialed)
  80. }
  81. if d.open != open {
  82. d.t.Errorf("%s: open=%d, want %d", message, d.open, open)
  83. }
  84. // if active := p.ActiveCount(); active != open {
  85. // d.t.Errorf("%s: active=%d, want %d", message, active, open)
  86. // }
  87. d.mu.Unlock()
  88. }
  89. func TestPoolReuse(t *testing.T) {
  90. d := poolDialer{t: t}
  91. p := NewPool(config)
  92. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  93. return d.dial()
  94. }
  95. for i := 0; i < 10; i++ {
  96. c1 := p.Get(context.TODO())
  97. c1.Do("PING")
  98. c2 := p.Get(context.TODO())
  99. c2.Do("PING")
  100. c1.Close()
  101. c2.Close()
  102. }
  103. d.check("before close", p, 2, 2)
  104. p.Close()
  105. d.check("after close", p, 2, 0)
  106. }
  107. func TestPoolMaxIdle(t *testing.T) {
  108. d := poolDialer{t: t}
  109. p := NewPool(config)
  110. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  111. return d.dial()
  112. }
  113. defer p.Close()
  114. for i := 0; i < 10; i++ {
  115. c1 := p.Get(context.TODO())
  116. c1.Do("PING")
  117. c2 := p.Get(context.TODO())
  118. c2.Do("PING")
  119. c3 := p.Get(context.TODO())
  120. c3.Do("PING")
  121. c1.Close()
  122. c2.Close()
  123. c3.Close()
  124. }
  125. d.check("before close", p, 12, 2)
  126. p.Close()
  127. d.check("after close", p, 12, 0)
  128. }
  129. func TestPoolError(t *testing.T) {
  130. d := poolDialer{t: t}
  131. p := NewPool(config)
  132. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  133. return d.dial()
  134. }
  135. defer p.Close()
  136. c := p.Get(context.TODO())
  137. c.Do("ERR", io.EOF)
  138. if c.Err() == nil {
  139. t.Errorf("expected c.Err() != nil")
  140. }
  141. c.Close()
  142. c = p.Get(context.TODO())
  143. c.Do("ERR", io.EOF)
  144. c.Close()
  145. d.check(".", p, 2, 0)
  146. }
  147. func TestPoolClose(t *testing.T) {
  148. d := poolDialer{t: t}
  149. p := NewPool(config)
  150. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  151. return d.dial()
  152. }
  153. defer p.Close()
  154. c1 := p.Get(context.TODO())
  155. c1.Do("PING")
  156. c2 := p.Get(context.TODO())
  157. c2.Do("PING")
  158. c3 := p.Get(context.TODO())
  159. c3.Do("PING")
  160. c1.Close()
  161. if _, err := c1.Do("PING"); err == nil {
  162. t.Errorf("expected error after connection closed")
  163. }
  164. c2.Close()
  165. c2.Close()
  166. p.Close()
  167. d.check("after pool close", p, 3, 1)
  168. if _, err := c1.Do("PING"); err == nil {
  169. t.Errorf("expected error after connection and pool closed")
  170. }
  171. c3.Close()
  172. d.check("after conn close", p, 3, 0)
  173. c1 = p.Get(context.TODO())
  174. if _, err := c1.Do("PING"); err == nil {
  175. t.Errorf("expected error after pool closed")
  176. }
  177. }
  178. func TestPoolConcurrenSendReceive(t *testing.T) {
  179. p := NewPool(config)
  180. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  181. return DialDefaultServer()
  182. }
  183. defer p.Close()
  184. c := p.Get(context.TODO())
  185. done := make(chan error, 1)
  186. go func() {
  187. _, err := c.Receive()
  188. done <- err
  189. }()
  190. c.Send("PING")
  191. c.Flush()
  192. err := <-done
  193. if err != nil {
  194. t.Fatalf("Receive() returned error %v", err)
  195. }
  196. _, err = c.Do("")
  197. if err != nil {
  198. t.Fatalf("Do() returned error %v", err)
  199. }
  200. c.Close()
  201. }
  202. func TestPoolMaxActive(t *testing.T) {
  203. d := poolDialer{t: t}
  204. config.Config = &pool.Config{
  205. Active: 2,
  206. Idle: 2,
  207. }
  208. p := NewPool(config)
  209. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  210. return d.dial()
  211. }
  212. defer p.Close()
  213. c1 := p.Get(context.TODO())
  214. c1.Do("PING")
  215. c2 := p.Get(context.TODO())
  216. c2.Do("PING")
  217. d.check("1", p, 2, 2)
  218. c3 := p.Get(context.TODO())
  219. if _, err := c3.Do("PING"); err != pool.ErrPoolExhausted {
  220. t.Errorf("expected pool exhausted")
  221. }
  222. c3.Close()
  223. d.check("2", p, 2, 2)
  224. c2.Close()
  225. d.check("3", p, 2, 2)
  226. c3 = p.Get(context.TODO())
  227. if _, err := c3.Do("PING"); err != nil {
  228. t.Errorf("expected good channel, err=%v", err)
  229. }
  230. c3.Close()
  231. d.check("4", p, 2, 2)
  232. }
  233. func TestPoolMonitorCleanup(t *testing.T) {
  234. d := poolDialer{t: t}
  235. p := NewPool(config)
  236. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  237. return d.dial()
  238. }
  239. defer p.Close()
  240. c := p.Get(context.TODO())
  241. c.Send("MONITOR")
  242. c.Close()
  243. d.check("", p, 1, 0)
  244. }
  245. func TestPoolPubSubCleanup(t *testing.T) {
  246. d := poolDialer{t: t}
  247. p := NewPool(config)
  248. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  249. return d.dial()
  250. }
  251. defer p.Close()
  252. c := p.Get(context.TODO())
  253. c.Send("SUBSCRIBE", "x")
  254. c.Close()
  255. want := []string{"SUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  256. if !reflect.DeepEqual(d.commands, want) {
  257. t.Errorf("got commands %v, want %v", d.commands, want)
  258. }
  259. d.commands = nil
  260. c = p.Get(context.TODO())
  261. c.Send("PSUBSCRIBE", "x*")
  262. c.Close()
  263. want = []string{"PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  264. if !reflect.DeepEqual(d.commands, want) {
  265. t.Errorf("got commands %v, want %v", d.commands, want)
  266. }
  267. d.commands = nil
  268. }
  269. func TestPoolTransactionCleanup(t *testing.T) {
  270. d := poolDialer{t: t}
  271. p := NewPool(config)
  272. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  273. return d.dial()
  274. }
  275. defer p.Close()
  276. c := p.Get(context.TODO())
  277. c.Do("WATCH", "key")
  278. c.Do("PING")
  279. c.Close()
  280. want := []string{"WATCH", "PING", "UNWATCH"}
  281. if !reflect.DeepEqual(d.commands, want) {
  282. t.Errorf("got commands %v, want %v", d.commands, want)
  283. }
  284. d.commands = nil
  285. c = p.Get(context.TODO())
  286. c.Do("WATCH", "key")
  287. c.Do("UNWATCH")
  288. c.Do("PING")
  289. c.Close()
  290. want = []string{"WATCH", "UNWATCH", "PING"}
  291. if !reflect.DeepEqual(d.commands, want) {
  292. t.Errorf("got commands %v, want %v", d.commands, want)
  293. }
  294. d.commands = nil
  295. c = p.Get(context.TODO())
  296. c.Do("WATCH", "key")
  297. c.Do("MULTI")
  298. c.Do("PING")
  299. c.Close()
  300. want = []string{"WATCH", "MULTI", "PING", "DISCARD"}
  301. if !reflect.DeepEqual(d.commands, want) {
  302. t.Errorf("got commands %v, want %v", d.commands, want)
  303. }
  304. d.commands = nil
  305. c = p.Get(context.TODO())
  306. c.Do("WATCH", "key")
  307. c.Do("MULTI")
  308. c.Do("DISCARD")
  309. c.Do("PING")
  310. c.Close()
  311. want = []string{"WATCH", "MULTI", "DISCARD", "PING"}
  312. if !reflect.DeepEqual(d.commands, want) {
  313. t.Errorf("got commands %v, want %v", d.commands, want)
  314. }
  315. d.commands = nil
  316. c = p.Get(context.TODO())
  317. c.Do("WATCH", "key")
  318. c.Do("MULTI")
  319. c.Do("EXEC")
  320. c.Do("PING")
  321. c.Close()
  322. want = []string{"WATCH", "MULTI", "EXEC", "PING"}
  323. if !reflect.DeepEqual(d.commands, want) {
  324. t.Errorf("got commands %v, want %v", d.commands, want)
  325. }
  326. d.commands = nil
  327. }
  328. func startGoroutines(p *Pool, cmd string, args ...interface{}) chan error {
  329. errs := make(chan error, 10)
  330. for i := 0; i < cap(errs); i++ {
  331. go func() {
  332. c := p.Get(context.TODO())
  333. _, err := c.Do(cmd, args...)
  334. errs <- err
  335. c.Close()
  336. }()
  337. }
  338. // Wait for goroutines to block.
  339. time.Sleep(time.Second / 4)
  340. return errs
  341. }
  342. func TestWaitPoolDialError(t *testing.T) {
  343. testErr := errors.New("test")
  344. d := poolDialer{t: t}
  345. config1 := getConfig()
  346. config1.Config = &pool.Config{
  347. Active: 1,
  348. Idle: 1,
  349. Wait: true,
  350. }
  351. p := NewPool(config1)
  352. p.Slice.New = func(ctx context.Context) (io.Closer, error) {
  353. return d.dial()
  354. }
  355. defer p.Close()
  356. c := p.Get(context.TODO())
  357. errs := startGoroutines(p, "ERR", testErr)
  358. d.check("before close", p, 1, 1)
  359. d.dialErr = errors.New("dial")
  360. c.Close()
  361. nilCount := 0
  362. errCount := 0
  363. timeout := time.After(2 * time.Second)
  364. for i := 0; i < cap(errs); i++ {
  365. select {
  366. case err := <-errs:
  367. switch err {
  368. case nil:
  369. nilCount++
  370. case d.dialErr:
  371. errCount++
  372. default:
  373. t.Fatalf("expected dial error or nil, got %v", err)
  374. }
  375. case <-timeout:
  376. t.Logf("Wait all the time and timeout %d", i)
  377. return
  378. }
  379. }
  380. if nilCount != 1 {
  381. t.Errorf("expected one nil error, got %d", nilCount)
  382. }
  383. if errCount != cap(errs)-1 {
  384. t.Errorf("expected %d dial erors, got %d", cap(errs)-1, errCount)
  385. }
  386. d.check("done", p, cap(errs), 0)
  387. }