Files
3x-ui-web-src/internal/web/job/node_traffic_sync_job_test.go
Sanaei f727d04f65
CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
v3.7.0
2026-08-24 15:07:15 +02:00

113 lines
2.5 KiB
Go

package job
import (
"sync"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestAtomicBool_DefaultIsFalse(t *testing.T) {
var a atomicBool
if a.takeAndReset() {
t.Fatal("default atomicBool should report false")
}
}
func TestAtomicBool_SetThenTakeReturnsTrueOnce(t *testing.T) {
var a atomicBool
a.set()
if !a.takeAndReset() {
t.Fatal("takeAndReset after set should return true")
}
if a.takeAndReset() {
t.Fatal("second takeAndReset should return false (state was reset)")
}
}
func TestAtomicBool_SetIsIdempotent(t *testing.T) {
var a atomicBool
a.set()
a.set()
a.set()
if !a.takeAndReset() {
t.Fatal("repeated set should still leave the flag true")
}
if a.takeAndReset() {
t.Fatal("flag should be cleared after the first take")
}
}
func TestAtomicBool_ConcurrentSettersExactlyOneTakeWins(t *testing.T) {
var a atomicBool
const setters = 100
const readers = 20
var wg sync.WaitGroup
for range setters {
wg.Go(func() {
a.set()
})
}
wg.Wait()
trueCount := 0
var rwg sync.WaitGroup
var mu sync.Mutex
for range readers {
rwg.Go(func() {
if a.takeAndReset() {
mu.Lock()
trueCount++
mu.Unlock()
}
})
}
rwg.Wait()
if trueCount != 1 {
t.Fatalf("expected exactly one reader to observe true, got %d", trueCount)
}
}
// Regression (#6283): a node onboarded in selected mode with an empty tag
// list empties its snapshot via FilterNodeSnapshot before the merge sees it,
// so that sync adopts nothing and must not stamp InboundsAdoptedAt.
func TestSyncCanAdoptInbounds(t *testing.T) {
cases := []struct {
name string
node *model.Node
aliases []string
expected bool
}{
{"all mode always adopts", &model.Node{InboundSyncMode: "all"}, nil, true},
{
"selected with tags adopts",
&model.Node{InboundSyncMode: "selected", InboundTags: []string{"in-443-tcp"}},
nil,
true,
},
{
"selected empty with adopted alias adopts",
&model.Node{InboundSyncMode: "selected"},
[]string{"in-443-tcp"},
true,
},
{
// The reported bug: registering in selected mode and choosing
// tags afterwards stamped adoption while adopting nothing.
"selected empty with no aliases adopts nothing",
&model.Node{InboundSyncMode: "selected"},
nil,
false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := syncCanAdoptInbounds(c.node, c.aliases); got != c.expected {
t.Fatalf("syncCanAdoptInbounds(%+v, %v) = %v, want %v", c.node, c.aliases, got, c.expected)
}
})
}
}