pool17_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.7
  15. package redis_test
  16. import (
  17. "context"
  18. "testing"
  19. "github.com/gomodule/redigo/redis"
  20. )
  21. func TestWaitPoolGetContext(t *testing.T) {
  22. d := poolDialer{t: t}
  23. p := &redis.Pool{
  24. MaxIdle: 1,
  25. MaxActive: 1,
  26. Dial: d.dial,
  27. Wait: true,
  28. }
  29. defer p.Close()
  30. c, err := p.GetContext(context.Background())
  31. if err != nil {
  32. t.Fatalf("GetContext returned %v", err)
  33. }
  34. defer c.Close()
  35. }
  36. func TestWaitPoolGetAfterClose(t *testing.T) {
  37. d := poolDialer{t: t}
  38. p := &redis.Pool{
  39. MaxIdle: 1,
  40. MaxActive: 1,
  41. Dial: d.dial,
  42. Wait: true,
  43. }
  44. p.Close()
  45. _, err := p.GetContext(context.Background())
  46. if err == nil {
  47. t.Fatal("expected error")
  48. }
  49. }
  50. func TestWaitPoolGetCanceledContext(t *testing.T) {
  51. d := poolDialer{t: t}
  52. p := &redis.Pool{
  53. MaxIdle: 1,
  54. MaxActive: 1,
  55. Dial: d.dial,
  56. Wait: true,
  57. }
  58. defer p.Close()
  59. ctx, f := context.WithCancel(context.Background())
  60. f()
  61. c := p.Get()
  62. defer c.Close()
  63. _, err := p.GetContext(ctx)
  64. if err != context.Canceled {
  65. t.Fatalf("got error %v, want %v", err, context.Canceled)
  66. }
  67. }