pool_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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_test
  15. import (
  16. "errors"
  17. "io"
  18. "reflect"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/garyburd/redigo/internal/redistest"
  23. "github.com/garyburd/redigo/redis"
  24. )
  25. type poolTestConn struct {
  26. d *poolDialer
  27. err error
  28. redis.Conn
  29. }
  30. func (c *poolTestConn) Close() error {
  31. c.d.mu.Lock()
  32. c.d.open -= 1
  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() (redis.Conn, error) {
  60. d.mu.Lock()
  61. d.dialed += 1
  62. dialErr := d.dialErr
  63. d.mu.Unlock()
  64. if dialErr != nil {
  65. return nil, d.dialErr
  66. }
  67. c, err := redistest.Dial()
  68. if err != nil {
  69. return nil, err
  70. }
  71. d.mu.Lock()
  72. d.open += 1
  73. d.mu.Unlock()
  74. return &poolTestConn{d: d, Conn: c}, nil
  75. }
  76. func (d *poolDialer) check(message string, p *redis.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 := &redis.Pool{
  92. MaxIdle: 2,
  93. Dial: d.dial,
  94. }
  95. for i := 0; i < 10; i++ {
  96. c1 := p.Get()
  97. c1.Do("PING")
  98. c2 := p.Get()
  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 := &redis.Pool{
  110. MaxIdle: 2,
  111. Dial: d.dial,
  112. }
  113. defer p.Close()
  114. for i := 0; i < 10; i++ {
  115. c1 := p.Get()
  116. c1.Do("PING")
  117. c2 := p.Get()
  118. c2.Do("PING")
  119. c3 := p.Get()
  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 := &redis.Pool{
  132. MaxIdle: 2,
  133. Dial: d.dial,
  134. }
  135. defer p.Close()
  136. c := p.Get()
  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()
  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 := &redis.Pool{
  150. MaxIdle: 2,
  151. Dial: d.dial,
  152. }
  153. defer p.Close()
  154. c1 := p.Get()
  155. c1.Do("PING")
  156. c2 := p.Get()
  157. c2.Do("PING")
  158. c3 := p.Get()
  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()
  174. if _, err := c1.Do("PING"); err == nil {
  175. t.Errorf("expected error after pool closed")
  176. }
  177. }
  178. func TestPoolTimeout(t *testing.T) {
  179. d := poolDialer{t: t}
  180. p := &redis.Pool{
  181. MaxIdle: 2,
  182. IdleTimeout: 300 * time.Second,
  183. Dial: d.dial,
  184. }
  185. defer p.Close()
  186. now := time.Now()
  187. redis.SetNowFunc(func() time.Time { return now })
  188. defer redis.SetNowFunc(time.Now)
  189. c := p.Get()
  190. c.Do("PING")
  191. c.Close()
  192. d.check("1", p, 1, 1)
  193. now = now.Add(p.IdleTimeout)
  194. c = p.Get()
  195. c.Do("PING")
  196. c.Close()
  197. d.check("2", p, 2, 1)
  198. }
  199. func TestPoolConcurrenSendReceive(t *testing.T) {
  200. p := &redis.Pool{
  201. Dial: redistest.Dial,
  202. }
  203. defer p.Close()
  204. c := p.Get()
  205. done := make(chan error, 1)
  206. go func() {
  207. _, err := c.Receive()
  208. done <- err
  209. }()
  210. c.Send("PING")
  211. c.Flush()
  212. err := <-done
  213. if err != nil {
  214. t.Fatalf("Receive() returned error %v", err)
  215. }
  216. _, err = c.Do("")
  217. if err != nil {
  218. t.Fatalf("Do() returned error %v", err)
  219. }
  220. c.Close()
  221. }
  222. func TestPoolBorrowCheck(t *testing.T) {
  223. d := poolDialer{t: t}
  224. p := &redis.Pool{
  225. MaxIdle: 2,
  226. Dial: d.dial,
  227. TestOnBorrow: func(redis.Conn, time.Time) error { return redis.Error("BLAH") },
  228. }
  229. defer p.Close()
  230. for i := 0; i < 10; i++ {
  231. c := p.Get()
  232. c.Do("PING")
  233. c.Close()
  234. }
  235. d.check("1", p, 10, 1)
  236. }
  237. func TestPoolMaxActive(t *testing.T) {
  238. d := poolDialer{t: t}
  239. p := &redis.Pool{
  240. MaxIdle: 2,
  241. MaxActive: 2,
  242. Dial: d.dial,
  243. }
  244. defer p.Close()
  245. c1 := p.Get()
  246. c1.Do("PING")
  247. c2 := p.Get()
  248. c2.Do("PING")
  249. d.check("1", p, 2, 2)
  250. c3 := p.Get()
  251. if _, err := c3.Do("PING"); err != redis.ErrPoolExhausted {
  252. t.Errorf("expected pool exhausted")
  253. }
  254. c3.Close()
  255. d.check("2", p, 2, 2)
  256. c2.Close()
  257. d.check("3", p, 2, 2)
  258. c3 = p.Get()
  259. if _, err := c3.Do("PING"); err != nil {
  260. t.Errorf("expected good channel, err=%v", err)
  261. }
  262. c3.Close()
  263. d.check("4", p, 2, 2)
  264. }
  265. func TestPoolMonitorCleanup(t *testing.T) {
  266. d := poolDialer{t: t}
  267. p := &redis.Pool{
  268. MaxIdle: 2,
  269. MaxActive: 2,
  270. Dial: d.dial,
  271. }
  272. defer p.Close()
  273. c := p.Get()
  274. c.Send("MONITOR")
  275. c.Close()
  276. d.check("", p, 1, 0)
  277. }
  278. func TestPoolPubSubCleanup(t *testing.T) {
  279. d := poolDialer{t: t}
  280. p := &redis.Pool{
  281. MaxIdle: 2,
  282. MaxActive: 2,
  283. Dial: d.dial,
  284. }
  285. defer p.Close()
  286. c := p.Get()
  287. c.Send("SUBSCRIBE", "x")
  288. c.Close()
  289. want := []string{"SUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  290. if !reflect.DeepEqual(d.commands, want) {
  291. t.Errorf("got commands %v, want %v", d.commands, want)
  292. }
  293. d.commands = nil
  294. c = p.Get()
  295. c.Send("PSUBSCRIBE", "x*")
  296. c.Close()
  297. want = []string{"PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  298. if !reflect.DeepEqual(d.commands, want) {
  299. t.Errorf("got commands %v, want %v", d.commands, want)
  300. }
  301. d.commands = nil
  302. }
  303. func TestPoolTransactionCleanup(t *testing.T) {
  304. d := poolDialer{t: t}
  305. p := &redis.Pool{
  306. MaxIdle: 2,
  307. MaxActive: 2,
  308. Dial: d.dial,
  309. }
  310. defer p.Close()
  311. c := p.Get()
  312. c.Do("WATCH", "key")
  313. c.Do("PING")
  314. c.Close()
  315. want := []string{"WATCH", "PING", "UNWATCH"}
  316. if !reflect.DeepEqual(d.commands, want) {
  317. t.Errorf("got commands %v, want %v", d.commands, want)
  318. }
  319. d.commands = nil
  320. c = p.Get()
  321. c.Do("WATCH", "key")
  322. c.Do("UNWATCH")
  323. c.Do("PING")
  324. c.Close()
  325. want = []string{"WATCH", "UNWATCH", "PING"}
  326. if !reflect.DeepEqual(d.commands, want) {
  327. t.Errorf("got commands %v, want %v", d.commands, want)
  328. }
  329. d.commands = nil
  330. c = p.Get()
  331. c.Do("WATCH", "key")
  332. c.Do("MULTI")
  333. c.Do("PING")
  334. c.Close()
  335. want = []string{"WATCH", "MULTI", "PING", "DISCARD"}
  336. if !reflect.DeepEqual(d.commands, want) {
  337. t.Errorf("got commands %v, want %v", d.commands, want)
  338. }
  339. d.commands = nil
  340. c = p.Get()
  341. c.Do("WATCH", "key")
  342. c.Do("MULTI")
  343. c.Do("DISCARD")
  344. c.Do("PING")
  345. c.Close()
  346. want = []string{"WATCH", "MULTI", "DISCARD", "PING"}
  347. if !reflect.DeepEqual(d.commands, want) {
  348. t.Errorf("got commands %v, want %v", d.commands, want)
  349. }
  350. d.commands = nil
  351. c = p.Get()
  352. c.Do("WATCH", "key")
  353. c.Do("MULTI")
  354. c.Do("EXEC")
  355. c.Do("PING")
  356. c.Close()
  357. want = []string{"WATCH", "MULTI", "EXEC", "PING"}
  358. if !reflect.DeepEqual(d.commands, want) {
  359. t.Errorf("got commands %v, want %v", d.commands, want)
  360. }
  361. d.commands = nil
  362. }
  363. func startGoroutines(p *redis.Pool, cmd string, args ...interface{}) chan error {
  364. errs := make(chan error, 10)
  365. for i := 0; i < cap(errs); i++ {
  366. go func() {
  367. c := p.Get()
  368. _, err := c.Do(cmd, args...)
  369. errs <- err
  370. c.Close()
  371. }()
  372. }
  373. // Wait for goroutines to block.
  374. time.Sleep(time.Second / 4)
  375. return errs
  376. }
  377. func TestWaitPool(t *testing.T) {
  378. d := poolDialer{t: t}
  379. p := &redis.Pool{
  380. MaxIdle: 1,
  381. MaxActive: 1,
  382. Dial: d.dial,
  383. Wait: true,
  384. }
  385. defer p.Close()
  386. c := p.Get()
  387. errs := startGoroutines(p, "PING")
  388. d.check("before close", p, 1, 1)
  389. c.Close()
  390. timeout := time.After(2 * time.Second)
  391. for i := 0; i < cap(errs); i++ {
  392. select {
  393. case err := <-errs:
  394. if err != nil {
  395. t.Fatal(err)
  396. }
  397. case <-timeout:
  398. t.Fatalf("timeout waiting for blocked goroutine %d", i)
  399. }
  400. }
  401. d.check("done", p, 1, 1)
  402. }
  403. func TestWaitPoolClose(t *testing.T) {
  404. d := poolDialer{t: t}
  405. p := &redis.Pool{
  406. MaxIdle: 1,
  407. MaxActive: 1,
  408. Dial: d.dial,
  409. Wait: true,
  410. }
  411. defer p.Close()
  412. c := p.Get()
  413. if _, err := c.Do("PING"); err != nil {
  414. t.Fatal(err)
  415. }
  416. errs := startGoroutines(p, "PING")
  417. d.check("before close", p, 1, 1)
  418. p.Close()
  419. timeout := time.After(2 * time.Second)
  420. for i := 0; i < cap(errs); i++ {
  421. select {
  422. case err := <-errs:
  423. switch err {
  424. case nil:
  425. t.Fatal("blocked goroutine did not get error")
  426. case redis.ErrPoolExhausted:
  427. t.Fatal("blocked goroutine got pool exhausted error")
  428. }
  429. case <-timeout:
  430. t.Fatal("timeout waiting for blocked goroutine")
  431. }
  432. }
  433. c.Close()
  434. d.check("done", p, 1, 0)
  435. }
  436. func TestWaitPoolCommandError(t *testing.T) {
  437. testErr := errors.New("test")
  438. d := poolDialer{t: t}
  439. p := &redis.Pool{
  440. MaxIdle: 1,
  441. MaxActive: 1,
  442. Dial: d.dial,
  443. Wait: true,
  444. }
  445. defer p.Close()
  446. c := p.Get()
  447. errs := startGoroutines(p, "ERR", testErr)
  448. d.check("before close", p, 1, 1)
  449. c.Close()
  450. timeout := time.After(2 * time.Second)
  451. for i := 0; i < cap(errs); i++ {
  452. select {
  453. case err := <-errs:
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. case <-timeout:
  458. t.Fatalf("timeout waiting for blocked goroutine %d", i)
  459. }
  460. }
  461. d.check("done", p, cap(errs), 0)
  462. }
  463. func TestWaitPoolDialError(t *testing.T) {
  464. testErr := errors.New("test")
  465. d := poolDialer{t: t}
  466. p := &redis.Pool{
  467. MaxIdle: 1,
  468. MaxActive: 1,
  469. Dial: d.dial,
  470. Wait: true,
  471. }
  472. defer p.Close()
  473. c := p.Get()
  474. errs := startGoroutines(p, "ERR", testErr)
  475. d.check("before close", p, 1, 1)
  476. d.dialErr = errors.New("dial")
  477. c.Close()
  478. nilCount := 0
  479. errCount := 0
  480. timeout := time.After(2 * time.Second)
  481. for i := 0; i < cap(errs); i++ {
  482. select {
  483. case err := <-errs:
  484. switch err {
  485. case nil:
  486. nilCount++
  487. case d.dialErr:
  488. errCount++
  489. default:
  490. t.Fatalf("expected dial error or nil, got %v", err)
  491. }
  492. case <-timeout:
  493. t.Fatalf("timeout waiting for blocked goroutine %d", i)
  494. }
  495. }
  496. if nilCount != 1 {
  497. t.Errorf("expected one nil error, got %d", nilCount)
  498. }
  499. if errCount != cap(errs)-1 {
  500. t.Errorf("expected %d dial erors, got %d", cap(errs)-1, errCount)
  501. }
  502. d.check("done", p, cap(errs), 0)
  503. }
  504. // Borrowing requires us to iterate over the idle connections, unlock the pool,
  505. // and perform a blocking operation to check the connection still works. If
  506. // TestOnBorrow fails, we must reacquire the lock and continue iteration. This
  507. // test ensures that iteration will work correctly if multiple threads are
  508. // iterating simultaneously.
  509. func TestLocking_TestOnBorrowFails_PoolDoesntCrash(t *testing.T) {
  510. const count = 100
  511. // First we'll Create a pool where the pilfering of idle connections fails.
  512. d := poolDialer{t: t}
  513. p := &redis.Pool{
  514. MaxIdle: count,
  515. MaxActive: count,
  516. Dial: d.dial,
  517. TestOnBorrow: func(c redis.Conn, t time.Time) error {
  518. return errors.New("No way back into the real world.")
  519. },
  520. }
  521. defer p.Close()
  522. // Fill the pool with idle connections.
  523. conns := make([]redis.Conn, count)
  524. for i := range conns {
  525. conns[i] = p.Get()
  526. }
  527. for i := range conns {
  528. conns[i].Close()
  529. }
  530. // Spawn a bunch of goroutines to thrash the pool.
  531. var wg sync.WaitGroup
  532. wg.Add(count)
  533. for i := 0; i < count; i++ {
  534. go func() {
  535. c := p.Get()
  536. if c.Err() != nil {
  537. t.Errorf("pool get failed: %v", c.Err())
  538. }
  539. c.Close()
  540. wg.Done()
  541. }()
  542. }
  543. wg.Wait()
  544. if d.dialed != count*2 {
  545. t.Errorf("Expected %d dials, got %d", count*2, d.dialed)
  546. }
  547. }
  548. func BenchmarkPoolGet(b *testing.B) {
  549. b.StopTimer()
  550. p := redis.Pool{Dial: redistest.Dial, MaxIdle: 2}
  551. c := p.Get()
  552. if err := c.Err(); err != nil {
  553. b.Fatal(err)
  554. }
  555. c.Close()
  556. defer p.Close()
  557. b.StartTimer()
  558. for i := 0; i < b.N; i++ {
  559. c = p.Get()
  560. c.Close()
  561. }
  562. }
  563. func BenchmarkPoolGetErr(b *testing.B) {
  564. b.StopTimer()
  565. p := redis.Pool{Dial: redistest.Dial, MaxIdle: 2}
  566. c := p.Get()
  567. if err := c.Err(); err != nil {
  568. b.Fatal(err)
  569. }
  570. c.Close()
  571. defer p.Close()
  572. b.StartTimer()
  573. for i := 0; i < b.N; i++ {
  574. c = p.Get()
  575. if err := c.Err(); err != nil {
  576. b.Fatal(err)
  577. }
  578. c.Close()
  579. }
  580. }
  581. func BenchmarkPoolGetPing(b *testing.B) {
  582. b.StopTimer()
  583. p := redis.Pool{Dial: redistest.Dial, MaxIdle: 2}
  584. c := p.Get()
  585. if err := c.Err(); err != nil {
  586. b.Fatal(err)
  587. }
  588. c.Close()
  589. defer p.Close()
  590. b.StartTimer()
  591. for i := 0; i < b.N; i++ {
  592. c = p.Get()
  593. if _, err := c.Do("PING"); err != nil {
  594. b.Fatal(err)
  595. }
  596. c.Close()
  597. }
  598. }