|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestDefaultConfig(t *testing.T) { |
| 9 | + cfg := DefaultConfig() |
| 10 | + |
| 11 | + // Verify telemetry is disabled by default |
| 12 | + if cfg.Enabled { |
| 13 | + t.Error("Expected telemetry to be disabled by default, got enabled") |
| 14 | + } |
| 15 | + |
| 16 | + // Verify other defaults |
| 17 | + if cfg.BatchSize != 100 { |
| 18 | + t.Errorf("Expected BatchSize 100, got %d", cfg.BatchSize) |
| 19 | + } |
| 20 | + |
| 21 | + if cfg.FlushInterval != 5*time.Second { |
| 22 | + t.Errorf("Expected FlushInterval 5s, got %v", cfg.FlushInterval) |
| 23 | + } |
| 24 | + |
| 25 | + if cfg.MaxRetries != 3 { |
| 26 | + t.Errorf("Expected MaxRetries 3, got %d", cfg.MaxRetries) |
| 27 | + } |
| 28 | + |
| 29 | + if cfg.RetryDelay != 100*time.Millisecond { |
| 30 | + t.Errorf("Expected RetryDelay 100ms, got %v", cfg.RetryDelay) |
| 31 | + } |
| 32 | + |
| 33 | + if !cfg.CircuitBreakerEnabled { |
| 34 | + t.Error("Expected CircuitBreakerEnabled true, got false") |
| 35 | + } |
| 36 | + |
| 37 | + if cfg.CircuitBreakerThreshold != 5 { |
| 38 | + t.Errorf("Expected CircuitBreakerThreshold 5, got %d", cfg.CircuitBreakerThreshold) |
| 39 | + } |
| 40 | + |
| 41 | + if cfg.CircuitBreakerTimeout != 1*time.Minute { |
| 42 | + t.Errorf("Expected CircuitBreakerTimeout 1m, got %v", cfg.CircuitBreakerTimeout) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestParseTelemetryConfig_EmptyParams(t *testing.T) { |
| 47 | + params := map[string]string{} |
| 48 | + cfg := ParseTelemetryConfig(params) |
| 49 | + |
| 50 | + // Should return defaults |
| 51 | + if cfg.Enabled { |
| 52 | + t.Error("Expected telemetry to be disabled by default") |
| 53 | + } |
| 54 | + |
| 55 | + if cfg.BatchSize != 100 { |
| 56 | + t.Errorf("Expected BatchSize 100, got %d", cfg.BatchSize) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestParseTelemetryConfig_EnabledTrue(t *testing.T) { |
| 61 | + params := map[string]string{ |
| 62 | + "telemetry": "true", |
| 63 | + } |
| 64 | + cfg := ParseTelemetryConfig(params) |
| 65 | + |
| 66 | + if !cfg.Enabled { |
| 67 | + t.Error("Expected telemetry to be enabled when set to 'true'") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestParseTelemetryConfig_Enabled1(t *testing.T) { |
| 72 | + params := map[string]string{ |
| 73 | + "telemetry": "1", |
| 74 | + } |
| 75 | + cfg := ParseTelemetryConfig(params) |
| 76 | + |
| 77 | + if !cfg.Enabled { |
| 78 | + t.Error("Expected telemetry to be enabled when set to '1'") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestParseTelemetryConfig_EnabledFalse(t *testing.T) { |
| 83 | + params := map[string]string{ |
| 84 | + "telemetry": "false", |
| 85 | + } |
| 86 | + cfg := ParseTelemetryConfig(params) |
| 87 | + |
| 88 | + if cfg.Enabled { |
| 89 | + t.Error("Expected telemetry to be disabled when set to 'false'") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestParseTelemetryConfig_BatchSize(t *testing.T) { |
| 94 | + params := map[string]string{ |
| 95 | + "telemetry_batch_size": "50", |
| 96 | + } |
| 97 | + cfg := ParseTelemetryConfig(params) |
| 98 | + |
| 99 | + if cfg.BatchSize != 50 { |
| 100 | + t.Errorf("Expected BatchSize 50, got %d", cfg.BatchSize) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestParseTelemetryConfig_BatchSizeInvalid(t *testing.T) { |
| 105 | + params := map[string]string{ |
| 106 | + "telemetry_batch_size": "invalid", |
| 107 | + } |
| 108 | + cfg := ParseTelemetryConfig(params) |
| 109 | + |
| 110 | + // Should fall back to default |
| 111 | + if cfg.BatchSize != 100 { |
| 112 | + t.Errorf("Expected BatchSize to fallback to 100, got %d", cfg.BatchSize) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestParseTelemetryConfig_BatchSizeZero(t *testing.T) { |
| 117 | + params := map[string]string{ |
| 118 | + "telemetry_batch_size": "0", |
| 119 | + } |
| 120 | + cfg := ParseTelemetryConfig(params) |
| 121 | + |
| 122 | + // Should ignore zero and use default |
| 123 | + if cfg.BatchSize != 100 { |
| 124 | + t.Errorf("Expected BatchSize to fallback to 100 when zero, got %d", cfg.BatchSize) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestParseTelemetryConfig_BatchSizeNegative(t *testing.T) { |
| 129 | + params := map[string]string{ |
| 130 | + "telemetry_batch_size": "-10", |
| 131 | + } |
| 132 | + cfg := ParseTelemetryConfig(params) |
| 133 | + |
| 134 | + // Should ignore negative and use default |
| 135 | + if cfg.BatchSize != 100 { |
| 136 | + t.Errorf("Expected BatchSize to fallback to 100 when negative, got %d", cfg.BatchSize) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestParseTelemetryConfig_FlushInterval(t *testing.T) { |
| 141 | + params := map[string]string{ |
| 142 | + "telemetry_flush_interval": "10s", |
| 143 | + } |
| 144 | + cfg := ParseTelemetryConfig(params) |
| 145 | + |
| 146 | + if cfg.FlushInterval != 10*time.Second { |
| 147 | + t.Errorf("Expected FlushInterval 10s, got %v", cfg.FlushInterval) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestParseTelemetryConfig_FlushIntervalInvalid(t *testing.T) { |
| 152 | + params := map[string]string{ |
| 153 | + "telemetry_flush_interval": "invalid", |
| 154 | + } |
| 155 | + cfg := ParseTelemetryConfig(params) |
| 156 | + |
| 157 | + // Should fall back to default |
| 158 | + if cfg.FlushInterval != 5*time.Second { |
| 159 | + t.Errorf("Expected FlushInterval to fallback to 5s, got %v", cfg.FlushInterval) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestParseTelemetryConfig_MultipleParams(t *testing.T) { |
| 164 | + params := map[string]string{ |
| 165 | + "telemetry": "true", |
| 166 | + "telemetry_batch_size": "200", |
| 167 | + "telemetry_flush_interval": "30s", |
| 168 | + } |
| 169 | + cfg := ParseTelemetryConfig(params) |
| 170 | + |
| 171 | + if !cfg.Enabled { |
| 172 | + t.Error("Expected telemetry to be enabled") |
| 173 | + } |
| 174 | + |
| 175 | + if cfg.BatchSize != 200 { |
| 176 | + t.Errorf("Expected BatchSize 200, got %d", cfg.BatchSize) |
| 177 | + } |
| 178 | + |
| 179 | + if cfg.FlushInterval != 30*time.Second { |
| 180 | + t.Errorf("Expected FlushInterval 30s, got %v", cfg.FlushInterval) |
| 181 | + } |
| 182 | + |
| 183 | + // Other fields should still have defaults |
| 184 | + if cfg.MaxRetries != 3 { |
| 185 | + t.Errorf("Expected MaxRetries to remain default 3, got %d", cfg.MaxRetries) |
| 186 | + } |
| 187 | +} |
0 commit comments