feat: channel test (#6917)

* feat: channel test

* fix: code smell
This commit is contained in:
Seefs
2026-08-18 18:03:59 +08:00
committed by GitHub
parent 2b0efd8484
commit 4add708ebe
17 changed files with 454 additions and 118 deletions
@@ -1,6 +1,7 @@
package operation_setting
import (
"fmt"
"os"
"strconv"
@@ -11,12 +12,17 @@ type MonitorSetting struct {
AutoTestChannelEnabled bool `json:"auto_test_channel_enabled"`
AutoTestChannelMinutes float64 `json:"auto_test_channel_minutes"`
ChannelTestMode string `json:"channel_test_mode"`
ChannelTestConcurrency int `json:"channel_test_concurrency"`
}
const (
ChannelTestModeScheduledAll = "scheduled_all"
ChannelTestModeAutoBanOnly = "auto_ban_only"
ChannelTestModePassiveRecovery = "passive_recovery"
ChannelTestConcurrencyOptionKey = "monitor_setting.channel_test_concurrency"
DefaultChannelTestConcurrency = 1
MaxChannelTestConcurrency = 32
)
// 默认配置
@@ -24,6 +30,7 @@ var monitorSetting = MonitorSetting{
AutoTestChannelEnabled: false,
AutoTestChannelMinutes: 10,
ChannelTestMode: ChannelTestModeScheduledAll,
ChannelTestConcurrency: DefaultChannelTestConcurrency,
}
func init() {
@@ -51,5 +58,24 @@ func GetMonitorSetting() *MonitorSetting {
default:
monitorSetting.ChannelTestMode = ChannelTestModeScheduledAll
}
monitorSetting.ChannelTestConcurrency = NormalizeChannelTestConcurrency(monitorSetting.ChannelTestConcurrency)
return &monitorSetting
}
func NormalizeChannelTestConcurrency(concurrency int) int {
if concurrency < 1 {
return DefaultChannelTestConcurrency
}
if concurrency > MaxChannelTestConcurrency {
return MaxChannelTestConcurrency
}
return concurrency
}
func ValidateChannelTestConcurrency(value string) error {
concurrency, err := strconv.Atoi(value)
if err != nil || concurrency < 1 || concurrency > MaxChannelTestConcurrency {
return fmt.Errorf("channel test concurrency must be between 1 and %d", MaxChannelTestConcurrency)
}
return nil
}
@@ -55,3 +55,37 @@ func TestGetMonitorSettingPreservesAutoBanOnlyMode(t *testing.T) {
require.NotNil(t, setting)
assert.Equal(t, ChannelTestModeAutoBanOnly, setting.ChannelTestMode)
}
func TestGetMonitorSettingNormalizesChannelTestConcurrency(t *testing.T) {
orig := monitorSetting
t.Cleanup(func() { monitorSetting = orig })
tests := []struct {
name string
concurrency int
want int
}{
{name: "missing uses safe default", concurrency: 0, want: DefaultChannelTestConcurrency},
{name: "configured value is preserved", concurrency: 8, want: 8},
{name: "oversized value is capped", concurrency: MaxChannelTestConcurrency + 1, want: MaxChannelTestConcurrency},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
monitorSetting = MonitorSetting{ChannelTestConcurrency: test.concurrency}
setting := GetMonitorSetting()
require.NotNil(t, setting)
assert.Equal(t, test.want, setting.ChannelTestConcurrency)
})
}
}
func TestValidateChannelTestConcurrency(t *testing.T) {
require.NoError(t, ValidateChannelTestConcurrency("1"))
require.NoError(t, ValidateChannelTestConcurrency("32"))
assert.Error(t, ValidateChannelTestConcurrency("0"))
assert.Error(t, ValidateChannelTestConcurrency("33"))
assert.Error(t, ValidateChannelTestConcurrency("1.5"))
}