|
| 1 | +package blobcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-faker/faker/v4" |
| 7 | + "github.com/mitchellh/mapstructure" |
| 8 | + redis "github.com/redis/go-redis/v9" |
| 9 | + "gotest.tools/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_CopyConfig(t *testing.T) { |
| 13 | + |
| 14 | + var config RedisConfig |
| 15 | + faker.FakeData(&config) |
| 16 | + |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + sentinel bool |
| 20 | + inputConfig RedisConfig |
| 21 | + expectedOptions redis.UniversalOptions |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "simple test", |
| 25 | + inputConfig: RedisConfig{ |
| 26 | + Addrs: []string{"localhost:6379"}, |
| 27 | + }, |
| 28 | + expectedOptions: redis.UniversalOptions{ |
| 29 | + Addrs: []string{"localhost:6379"}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "full config", |
| 34 | + inputConfig: config, |
| 35 | + expectedOptions: redis.UniversalOptions{ |
| 36 | + Addrs: config.Addrs, |
| 37 | + ClientName: config.ClientName, |
| 38 | + Username: config.Username, |
| 39 | + Password: config.Password, |
| 40 | + MinIdleConns: config.MinIdleConns, |
| 41 | + MaxIdleConns: config.MaxIdleConns, |
| 42 | + ConnMaxIdleTime: config.ConnMaxIdleTime, |
| 43 | + ConnMaxLifetime: config.ConnMaxLifetime, |
| 44 | + DialTimeout: config.DialTimeout, |
| 45 | + ReadTimeout: config.ReadTimeout, |
| 46 | + WriteTimeout: config.WriteTimeout, |
| 47 | + MaxRedirects: config.MaxRedirects, |
| 48 | + MaxRetries: config.MaxRetries, |
| 49 | + PoolSize: config.PoolSize, |
| 50 | + RouteByLatency: config.RouteByLatency, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "sentinel config", |
| 55 | + sentinel: true, |
| 56 | + inputConfig: RedisConfig{ |
| 57 | + Addrs: []string{"localhost:26379"}, |
| 58 | + MasterName: "mymaster", |
| 59 | + RouteByLatency: true, |
| 60 | + }, |
| 61 | + expectedOptions: redis.UniversalOptions{ |
| 62 | + Addrs: []string{"localhost:26379"}, |
| 63 | + MasterName: "mymaster", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, test := range tests { |
| 69 | + t.Run(test.name, func(t *testing.T) { |
| 70 | + var actualOptions redis.UniversalOptions |
| 71 | + mapstructure.Decode(test.inputConfig, &actualOptions) |
| 72 | + assert.Equal(t, test.expectedOptions.Addrs[0], actualOptions.Addrs[0]) |
| 73 | + assert.Equal(t, test.expectedOptions.ClientName, actualOptions.ClientName) |
| 74 | + assert.Equal(t, test.expectedOptions.Username, actualOptions.Username) |
| 75 | + assert.Equal(t, test.expectedOptions.Password, actualOptions.Password) |
| 76 | + assert.Equal(t, test.expectedOptions.MinIdleConns, actualOptions.MinIdleConns) |
| 77 | + assert.Equal(t, test.expectedOptions.MaxIdleConns, actualOptions.MaxIdleConns) |
| 78 | + assert.Equal(t, test.expectedOptions.ConnMaxIdleTime, actualOptions.ConnMaxIdleTime) |
| 79 | + assert.Equal(t, test.expectedOptions.ConnMaxLifetime, actualOptions.ConnMaxLifetime) |
| 80 | + |
| 81 | + if test.sentinel { |
| 82 | + failoverOpts := actualOptions.Failover() |
| 83 | + assert.Equal(t, test.inputConfig.Addrs[0], failoverOpts.SentinelAddrs[0]) |
| 84 | + assert.Equal(t, test.inputConfig.MasterName, failoverOpts.MasterName) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments