test: stabilize channel affinity cache keys (#8009)

Replace wall-clock identifiers with an atomic sequence and test names so repeated Windows runs cannot share usage-cache entries.

Verification: latest main baseline failed with cumulative counts; patched affinity tests passed -count=20; full service suite and go vet passed.

Signed-off-by: sstar16 <3221386464@qq.com>
This commit is contained in:
sstar16 authored and GitHub committed 2026-10-25 21:29:23 +08:00
1 parent 0165fef98d
commit 34003e9c2e
1 file changed
+12 -7
+12 -7
View File
@@ -3,8 +3,8 @@ package service
import (
"fmt"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
@@ -12,6 +12,14 @@ import (
"github.com/stretchr/testify/require"
)
var channelAffinityTestSeq atomic.Int64
func affinityTestKeys(t *testing.T) (string, string) {
t.Helper()
key := fmt.Sprintf("%s:%d", t.Name(), channelAffinityTestSeq.Add(1))
return key, key
}
func buildChannelAffinityStatsContextForTest(ruleName, usingGroup, keyFP string) *gin.Context {
rec := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(rec)
@@ -26,9 +34,8 @@ func buildChannelAffinityStatsContextForTest(ruleName, usingGroup, keyFP string)
}
func TestObserveChannelAffinityUsageCacheByRelayFormat_ClaudeMode(t *testing.T) {
ruleName := fmt.Sprintf("rule_%d", time.Now().UnixNano())
ruleName, keyFP := affinityTestKeys(t)
usingGroup := "default"
keyFP := fmt.Sprintf("fp_%d", time.Now().UnixNano())
ctx := buildChannelAffinityStatsContextForTest(ruleName, usingGroup, keyFP)
usage := &dto.Usage{
@@ -53,9 +60,8 @@ func TestObserveChannelAffinityUsageCacheByRelayFormat_ClaudeMode(t *testing.T)
}
func TestObserveChannelAffinityUsageCacheByRelayFormat_MixedMode(t *testing.T) {
ruleName := fmt.Sprintf("rule_%d", time.Now().UnixNano())
ruleName, keyFP := affinityTestKeys(t)
usingGroup := "default"
keyFP := fmt.Sprintf("fp_%d", time.Now().UnixNano())
ctx := buildChannelAffinityStatsContextForTest(ruleName, usingGroup, keyFP)
openAIUsage := &dto.Usage{
@@ -83,9 +89,8 @@ func TestObserveChannelAffinityUsageCacheByRelayFormat_MixedMode(t *testing.T) {
}
func TestObserveChannelAffinityUsageCacheByRelayFormat_UnsupportedModeKeepsEmpty(t *testing.T) {
ruleName := fmt.Sprintf("rule_%d", time.Now().UnixNano())
ruleName, keyFP := affinityTestKeys(t)
usingGroup := "default"
keyFP := fmt.Sprintf("fp_%d", time.Now().UnixNano())
ctx := buildChannelAffinityStatsContextForTest(ruleName, usingGroup, keyFP)
usage := &dto.Usage{