TestIntegration_HelloAndCall 验证 hello 含 config_hash + hub.Call → dispatcher 路由。
(t *testing.T)
| 80 | |
| 81 | // TestIntegration_HelloAndCall 验证 hello 含 config_hash + hub.Call → dispatcher 路由。 |
| 82 | func TestIntegration_HelloAndCall(t *testing.T) { |
| 83 | t.Parallel() |
| 84 | ph := &integrationPushHandler{} |
| 85 | hub, dialer, stop := startHub(t, ph) |
| 86 | defer stop() |
| 87 | |
| 88 | api := newTestAPI() |
| 89 | disp := NewAPIDispatcher(api, nil) |
| 90 | |
| 91 | ctx, cancel := context.WithCancel(context.Background()) |
| 92 | defer cancel() |
| 93 | |
| 94 | cfg := Config{ |
| 95 | NodeID: "n1", |
| 96 | ServerAddr: "passthrough:///bufnet", |
| 97 | Dispatcher: disp, |
| 98 | HelloProvider: DefaultHelloProvider("n1", ConfigHasher(api)), |
| 99 | Dialer: func(_ context.Context) (*grpc.ClientConn, error) { |
| 100 | return grpc.NewClient("passthrough:///bufnet", |
| 101 | grpc.WithContextDialer(dialer), |
| 102 | grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 103 | ) |
| 104 | }, |
| 105 | ReconnectBackoff: []time.Duration{20 * time.Millisecond}, |
| 106 | } |
| 107 | done := make(chan error, 1) |
| 108 | go func() { done <- Run(ctx, cfg) }() |
| 109 | defer func() { cancel(); <-done }() |
| 110 | |
| 111 | deadline := time.Now().Add(2 * time.Second) |
| 112 | for time.Now().Before(deadline) { |
| 113 | if hb, _ := ph.snapshot(); len(hb) > 0 { |
| 114 | break |
| 115 | } |
| 116 | time.Sleep(20 * time.Millisecond) |
| 117 | } |
| 118 | hb, _ := ph.snapshot() |
| 119 | if len(hb) == 0 { |
| 120 | t.Fatalf("no hello received") |
| 121 | } |
| 122 | var payload struct { |
| 123 | NodeID string `json:"node_id"` |
| 124 | ConfigHash string `json:"config_hash"` |
| 125 | Version string `json:"version"` |
| 126 | } |
| 127 | if err := json.Unmarshal(hb[0], &payload); err != nil { |
| 128 | t.Fatalf("decode hello: %v", err) |
| 129 | } |
| 130 | if payload.NodeID != "n1" { |
| 131 | t.Fatalf("hello node_id=%q want n1", payload.NodeID) |
| 132 | } |
| 133 | // xray 未启动时 config 字符串为空,hash 也为空;这是已知行为。 |
| 134 | _ = payload.ConfigHash |
| 135 | |
| 136 | // 等 hub 看到这个 conn 上线。 |
| 137 | deadline = time.Now().Add(time.Second) |
| 138 | for time.Now().Before(deadline) && !hub.IsOnline("n1") { |
| 139 | time.Sleep(10 * time.Millisecond) |
nothing calls this directly
no test coverage detected