TestIntegration_UsagePushAck 端到端:node push usage_push → hub 自动 ack → node 调 DoUsage(reset=true) 推进 baseline。
(t *testing.T)
| 159 | // TestIntegration_UsagePushAck 端到端:node push usage_push → hub 自动 ack |
| 160 | // → node 调 DoUsage(reset=true) 推进 baseline。 |
| 161 | func TestIntegration_UsagePushAck(t *testing.T) { |
| 162 | t.Parallel() |
| 163 | ph := &integrationPushHandler{} |
| 164 | _, dialer, stop := startHub(t, ph) |
| 165 | defer stop() |
| 166 | |
| 167 | usageAPI := &countingUsageAPI{} |
| 168 | pusher := NewUsagePusher(usageAPI, time.Hour) |
| 169 | pusher.SetAckTimeout(2 * time.Second) |
| 170 | |
| 171 | ctx, cancel := context.WithCancel(context.Background()) |
| 172 | defer cancel() |
| 173 | |
| 174 | cfg := Config{ |
| 175 | NodeID: "n1", |
| 176 | ServerAddr: "passthrough:///bufnet", |
| 177 | Dispatcher: NoopDispatcher{}, |
| 178 | HelloProvider: DefaultHelloProvider("n1", nil), |
| 179 | Dialer: func(_ context.Context) (*grpc.ClientConn, error) { |
| 180 | return grpc.NewClient("passthrough:///bufnet", |
| 181 | grpc.WithContextDialer(dialer), |
| 182 | grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 183 | ) |
| 184 | }, |
| 185 | OnConnected: func(_ context.Context, s Sender) { |
| 186 | pusher.SetSender(s) |
| 187 | }, |
| 188 | } |
| 189 | done := make(chan error, 1) |
| 190 | go func() { done <- Run(ctx, cfg) }() |
| 191 | defer func() { cancel(); <-done }() |
| 192 | |
| 193 | deadline := time.Now().Add(2 * time.Second) |
| 194 | for time.Now().Before(deadline) { |
| 195 | if pusher.currentSender() != nil { |
| 196 | break |
| 197 | } |
| 198 | time.Sleep(20 * time.Millisecond) |
| 199 | } |
| 200 | if pusher.currentSender() == nil { |
| 201 | t.Fatalf("sender not injected") |
| 202 | } |
| 203 | |
| 204 | // 第一次 tick:priming reset,不 push。 |
| 205 | pusher.tick(ctx) |
| 206 | if got := usageAPI.resets.Load(); got != 1 { |
| 207 | t.Fatalf("priming reset count=%d want 1", got) |
| 208 | } |
| 209 | |
| 210 | // 第二次 tick:push;hub 自动 ack;节点端 ack 后再 reset。 |
| 211 | pusher.tick(ctx) |
| 212 | |
| 213 | deadline = time.Now().Add(3 * time.Second) |
| 214 | for time.Now().Before(deadline) { |
| 215 | _, ups := ph.snapshot() |
| 216 | if len(ups) >= 1 && pusher.PendingCount() == 0 && usageAPI.resets.Load() >= 2 { |
| 217 | break |
| 218 | } |
nothing calls this directly
no test coverage detected