MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / TestBuild

Function TestBuild

internal/usage/summary_test.go:11–51  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestBuild(t *testing.T) {
12 nodeStore := nodes.NewMemoryStore()
13 userStore := users.NewMemoryStore()
14
15 _, _ = nodeStore.Upsert(nodes.Node{ID: "node-1", Name: "node-1", BaseURL: "http://127.0.0.1:8081"})
16 _, _ = nodeStore.Upsert(nodes.Node{ID: "node-2", Name: "node-2", BaseURL: "http://127.0.0.1:8082"})
17 // 模拟节点累计流量(alice+bob 走 node-1,carol 走 node-2)
18 _ = nodeStore.AddTraffic("node-1", 40, 70)
19 _ = nodeStore.AddTraffic("node-2", 30, 30)
20
21 recentOnline := time.Now().Add(-1 * time.Minute) // 1 分钟前,在线
22 staleOnline := time.Now().Add(-10 * time.Minute) // 10 分钟前,离线
23
24 _, _ = userStore.UpsertUser(users.User{ID: "u1", Username: "alice", Status: users.StatusActive, UploadBytes: 10, DownloadBytes: 20, OnlineAt: &recentOnline})
25 // bob 超限(UsedBytes=80 >= TrafficLimit=70),在线
26 _, _ = userStore.UpsertUser(users.User{ID: "u2", Username: "bob", Status: users.StatusActive, TrafficLimit: 70, UploadBytes: 30, DownloadBytes: 50, OnlineAt: &recentOnline})
27 // carol 禁用,10 分钟前有流量,视为离线
28 _, _ = userStore.UpsertUser(users.User{ID: "u3", Username: "carol", Status: users.StatusDisabled, TrafficLimit: 50, UploadBytes: 30, DownloadBytes: 30, OnlineAt: &staleOnline})
29
30 _, _ = userStore.UpsertUserInbound(users.UserInbound{ID: "u1-ib0", UserID: "u1", NodeID: "node-1", UUID: "uuid-alice", Secret: "secret-alice"})
31 _, _ = userStore.UpsertUserInbound(users.UserInbound{ID: "u2-ib0", UserID: "u2", NodeID: "node-1", UUID: "uuid-bob", Secret: "secret-bob"})
32 _, _ = userStore.UpsertUserInbound(users.UserInbound{ID: "u3-ib0", UserID: "u3", NodeID: "node-2", UUID: "uuid-carol", Secret: "secret-carol"})
33
34 summary, err := Build(nodeStore, userStore, 14)
35 if err != nil {
36 t.Fatalf("Build() error = %v", err)
37 }
38
39 if summary.NodesCount != 2 || summary.UsersCount != 3 {
40 t.Fatalf("unexpected counts: %#v", summary)
41 }
42 if summary.TotalUploadBytes != 70 || summary.TotalDownloadBytes != 100 || summary.TotalUsedBytes != 170 {
43 t.Fatalf("unexpected byte totals: %#v", summary)
44 }
45 if summary.ActiveUsersCount != 1 || summary.LimitedUsersCount != 1 || summary.DisabledUsersCount != 1 || summary.ExpiredUsersCount != 0 {
46 t.Fatalf("unexpected status counts: %#v", summary)
47 }
48 if summary.OnlineUsersCount != 2 {
49 t.Fatalf("expected 2 online users, got %d", summary.OnlineUsersCount)
50 }
51}

Callers

nothing calls this directly

Calls 8

UpsertMethod · 0.95
AddTrafficMethod · 0.95
UpsertUserMethod · 0.95
UpsertUserInboundMethod · 0.95
NewMemoryStoreFunction · 0.92
NewMemoryStoreFunction · 0.92
AddMethod · 0.80
BuildFunction · 0.70

Tested by

no test coverage detected