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

Function TestCallStream_LogFrames

internal/nodehub/stream_test.go:15–90  ·  view source on GitHub ↗

TestCallStream_LogFrames:CallStream 发起后,node 推送多条 log 帧 + 终态 ok → caller 收到所有帧,Done 关闭,Err()==nil。

(t *testing.T)

Source from the content-addressed store, hash-verified

13// TestCallStream_LogFrames:CallStream 发起后,node 推送多条 log 帧 + 终态 ok
14// → caller 收到所有帧,Done 关闭,Err()==nil。
15func TestCallStream_LogFrames(t *testing.T) {
16 hub := New(Options{PeerExtractor: mdPeerExtractor})
17 env := newTestEnv(t, hub)
18
19 cc := env.dial(t)
20 mock := startNodeMock(t, cc, "node-stream")
21 waitOnline(t, hub, "node-stream")
22
23 // node 收到 LogsStream 时推 3 帧 log,再发终态 Ok。
24 mock.handle("LogsStream", func(reqID string, _ []byte) (bool, []byte, string) {
25 // 在 mock 层 handle 必须返回响应,所以我们这里返回 ok=true 表示流终态;
26 // 帧通过 sendNode 单独 push。
27 for i := 0; i < 3; i++ {
28 body, _ := json.Marshal(map[string]string{"line": "log-" + string(rune('A'+i))})
29 mock.sendNode(&nodev1.NodeMessage{Id: reqID, Event: "log", Body: body})
30 }
31 return true, nil, ""
32 })
33
34 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
35 defer cancel()
36 stream, err := hub.CallStream(ctx, "node-stream", "LogsStream", nil)
37 if err != nil {
38 t.Fatalf("CallStream: %v", err)
39 }
40 defer stream.Close()
41
42 got := []string{}
43 timeout := time.After(3 * time.Second)
44loop:
45 for {
46 select {
47 case f, ok := <-stream.Frames():
48 if !ok {
49 break loop
50 }
51 if f.Event != "log" {
52 t.Fatalf("unexpected event: %q", f.Event)
53 }
54 var p struct {
55 Line string `json:"line"`
56 }
57 if err := json.Unmarshal(f.Body, &p); err != nil {
58 t.Fatalf("decode: %v", err)
59 }
60 got = append(got, p.Line)
61 case <-stream.Done():
62 // 排干 frames(终态前缓冲的剩余帧)
63 for {
64 select {
65 case f, ok := <-stream.Frames():
66 if !ok {
67 break loop
68 }
69 var p struct {
70 Line string `json:"line"`
71 }
72 if err := json.Unmarshal(f.Body, &p); err != nil {

Callers

nothing calls this directly

Calls 12

newTestEnvFunction · 0.85
startNodeMockFunction · 0.85
waitOnlineFunction · 0.85
dialMethod · 0.80
sendNodeMethod · 0.80
CallStreamMethod · 0.80
NewFunction · 0.70
CloseMethod · 0.65
FramesMethod · 0.65
DoneMethod · 0.65
ErrMethod · 0.65
handleMethod · 0.45

Tested by

no test coverage detected