pool_test.go 13 KB

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