| package config |
|
|
| import ( |
| "testing" |
| "time" |
| ) |
|
|
| |
| func TestDefaultConstants(t *testing.T) { |
| tests := []struct { |
| name string |
| value int |
| min int |
| max int |
| }{ |
| |
| {"DefaultMaxConcurrency", DefaultMaxConcurrency, 1, 10000}, |
| {"DefaultMaxKeyRetries", DefaultMaxKeyRetries, 1, 10}, |
| {"HTTPMaxIdleConns", HTTPMaxIdleConns, 1, 1000}, |
| {"HTTPMaxIdleConnsPerHost", HTTPMaxIdleConnsPerHost, 1, 1000}, |
| {"HTTPMaxConnsPerHost", HTTPMaxConnsPerHost, 0, 1000}, |
|
|
| |
| {"DefaultLogBufferSize", DefaultLogBufferSize, 100, 100000}, |
| {"DefaultLogWorkers", DefaultLogWorkers, 1, 10}, |
| {"LogBatchSize", LogBatchSize, 1, 1000}, |
|
|
| |
| {"TokenRandomBytes", TokenRandomBytes, 16, 64}, |
| {"DefaultTokenStatsBufferSize", DefaultTokenStatsBufferSize, 100, 100000}, |
|
|
| |
| {"SQLiteMaxOpenConnsFile", SQLiteMaxOpenConnsFile, 1, 100}, |
| {"SQLiteMaxIdleConnsFile", SQLiteMaxIdleConnsFile, 1, 100}, |
|
|
| |
| {"LogFlushTimeoutMs", LogFlushTimeoutMs, 100, 60000}, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| if tt.value < tt.min || tt.value > tt.max { |
| t.Errorf("%s=%d 超出合理范围 [%d, %d]", tt.name, tt.value, tt.min, tt.max) |
| } |
| }) |
| } |
| } |
|
|
| |
| func TestBufferSizeConstants(t *testing.T) { |
| tests := []struct { |
| name string |
| value int |
| min int |
| max int |
| }{ |
| {"TLSSessionCacheSize", TLSSessionCacheSize, 0, 10000}, |
| {"DefaultMaxBodyBytes", DefaultMaxBodyBytes, 1024, 100 * 1024 * 1024}, |
| } |
|
|
| for _, tt := range tests { |
| t.Run(tt.name, func(t *testing.T) { |
| if tt.value < tt.min || tt.value > tt.max { |
| t.Errorf("%s=%d 超出合理范围 [%d, %d]", tt.name, tt.value, tt.min, tt.max) |
| } |
| }) |
| } |
| } |
|
|
| |
| func TestConfigRelationships(t *testing.T) { |
| |
| if SQLiteMaxOpenConnsFile < SQLiteMaxIdleConnsFile { |
| t.Errorf("文件模式: MaxOpenConns(%d) < MaxIdleConns(%d)", |
| SQLiteMaxOpenConnsFile, SQLiteMaxIdleConnsFile) |
| } |
|
|
| |
| if HTTPMaxIdleConns < HTTPMaxIdleConnsPerHost { |
| t.Errorf("HTTP: MaxIdleConns(%d) < MaxIdleConnsPerHost(%d)", |
| HTTPMaxIdleConns, HTTPMaxIdleConnsPerHost) |
| } |
|
|
| |
| if DefaultLogBufferSize < LogBatchSize { |
| t.Errorf("日志: BufferSize(%d) < BatchSize(%d)", |
| DefaultLogBufferSize, LogBatchSize) |
| } |
|
|
| |
| |
| cleanupHours := int(LogCleanupInterval.Hours()) |
| minRetentionHours := 24 |
| if cleanupHours >= minRetentionHours { |
| t.Errorf("日志清理: CleanupInterval(%dh) >= MinRetention(%dh)", |
| cleanupHours, minRetentionHours) |
| } |
| } |
|
|
| |
| func TestHTTPTimeoutValues(t *testing.T) { |
| |
| timeouts := map[string]time.Duration{ |
| "HTTPDialTimeout": HTTPDialTimeout, |
| "HTTPKeepAliveInterval": HTTPKeepAliveInterval, |
| "HTTPTLSHandshakeTimeout": HTTPTLSHandshakeTimeout, |
| } |
|
|
| for name, value := range timeouts { |
| if value <= 0 { |
| t.Errorf("%s=%v 应该大于0", name, value) |
| } |
| } |
| } |
|
|
| |
| func TestLogConfigValues(t *testing.T) { |
| |
| if DefaultLogWorkers < 1 { |
| t.Error("DefaultLogWorkers应该至少为1") |
| } |
| if DefaultLogWorkers > 10 { |
| t.Logf("DefaultLogWorkers=%d 可能过多", DefaultLogWorkers) |
| } |
|
|
| |
| if LogBatchSize > DefaultLogBufferSize { |
| t.Errorf("LogBatchSize(%d) > DefaultLogBufferSize(%d)", |
| LogBatchSize, DefaultLogBufferSize) |
| } |
| } |
|
|