|
1 | 1 | package redis_bloom_go |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "github.com/gomodule/redigo/redis" |
| 5 | + "github.com/stretchr/testify/assert" |
4 | 6 | "testing" |
5 | 7 | ) |
6 | 8 |
|
@@ -30,3 +32,65 @@ func TestNewMultiHostPool(t *testing.T) { |
30 | 32 | }) |
31 | 33 | } |
32 | 34 | } |
| 35 | + |
| 36 | +func TestMultiHostPool_Close(t *testing.T) { |
| 37 | + host, password := getTestConnectionDetails() |
| 38 | + // Test a simple flow |
| 39 | + if password == "" { |
| 40 | + oneMulti := NewMultiHostPool([]string{host}, nil) |
| 41 | + conn := oneMulti.Get() |
| 42 | + assert.NotNil(t, conn) |
| 43 | + err := oneMulti.Close() |
| 44 | + assert.Nil(t, err) |
| 45 | + err = oneMulti.Close() |
| 46 | + assert.NotNil(t, conn) |
| 47 | + severalMulti := NewMultiHostPool([]string{host, host}, nil) |
| 48 | + connMulti := severalMulti.Get() |
| 49 | + assert.NotNil(t, connMulti) |
| 50 | + err = severalMulti.Close() |
| 51 | + assert.Nil(t, err) |
| 52 | + } |
| 53 | + // Exhaustive test |
| 54 | + dial := func() (redis.Conn, error) { |
| 55 | + return redis.Dial("tcp", host, redis.DialPassword(password)) |
| 56 | + } |
| 57 | + pool1 := &redis.Pool{Dial: dial, MaxIdle: maxConns} |
| 58 | + pool2 := &redis.Pool{Dial: dial, MaxIdle: maxConns} |
| 59 | + pool3 := &redis.Pool{Dial: dial, MaxIdle: maxConns} |
| 60 | + //Close pull3 prior to enforce error |
| 61 | + pool3.Close() |
| 62 | + pool4 := &redis.Pool{Dial: dial, MaxIdle: maxConns} |
| 63 | + |
| 64 | + type fields struct { |
| 65 | + pools map[string]*redis.Pool |
| 66 | + hosts []string |
| 67 | + } |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + fields fields |
| 71 | + wantErr bool |
| 72 | + }{ |
| 73 | + {"empty", fields{map[string]*redis.Pool{}, []string{}}, false}, |
| 74 | + {"normal", fields{map[string]*redis.Pool{"hostpool1": pool1}, []string{"hostpool1"}}, false}, |
| 75 | + {"pool3-already-close", fields{map[string]*redis.Pool{"hostpool2": pool2, "hostpool3": pool3, "hostpool4": pool4}, []string{"hostpool2", "hostpool3", "hostpool3"}}, false}, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + p := &MultiHostPool{ |
| 80 | + pools: tt.fields.pools, |
| 81 | + hosts: tt.fields.hosts, |
| 82 | + } |
| 83 | + if err := p.Close(); (err != nil) != tt.wantErr { |
| 84 | + t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr) |
| 85 | + } |
| 86 | + // ensure all connections are really closed |
| 87 | + if !tt.wantErr { |
| 88 | + for _, pool := range p.pools { |
| 89 | + if _, err := pool.Get().Do("PING"); err == nil { |
| 90 | + t.Errorf("expected error after connection closed. Got %v", err) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments