---- 测试:Call 成功 ----
(t *testing.T)
| 187 | // ---- 测试:Call 成功 ---- |
| 188 | |
| 189 | func TestCall_Success(t *testing.T) { |
| 190 | hub := New(Options{PeerExtractor: mdPeerExtractor}) |
| 191 | env := newTestEnv(t, hub) |
| 192 | |
| 193 | cc := env.dial(t) |
| 194 | mock := startNodeMock(t, cc, "node-1") |
| 195 | waitOnline(t, hub, "node-1") |
| 196 | |
| 197 | type req struct{ Foo string } |
| 198 | type resp struct{ Echo string } |
| 199 | |
| 200 | mock.handle("hello", func(reqID string, body []byte) (bool, []byte, string) { |
| 201 | var r req |
| 202 | _ = json.Unmarshal(body, &r) |
| 203 | out, _ := json.Marshal(resp{Echo: "hi-" + r.Foo}) |
| 204 | return true, out, "" |
| 205 | }) |
| 206 | |
| 207 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 208 | defer cancel() |
| 209 | raw, err := hub.Call(ctx, "node-1", "hello", req{Foo: "abc"}) |
| 210 | if err != nil { |
| 211 | t.Fatalf("Call: %v", err) |
| 212 | } |
| 213 | var got resp |
| 214 | if err := json.Unmarshal(raw, &got); err != nil { |
| 215 | t.Fatalf("unmarshal: %v", err) |
| 216 | } |
| 217 | if got.Echo != "hi-abc" { |
| 218 | t.Fatalf("unexpected echo: %q", got.Echo) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // ---- 测试:Call 错误响应 ---- |
| 223 |
nothing calls this directly
no test coverage detected