|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + ct "github.com/launchdarkly/go-configtypes" |
| 8 | + "github.com/launchdarkly/ld-relay/v8/config" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNewCacheConfigFromRelay(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + relayConfig config.ObjectCacheConfig |
| 17 | + expectedConfig CacheConfig |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "all values set", |
| 21 | + relayConfig: config.ObjectCacheConfig{ |
| 22 | + Enabled: true, |
| 23 | + MaxObjects: mustNewOptInt(5000), |
| 24 | + TTL: ct.NewOptDuration(10 * time.Minute), |
| 25 | + }, |
| 26 | + expectedConfig: CacheConfig{ |
| 27 | + Enabled: true, |
| 28 | + MaxObjects: 5000, |
| 29 | + TTL: 10 * time.Minute, |
| 30 | + }, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "disabled cache", |
| 34 | + relayConfig: config.ObjectCacheConfig{ |
| 35 | + Enabled: false, |
| 36 | + MaxObjects: mustNewOptInt(1000), |
| 37 | + TTL: ct.NewOptDuration(2 * time.Minute), |
| 38 | + }, |
| 39 | + expectedConfig: CacheConfig{ |
| 40 | + Enabled: false, |
| 41 | + MaxObjects: 1000, |
| 42 | + TTL: 2 * time.Minute, |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "default values used when not set", |
| 47 | + relayConfig: config.ObjectCacheConfig{ |
| 48 | + Enabled: true, |
| 49 | + // MaxObjects and TTL not set - should use defaults |
| 50 | + }, |
| 51 | + expectedConfig: CacheConfig{ |
| 52 | + Enabled: true, |
| 53 | + MaxObjects: 10000, // default value |
| 54 | + TTL: 5 * time.Minute, // default value |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "empty config uses all defaults", |
| 59 | + relayConfig: config.ObjectCacheConfig{ |
| 60 | + // All fields at zero values |
| 61 | + }, |
| 62 | + expectedConfig: CacheConfig{ |
| 63 | + Enabled: false, // default value |
| 64 | + MaxObjects: 10000, // default value |
| 65 | + TTL: 5 * time.Minute, // default value |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := NewCacheConfigFromRelay(tt.relayConfig) |
| 73 | + assert.Equal(t, tt.expectedConfig, result) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestDefaultObjectCacheConfig(t *testing.T) { |
| 79 | + config := config.ObjectCacheConfig{} |
| 80 | + |
| 81 | + assert.False(t, config.Enabled) // Should be disabled by default for safety |
| 82 | + // MaxObjects and TTL are not set, so they will have zero values |
| 83 | +} |
| 84 | + |
| 85 | +func TestCacheConfigValidation(t *testing.T) { |
| 86 | + // Test that valid configurations are accepted |
| 87 | + validConfig := CacheConfig{ |
| 88 | + Enabled: true, |
| 89 | + MaxObjects: 1000, |
| 90 | + TTL: time.Minute, |
| 91 | + } |
| 92 | + assert.True(t, validConfig.Enabled) |
| 93 | + assert.Equal(t, 1000, validConfig.MaxObjects) |
| 94 | + assert.Equal(t, time.Minute, validConfig.TTL) |
| 95 | + |
| 96 | + // Test edge cases |
| 97 | + edgeConfig := CacheConfig{ |
| 98 | + Enabled: true, |
| 99 | + MaxObjects: 1, // Minimum reasonable value |
| 100 | + TTL: time.Millisecond, // Very short TTL |
| 101 | + } |
| 102 | + assert.True(t, edgeConfig.Enabled) |
| 103 | + assert.Equal(t, 1, edgeConfig.MaxObjects) |
| 104 | + assert.Equal(t, time.Millisecond, edgeConfig.TTL) |
| 105 | +} |
| 106 | + |
| 107 | +func TestOptIntGreaterThanZeroIntegration(t *testing.T) { |
| 108 | + // Test that the configtypes integration works correctly |
| 109 | + optInt := mustNewOptInt(42) |
| 110 | + |
| 111 | + relayConfig := config.ObjectCacheConfig{ |
| 112 | + Enabled: true, |
| 113 | + MaxObjects: optInt, |
| 114 | + } |
| 115 | + |
| 116 | + cacheConfig := NewCacheConfigFromRelay(relayConfig) |
| 117 | + assert.Equal(t, 42, cacheConfig.MaxObjects) |
| 118 | +} |
| 119 | + |
| 120 | +func TestOptDurationIntegration(t *testing.T) { |
| 121 | + // Test that the configtypes integration works correctly |
| 122 | + optDuration := ct.NewOptDuration(30 * time.Second) |
| 123 | + |
| 124 | + relayConfig := config.ObjectCacheConfig{ |
| 125 | + Enabled: true, |
| 126 | + TTL: optDuration, |
| 127 | + } |
| 128 | + |
| 129 | + cacheConfig := NewCacheConfigFromRelay(relayConfig) |
| 130 | + assert.Equal(t, 30*time.Second, cacheConfig.TTL) |
| 131 | +} |
| 132 | + |
| 133 | +// Helper function that panics on error for test simplicity |
| 134 | +func mustNewOptInt(value int) ct.OptIntGreaterThanZero { |
| 135 | + opt, err := ct.NewOptIntGreaterThanZero(value) |
| 136 | + if err != nil { |
| 137 | + panic(err) |
| 138 | + } |
| 139 | + return opt |
| 140 | +} |
| 141 | + |
0 commit comments