mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-01 15:05:46 +03:00
c7f5850e5f
The three regressions for a node counter that restarts below its baseline, and the migration test for the column that fix added, shared fixed node ids and inbound tags. Run as a group against PostgreSQL, the database in the report, the second one failed on a duplicate key before reaching the code under test.
Give each case its own PostgreSQL schema and run all four in the PostgreSQL CI job, requiring four passes. No panel code changes; the fix itself is already on main in 408fd682.
Refs #7271
107 lines
4.0 KiB
Go
107 lines
4.0 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
)
|
|
|
|
func initFrozenDipTestDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
cleanup, err := testpg.IsolatePackage("node_frozen_dip")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(cleanup)
|
|
return initTrafficTestDB(t)
|
|
}
|
|
|
|
func syncDirtyNode(t *testing.T, svc *InboundService, nodeID int, tag, settings string, stats ...xray.ClientTraffic) {
|
|
t.Helper()
|
|
snap := &runtime.TrafficSnapshot{
|
|
Inbounds: []*model.Inbound{{Tag: tag, Settings: settings, ClientStats: stats}},
|
|
}
|
|
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, true, false); err != nil {
|
|
t.Fatalf("dirty setRemoteTrafficLocked node %d: %v", nodeID, err)
|
|
}
|
|
}
|
|
|
|
// A node counter that restarted below the baseline while the node is dirty must
|
|
// add the other field's growth once, not again on every tick.
|
|
func TestNodeFrozenDip_IdleClientCountedOnce(t *testing.T) {
|
|
db := initFrozenDipTestDB(t)
|
|
svc := &InboundService{}
|
|
|
|
const email = "idle@x"
|
|
seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
|
|
createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
|
|
settings := `{"clients":[{"email":"idle@x","enable":true}]}`
|
|
for range 2 {
|
|
syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 2000, Down: 3000, Enable: true})
|
|
}
|
|
before := readTraffic(t, db, email)
|
|
|
|
for range 3 {
|
|
syncDirtyNode(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 100, Down: 18000, Enable: true})
|
|
}
|
|
|
|
assertUpDown(t, readTraffic(t, db, email), before.Up, before.Down+15000, "after three dirty ticks of an idle client")
|
|
}
|
|
|
|
// Usage a client makes below the pre-restart baseline while the node is dirty
|
|
// must still be counted.
|
|
func TestNodeFrozenDip_UsageBelowOldBaselineCounted(t *testing.T) {
|
|
db := initFrozenDipTestDB(t)
|
|
svc := &InboundService{}
|
|
|
|
const email = "active@x"
|
|
seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
|
|
createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
|
|
settings := `{"clients":[{"email":"active@x","enable":true}]}`
|
|
for range 2 {
|
|
syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 5000, Down: 5000, Enable: true})
|
|
}
|
|
before := readTraffic(t, db, email)
|
|
|
|
for _, used := range []int64{100, 1000, 4000} {
|
|
syncDirtyNode(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: used, Down: used, Enable: true})
|
|
}
|
|
|
|
assertUpDown(t, readTraffic(t, db, email), before.Up+3900, before.Down+3900, "after the restarted counter grew to 4000")
|
|
}
|
|
|
|
// A node-side renewal seen while dirty must still be adopted on the first clean
|
|
// tick, even after the client's usage outgrew the pre-renewal baseline.
|
|
func TestNodeFrozenDip_RenewalSurvivesUsagePastOldBaseline(t *testing.T) {
|
|
db := initFrozenDipTestDB(t)
|
|
createNodeInbound(t, db, 1, "n1-in", 41001)
|
|
svc := &InboundService{}
|
|
|
|
const email = "renew-then-use"
|
|
syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{
|
|
Email: email, Up: 100, Down: 100, ExpiryTime: earlyAbs, Enable: true, Reset: 30,
|
|
})
|
|
if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).
|
|
Updates(map[string]any{"expiry_time": earlyAbs, "enable": true, "up": int64(100), "down": int64(100)}).Error; err != nil {
|
|
t.Fatalf("seed master: %v", err)
|
|
}
|
|
|
|
renewed := xray.ClientTraffic{Email: email, Up: 10, Down: 10, ExpiryTime: lateAbs, Enable: true, Reset: 30}
|
|
syncDirtyNode(t, svc, 1, "n1-in", "", renewed)
|
|
renewed.Up, renewed.Down = 500, 500
|
|
syncDirtyNode(t, svc, 1, "n1-in", "", renewed)
|
|
if got := readTraffic(t, db, email).ExpiryTime; got != earlyAbs {
|
|
t.Fatalf("renewal while dirty raised expiry: got %d want %d", got, earlyAbs)
|
|
}
|
|
|
|
syncNode(t, svc, 1, "n1-in", renewed)
|
|
if got := readTraffic(t, db, email).ExpiryTime; got != lateAbs {
|
|
t.Fatalf("renewal after the dirty flag cleared did not apply: got %d want %d", got, lateAbs)
|
|
}
|
|
}
|