pool_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. "reflect"
  18. "testing"
  19. "time"
  20. )
  21. type poolTestConn struct {
  22. d *poolDialer
  23. err error
  24. Conn
  25. }
  26. func (c *poolTestConn) Close() error { c.d.open -= 1; return nil }
  27. func (c *poolTestConn) Err() error { return c.err }
  28. func (c *poolTestConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
  29. if commandName == "ERR" {
  30. c.err = args[0].(error)
  31. }
  32. if commandName != "" {
  33. c.d.commands = append(c.d.commands, commandName)
  34. }
  35. return c.Conn.Do(commandName, args...)
  36. }
  37. func (c *poolTestConn) Send(commandName string, args ...interface{}) error {
  38. c.d.commands = append(c.d.commands, commandName)
  39. return c.Conn.Send(commandName, args...)
  40. }
  41. type poolDialer struct {
  42. t *testing.T
  43. dialed, open int
  44. commands []string
  45. }
  46. func (d *poolDialer) dial() (Conn, error) {
  47. d.open += 1
  48. d.dialed += 1
  49. c, err := DialTestDB()
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &poolTestConn{d: d, Conn: c}, nil
  54. }
  55. func (d *poolDialer) check(message string, p *Pool, dialed, open int) {
  56. if d.dialed != dialed {
  57. d.t.Errorf("%s: dialed=%d, want %d", message, d.dialed, dialed)
  58. }
  59. if d.open != open {
  60. d.t.Errorf("%s: open=%d, want %d", message, d.open, open)
  61. }
  62. if active := p.ActiveCount(); active != open {
  63. d.t.Errorf("%s: active=%d, want %d", message, active, open)
  64. }
  65. }
  66. func TestPoolReuse(t *testing.T) {
  67. d := poolDialer{t: t}
  68. p := &Pool{
  69. MaxIdle: 2,
  70. Dial: d.dial,
  71. }
  72. for i := 0; i < 10; i++ {
  73. c1 := p.Get()
  74. c1.Do("PING")
  75. c2 := p.Get()
  76. c2.Do("PING")
  77. c1.Close()
  78. c2.Close()
  79. }
  80. d.check("before close", p, 2, 2)
  81. p.Close()
  82. d.check("after close", p, 2, 0)
  83. }
  84. func TestPoolMaxIdle(t *testing.T) {
  85. d := poolDialer{t: t}
  86. p := &Pool{
  87. MaxIdle: 2,
  88. Dial: d.dial,
  89. }
  90. for i := 0; i < 10; i++ {
  91. c1 := p.Get()
  92. c1.Do("PING")
  93. c2 := p.Get()
  94. c2.Do("PING")
  95. c3 := p.Get()
  96. c3.Do("PING")
  97. c1.Close()
  98. c2.Close()
  99. c3.Close()
  100. }
  101. d.check("before close", p, 12, 2)
  102. p.Close()
  103. d.check("after close", p, 12, 0)
  104. }
  105. func TestPoolError(t *testing.T) {
  106. d := poolDialer{t: t}
  107. p := &Pool{
  108. MaxIdle: 2,
  109. Dial: d.dial,
  110. }
  111. c := p.Get()
  112. c.Do("ERR", io.EOF)
  113. if c.Err() == nil {
  114. t.Errorf("expected c.Err() != nil")
  115. }
  116. c.Close()
  117. c = p.Get()
  118. c.Do("ERR", io.EOF)
  119. c.Close()
  120. d.check(".", p, 2, 0)
  121. }
  122. func TestPoolClose(t *testing.T) {
  123. d := poolDialer{t: t}
  124. p := &Pool{
  125. MaxIdle: 2,
  126. Dial: d.dial,
  127. }
  128. c1 := p.Get()
  129. c1.Do("PING")
  130. c2 := p.Get()
  131. c2.Do("PING")
  132. c3 := p.Get()
  133. c3.Do("PING")
  134. c1.Close()
  135. if _, err := c1.Do("PING"); err == nil {
  136. t.Errorf("expected error after connection closed")
  137. }
  138. c2.Close()
  139. p.Close()
  140. d.check("after pool close", p, 3, 1)
  141. if _, err := c1.Do("PING"); err == nil {
  142. t.Errorf("expected error after connection and pool closed")
  143. }
  144. c3.Close()
  145. d.check("after channel close", p, 3, 0)
  146. c1 = p.Get()
  147. if _, err := c1.Do("PING"); err == nil {
  148. t.Errorf("expected error after pool closed")
  149. }
  150. }
  151. func TestPoolTimeout(t *testing.T) {
  152. d := poolDialer{t: t}
  153. p := &Pool{
  154. MaxIdle: 2,
  155. IdleTimeout: 300 * time.Second,
  156. Dial: d.dial,
  157. }
  158. now := time.Now()
  159. nowFunc = func() time.Time { return now }
  160. defer func() { nowFunc = time.Now }()
  161. c := p.Get()
  162. c.Do("PING")
  163. c.Close()
  164. d.check("1", p, 1, 1)
  165. now = now.Add(p.IdleTimeout)
  166. c = p.Get()
  167. c.Do("PING")
  168. c.Close()
  169. d.check("2", p, 2, 1)
  170. p.Close()
  171. }
  172. func TestBorrowCheck(t *testing.T) {
  173. d := poolDialer{t: t}
  174. p := &Pool{
  175. MaxIdle: 2,
  176. Dial: d.dial,
  177. TestOnBorrow: func(Conn, time.Time) error { return Error("BLAH") },
  178. }
  179. for i := 0; i < 10; i++ {
  180. c := p.Get()
  181. c.Do("PING")
  182. c.Close()
  183. }
  184. d.check("1", p, 10, 1)
  185. p.Close()
  186. }
  187. func TestMaxActive(t *testing.T) {
  188. d := poolDialer{t: t}
  189. p := &Pool{
  190. MaxIdle: 2,
  191. MaxActive: 2,
  192. Dial: d.dial,
  193. }
  194. c1 := p.Get()
  195. c1.Do("PING")
  196. c2 := p.Get()
  197. c2.Do("PING")
  198. d.check("1", p, 2, 2)
  199. c3 := p.Get()
  200. if _, err := c3.Do("PING"); err != ErrPoolExhausted {
  201. t.Errorf("expected pool exhausted")
  202. }
  203. c3.Close()
  204. d.check("2", p, 2, 2)
  205. c2.Close()
  206. d.check("3", p, 2, 2)
  207. c3 = p.Get()
  208. if _, err := c3.Do("PING"); err != nil {
  209. t.Errorf("expected good channel, err=%v", err)
  210. }
  211. c3.Close()
  212. d.check("4", p, 2, 2)
  213. p.Close()
  214. }
  215. func TestMonitorCleanup(t *testing.T) {
  216. d := poolDialer{t: t}
  217. p := &Pool{
  218. MaxIdle: 2,
  219. MaxActive: 2,
  220. Dial: d.dial,
  221. }
  222. c := p.Get()
  223. c.Send("MONITOR")
  224. c.Close()
  225. d.check("", p, 1, 0)
  226. p.Close()
  227. }
  228. func TestPubSubCleanup(t *testing.T) {
  229. d := poolDialer{t: t}
  230. p := &Pool{
  231. MaxIdle: 2,
  232. MaxActive: 2,
  233. Dial: d.dial,
  234. }
  235. c := p.Get()
  236. c.Send("SUBSCRIBE", "x")
  237. c.Close()
  238. want := []string{"SUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  239. if !reflect.DeepEqual(d.commands, want) {
  240. t.Errorf("got commands %v, want %v", d.commands, want)
  241. }
  242. d.commands = nil
  243. c = p.Get()
  244. c.Send("PSUBSCRIBE", "x*")
  245. c.Close()
  246. want = []string{"PSUBSCRIBE", "UNSUBSCRIBE", "PUNSUBSCRIBE", "ECHO"}
  247. if !reflect.DeepEqual(d.commands, want) {
  248. t.Errorf("got commands %v, want %v", d.commands, want)
  249. }
  250. d.commands = nil
  251. p.Close()
  252. }
  253. func TestTransactionCleanup(t *testing.T) {
  254. d := poolDialer{t: t}
  255. p := &Pool{
  256. MaxIdle: 2,
  257. MaxActive: 2,
  258. Dial: d.dial,
  259. }
  260. c := p.Get()
  261. c.Do("WATCH", "key")
  262. c.Do("PING")
  263. c.Close()
  264. want := []string{"WATCH", "PING", "UNWATCH"}
  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.Do("WATCH", "key")
  271. c.Do("UNWATCH")
  272. c.Do("PING")
  273. c.Close()
  274. want = []string{"WATCH", "UNWATCH", "PING"}
  275. if !reflect.DeepEqual(d.commands, want) {
  276. t.Errorf("got commands %v, want %v", d.commands, want)
  277. }
  278. d.commands = nil
  279. c = p.Get()
  280. c.Do("WATCH", "key")
  281. c.Do("MULTI")
  282. c.Do("PING")
  283. c.Close()
  284. want = []string{"WATCH", "MULTI", "PING", "DISCARD"}
  285. if !reflect.DeepEqual(d.commands, want) {
  286. t.Errorf("got commands %v, want %v", d.commands, want)
  287. }
  288. d.commands = nil
  289. c = p.Get()
  290. c.Do("WATCH", "key")
  291. c.Do("MULTI")
  292. c.Do("DISCARD")
  293. c.Do("PING")
  294. c.Close()
  295. want = []string{"WATCH", "MULTI", "DISCARD", "PING"}
  296. if !reflect.DeepEqual(d.commands, want) {
  297. t.Errorf("got commands %v, want %v", d.commands, want)
  298. }
  299. d.commands = nil
  300. c = p.Get()
  301. c.Do("WATCH", "key")
  302. c.Do("MULTI")
  303. c.Do("EXEC")
  304. c.Do("PING")
  305. c.Close()
  306. want = []string{"WATCH", "MULTI", "EXEC", "PING"}
  307. if !reflect.DeepEqual(d.commands, want) {
  308. t.Errorf("got commands %v, want %v", d.commands, want)
  309. }
  310. d.commands = nil
  311. p.Close()
  312. }
  313. func BenchmarkPoolGet(b *testing.B) {
  314. b.StopTimer()
  315. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  316. c := p.Get()
  317. if err := c.Err(); err != nil {
  318. b.Fatal(err)
  319. }
  320. c.Close()
  321. defer p.Close()
  322. b.StartTimer()
  323. for i := 0; i < b.N; i++ {
  324. c = p.Get()
  325. c.Close()
  326. }
  327. }
  328. func BenchmarkPoolGetErr(b *testing.B) {
  329. b.StopTimer()
  330. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  331. c := p.Get()
  332. if err := c.Err(); err != nil {
  333. b.Fatal(err)
  334. }
  335. c.Close()
  336. defer p.Close()
  337. b.StartTimer()
  338. for i := 0; i < b.N; i++ {
  339. c = p.Get()
  340. if err := c.Err(); err != nil {
  341. b.Fatal(err)
  342. }
  343. c.Close()
  344. }
  345. }
  346. func BenchmarkPoolGetPing(b *testing.B) {
  347. b.StopTimer()
  348. p := Pool{Dial: DialTestDB, MaxIdle: 2}
  349. c := p.Get()
  350. if err := c.Err(); err != nil {
  351. b.Fatal(err)
  352. }
  353. c.Close()
  354. defer p.Close()
  355. b.StartTimer()
  356. for i := 0; i < b.N; i++ {
  357. c = p.Get()
  358. if _, err := c.Do("PING"); err != nil {
  359. b.Fatal(err)
  360. }
  361. c.Close()
  362. }
  363. }