pool_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. "io"
  17. "net/textproto"
  18. "reflect"
  19. "sync"
  20. "testing"
  21. "time"
  22. )
  23. type poolTestConn struct {
  24. d *poolDialer
  25. err error
  26. Conn
  27. }
  28. func (c *poolTestConn) Close() error { c.d.open -= 1; return nil }
  29. func (c *poolTestConn) Err() error { return c.err }
  30. func (c *poolTestConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  31. if commandName == "ERR" {
  32. c.err = args[0].(error)
  33. }
  34. if commandName != "" {
  35. c.d.commands = append(c.d.commands, commandName)
  36. }
  37. return c.Conn.Do(commandName, args...)
  38. }
  39. func (c *poolTestConn) Send(commandName string, args ...interface{}) error {
  40. c.d.commands = append(c.d.commands, commandName)
  41. return c.Conn.Send(commandName, args...)
  42. }
  43. type poolDialer struct {
  44. t *testing.T
  45. dialed, open int
  46. commands []string
  47. }
  48. func (d *poolDialer) dial() (Conn, error) {
  49. d.open += 1
  50. d.dialed += 1
  51. c, err := DialTestDB()
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &poolTestConn{d: d, Conn: c}, nil
  56. }
  57. func (d *poolDialer) check(message string, p *Pool, dialed, open int) {
  58. if d.dialed != dialed {
  59. d.t.Errorf("%s: dialed=%d, want %d", message, d.dialed, dialed)
  60. }
  61. if d.open != open {
  62. d.t.Errorf("%s: open=%d, want %d", message, d.open, open)
  63. }
  64. if active := p.ActiveCount(); active != open {
  65. d.t.Errorf("%s: active=%d, want %d", message, active, open)
  66. }
  67. }
  68. func TestPoolReuse(t *testing.T) {
  69. d := poolDialer{t: t}
  70. p := &Pool{
  71. MaxIdle: 2,
  72. Dial: d.dial,
  73. }
  74. for i := 0; i < 10; i++ {
  75. c1 := p.Get()
  76. c1.Do("PING")
  77. c2 := p.Get()
  78. c2.Do("PING")
  79. c1.Close()
  80. c2.Close()
  81. }
  82. d.check("before close", p, 2, 2)
  83. p.Close()
  84. d.check("after close", p, 2, 0)
  85. }
  86. func TestPoolMaxIdle(t *testing.T) {
  87. d := poolDialer{t: t}
  88. p := &Pool{
  89. MaxIdle: 2,
  90. Dial: d.dial,
  91. }
  92. for i := 0; i < 10; i++ {
  93. c1 := p.Get()
  94. c1.Do("PING")
  95. c2 := p.Get()
  96. c2.Do("PING")
  97. c3 := p.Get()
  98. c3.Do("PING")
  99. c1.Close()
  100. c2.Close()
  101. c3.Close()
  102. }
  103. d.check("before close", p, 12, 2)
  104. p.Close()
  105. d.check("after close", p, 12, 0)
  106. }
  107. func TestPoolError(t *testing.T) {
  108. d := poolDialer{t: t}
  109. p := &Pool{
  110. MaxIdle: 2,
  111. Dial: d.dial,
  112. }
  113. c := p.Get()
  114. c.Do("ERR", io.EOF)
  115. if c.Err() == nil {
  116. t.Errorf("expected c.Err() != nil")
  117. }
  118. c.Close()
  119. c = p.Get()
  120. c.Do("ERR", io.EOF)
  121. c.Close()
  122. d.check(".", p, 2, 0)
  123. }
  124. func TestPoolClose(t *testing.T) {
  125. d := poolDialer{t: t}
  126. p := &Pool{
  127. MaxIdle: 2,
  128. Dial: d.dial,
  129. }
  130. c1 := p.Get()
  131. c1.Do("PING")
  132. c2 := p.Get()
  133. c2.Do("PING")
  134. c3 := p.Get()
  135. c3.Do("PING")
  136. c1.Close()
  137. if _, err := c1.Do("PING"); err == nil {
  138. t.Errorf("expected error after connection closed")
  139. }
  140. c2.Close()
  141. c2.Close()
  142. p.Close()
  143. d.check("after pool close", p, 3, 1)
  144. if _, err := c1.Do("PING"); err == nil {
  145. t.Errorf("expected error after connection and pool closed")
  146. }
  147. c3.Close()
  148. d.check("after conn close", p, 3, 0)
  149. c1 = p.Get()
  150. if _, err := c1.Do("PING"); err == nil {
  151. t.Errorf("expected error after pool closed")
  152. }
  153. }
  154. func TestPoolTimeout(t *testing.T) {
  155. d := poolDialer{t: t}
  156. p := &Pool{
  157. MaxIdle: 2,
  158. IdleTimeout: 300 * time.Second,
  159. Dial: d.dial,
  160. }
  161. now := time.Now()
  162. nowFunc = func() time.Time { return now }
  163. defer func() { nowFunc = time.Now }()
  164. c := p.Get()
  165. c.Do("PING")
  166. c.Close()
  167. d.check("1", p, 1, 1)
  168. now = now.Add(p.IdleTimeout)
  169. c = p.Get()
  170. c.Do("PING")
  171. c.Close()
  172. d.check("2", p, 2, 1)
  173. p.Close()
  174. }
  175. func TestPoolConcurrenSendReceive(t *testing.T) {
  176. p := &Pool{
  177. Dial: DialTestDB,
  178. }
  179. c := p.Get()
  180. done := make(chan error, 1)
  181. go func() {
  182. _, err := c.Receive()
  183. done <- err
  184. }()
  185. c.Send("PING")
  186. c.Flush()
  187. err := <-done
  188. if err != nil {
  189. t.Fatalf("Receive() returned error %v", err)
  190. }
  191. _, err = c.Do("")
  192. if err != nil {
  193. t.Fatalf("Do() returned error %v", err)
  194. }
  195. c.Close()
  196. p.Close()
  197. }
  198. func TestPoolBorrowCheck(t *testing.T) {
  199. d := poolDialer{t: t}
  200. p := &Pool{
  201. MaxIdle: 2,
  202. Dial: d.dial,
  203. TestOnBorrow: func(Conn, time.Time) error { return Error("BLAH") },
  204. }
  205. for i := 0; i < 10; i++ {
  206. c := p.Get()
  207. c.Do("PING")
  208. c.Close()
  209. }
  210. d.check("1", p, 10, 1)
  211. p.Close()
  212. }
  213. func TestPoolMaxActive(t *testing.T) {
  214. d := poolDialer{t: t}
  215. p := &Pool{
  216. MaxIdle: 2,
  217. MaxActive: 2,
  218. Dial: d.dial,
  219. }
  220. c1 := p.Get()
  221. c1.Do("PING")
  222. c2 := p.Get()
  223. c2.Do("PING")
  224. d.check("1", p, 2, 2)
  225. c3 := p.Get()
  226. if _, err := c3.Do("PING"); err != ErrPoolExhausted {
  227. t.Errorf("expected pool exhausted")
  228. }
  229. c3.Close()
  230. d.check("2", p, 2, 2)
  231. c2.Close()
  232. d.check("3", p, 2, 2)
  233. c3 = p.Get()
  234. if _, err := c3.Do("PING"); err != nil {
  235. t.Errorf("expected good channel, err=%v", err)
  236. }
  237. c3.Close()
  238. d.check("4", p, 2, 2)
  239. p.Close()
  240. }
  241. func TestPoolMonitorCleanup(t *testing.T) {
  242. d := poolDialer{t: t}
  243. p := &Pool{
  244. MaxIdle: 2,
  245. MaxActive: 2,
  246. Dial: d.dial,
  247. }
  248. c := p.Get()
  249. c.Send("MONITOR")
  250. c.Close()
  251. d.check("", p, 1, 0)
  252. p.Close()
  253. }
  254. func TestPoolPubSubCleanup(t *testing.T) {
  255. d := poolDialer{t: t}
  256. p := &Pool{
  257. MaxIdle: 2,
  258. MaxActive: 2,
  259. Dial: d.dial,
  260. }
  261. c := p.Get()
  262. c.Send("SUBSCRIBE", "x")
  263. c.Close()
  264. want := []string{"SUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  265. if !reflect.DeepEqual(d.commands, want) {
  266. t.Errorf("got commands %v, want %v", d.commands, want)
  267. }
  268. d.commands = nil
  269. c = p.Get()
  270. c.Send("PSUBSCRIBE", "x*")
  271. c.Close()
  272. want = []string{"PSUBSCRIBE", "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. p.Close()
  278. }
  279. func TestPoolTransactionCleanup(t *testing.T) {
  280. d := poolDialer{t: t}
  281. p := &Pool{
  282. MaxIdle: 2,
  283. MaxActive: 2,
  284. Dial: d.dial,
  285. }
  286. c := p.Get()
  287. c.Do("WATCH", "key")
  288. c.Do("PING")
  289. c.Close()
  290. want := []string{"WATCH", "PING", "UNWATCH"}
  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()
  296. c.Do("WATCH", "key")
  297. c.Do("UNWATCH")
  298. c.Do("PING")
  299. c.Close()
  300. want = []string{"WATCH", "UNWATCH", "PING"}
  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()
  306. c.Do("WATCH", "key")
  307. c.Do("MULTI")
  308. c.Do("PING")
  309. c.Close()
  310. want = []string{"WATCH", "MULTI", "PING", "DISCARD"}
  311. if !reflect.DeepEqual(d.commands, want) {
  312. t.Errorf("got commands %v, want %v", d.commands, want)
  313. }
  314. d.commands = nil
  315. c = p.Get()
  316. c.Do("WATCH", "key")
  317. c.Do("MULTI")
  318. c.Do("DISCARD")
  319. c.Do("PING")
  320. c.Close()
  321. want = []string{"WATCH", "MULTI", "DISCARD", "PING"}
  322. if !reflect.DeepEqual(d.commands, want) {
  323. t.Errorf("got commands %v, want %v", d.commands, want)
  324. }
  325. d.commands = nil
  326. c = p.Get()
  327. c.Do("WATCH", "key")
  328. c.Do("MULTI")
  329. c.Do("EXEC")
  330. c.Do("PING")
  331. c.Close()
  332. want = []string{"WATCH", "MULTI", "EXEC", "PING"}
  333. if !reflect.DeepEqual(d.commands, want) {
  334. t.Errorf("got commands %v, want %v", d.commands, want)
  335. }
  336. d.commands = nil
  337. p.Close()
  338. }
  339. func BenchmarkPoolGet(b *testing.B) {
  340. b.StopTimer()
  341. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  342. c := p.Get()
  343. if err := c.Err(); err != nil {
  344. b.Fatal(err)
  345. }
  346. c.Close()
  347. defer p.Close()
  348. b.StartTimer()
  349. for i := 0; i < b.N; i++ {
  350. c = p.Get()
  351. c.Close()
  352. }
  353. }
  354. func BenchmarkPoolGetErr(b *testing.B) {
  355. b.StopTimer()
  356. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  357. c := p.Get()
  358. if err := c.Err(); err != nil {
  359. b.Fatal(err)
  360. }
  361. c.Close()
  362. defer p.Close()
  363. b.StartTimer()
  364. for i := 0; i < b.N; i++ {
  365. c = p.Get()
  366. if err := c.Err(); err != nil {
  367. b.Fatal(err)
  368. }
  369. c.Close()
  370. }
  371. }
  372. func BenchmarkPoolGetPing(b *testing.B) {
  373. b.StopTimer()
  374. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  375. c := p.Get()
  376. if err := c.Err(); err != nil {
  377. b.Fatal(err)
  378. }
  379. c.Close()
  380. defer p.Close()
  381. b.StartTimer()
  382. for i := 0; i < b.N; i++ {
  383. c = p.Get()
  384. if _, err := c.Do("PING"); err != nil {
  385. b.Fatal(err)
  386. }
  387. c.Close()
  388. }
  389. }
  390. const numConcurrent = 10
  391. func BenchmarkPipelineConcurrency(b *testing.B) {
  392. b.StopTimer()
  393. c, err := DialTestDB()
  394. if err != nil {
  395. b.Fatalf("error connection to database, %v", err)
  396. }
  397. defer c.Close()
  398. var wg sync.WaitGroup
  399. wg.Add(numConcurrent)
  400. var pipeline textproto.Pipeline
  401. b.StartTimer()
  402. for i := 0; i < numConcurrent; i++ {
  403. go func() {
  404. defer wg.Done()
  405. for i := 0; i < b.N; i++ {
  406. id := pipeline.Next()
  407. pipeline.StartRequest(id)
  408. c.Send("PING")
  409. c.Flush()
  410. pipeline.EndRequest(id)
  411. pipeline.StartResponse(id)
  412. _, err := c.Receive()
  413. if err != nil {
  414. b.Fatal(err)
  415. }
  416. c.Flush()
  417. pipeline.EndResponse(id)
  418. }
  419. }()
  420. }
  421. wg.Wait()
  422. }
  423. func BenchmarkPoolConcurrency(b *testing.B) {
  424. b.StopTimer()
  425. p := Pool{Dial: DialTestDB, MaxIdle: numConcurrent}
  426. defer p.Close()
  427. // fill the pool
  428. conns := make([]Conn, numConcurrent)
  429. for i := range conns {
  430. c := p.Get()
  431. if err := c.Err(); err != nil {
  432. b.Fatal(err)
  433. }
  434. conns[i] = c
  435. }
  436. for _, c := range conns {
  437. c.Close()
  438. }
  439. var wg sync.WaitGroup
  440. wg.Add(numConcurrent)
  441. b.StartTimer()
  442. for i := 0; i < numConcurrent; i++ {
  443. go func() {
  444. defer wg.Done()
  445. for i := 0; i < b.N; i++ {
  446. c := p.Get()
  447. c.Do("PING")
  448. c.Close()
  449. }
  450. }()
  451. }
  452. wg.Wait()
  453. }