mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-08 14:13:22 +00:00
* fix: prune stale observatory status * More readable Refactor observer to clear removed outbounds instead of updating status. Introduced slices package for improved outbound checking. --------- Co-authored-by: 风扇滑翔翼 <Fangliding.fshxy@outlook.com>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package observatory
|
|
|
|
import "testing"
|
|
|
|
func TestObserverUpdateStatusPrunesStaleOutbounds(t *testing.T) {
|
|
observer := &Observer{
|
|
status: []*OutboundStatus{
|
|
{
|
|
OutboundTag: "keep",
|
|
Alive: true,
|
|
Delay: 42,
|
|
LastErrorReason: "",
|
|
LastSeenTime: 111,
|
|
LastTryTime: 222,
|
|
},
|
|
{
|
|
OutboundTag: "drop",
|
|
Alive: false,
|
|
Delay: 99999999,
|
|
LastErrorReason: "probe failed",
|
|
LastSeenTime: 333,
|
|
LastTryTime: 444,
|
|
},
|
|
},
|
|
}
|
|
|
|
observer.clearRemovedOutbounds([]string{"keep"})
|
|
|
|
if len(observer.status) != 1 {
|
|
t.Fatalf("expected 1 status after pruning, got %d", len(observer.status))
|
|
}
|
|
|
|
got := observer.status[0]
|
|
if got.OutboundTag != "keep" {
|
|
t.Fatalf("expected remaining status for keep, got %q", got.OutboundTag)
|
|
}
|
|
if !got.Alive {
|
|
t.Fatal("expected remaining status to preserve Alive field")
|
|
}
|
|
if got.Delay != 42 {
|
|
t.Fatalf("expected remaining status to preserve Delay, got %d", got.Delay)
|
|
}
|
|
if got.LastSeenTime != 111 {
|
|
t.Fatalf("expected remaining status to preserve LastSeenTime, got %d", got.LastSeenTime)
|
|
}
|
|
if got.LastTryTime != 222 {
|
|
t.Fatalf("expected remaining status to preserve LastTryTime, got %d", got.LastTryTime)
|
|
}
|
|
}
|
|
|
|
func TestObserverUpdateStatusClearsWhenNoOutboundsRemain(t *testing.T) {
|
|
observer := &Observer{
|
|
status: []*OutboundStatus{
|
|
{OutboundTag: "drop-1"},
|
|
{OutboundTag: "drop-2"},
|
|
},
|
|
}
|
|
|
|
observer.clearRemovedOutbounds(nil)
|
|
|
|
if len(observer.status) != 0 {
|
|
t.Fatalf("expected all statuses to be removed, got %d", len(observer.status))
|
|
}
|
|
}
|