(t *testing.T)
| 9 | ) |
| 10 | |
| 11 | func 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 | } |
nothing calls this directly
no test coverage detected