list_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 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. // +build go1.9
  15. package redis
  16. import "testing"
  17. func TestPoolList(t *testing.T) {
  18. var idle idleList
  19. var a, b, c idleConn
  20. check := func(ics ...*idleConn) {
  21. if idle.count != len(ics) {
  22. t.Fatal("idle.count != len(ics)")
  23. }
  24. if len(ics) == 0 {
  25. if idle.front != nil {
  26. t.Fatalf("front not nil")
  27. }
  28. if idle.back != nil {
  29. t.Fatalf("back not nil")
  30. }
  31. return
  32. }
  33. if idle.front != ics[0] {
  34. t.Fatal("front != ics[0]")
  35. }
  36. if idle.back != ics[len(ics)-1] {
  37. t.Fatal("back != ics[len(ics)-1]")
  38. }
  39. if idle.front.prev != nil {
  40. t.Fatal("front.prev != nil")
  41. }
  42. if idle.back.next != nil {
  43. t.Fatal("back.next != nil")
  44. }
  45. for i := 1; i < len(ics)-1; i++ {
  46. if ics[i-1].next != ics[i] {
  47. t.Fatal("ics[i-1].next != ics[i]")
  48. }
  49. if ics[i+1].prev != ics[i] {
  50. t.Fatal("ics[i+1].prev != ics[i]")
  51. }
  52. }
  53. }
  54. idle.pushFront(&c)
  55. check(&c)
  56. idle.pushFront(&b)
  57. check(&b, &c)
  58. idle.pushFront(&a)
  59. check(&a, &b, &c)
  60. idle.popFront()
  61. check(&b, &c)
  62. idle.popFront()
  63. check(&c)
  64. idle.popFront()
  65. check()
  66. idle.pushFront(&c)
  67. check(&c)
  68. idle.pushFront(&b)
  69. check(&b, &c)
  70. idle.pushFront(&a)
  71. check(&a, &b, &c)
  72. idle.popBack()
  73. check(&a, &b)
  74. idle.popBack()
  75. check(&a)
  76. idle.popBack()
  77. check()
  78. }