MCPcopy Create free account
hub / github.com/astercloud/aster / TestCopy_Concurrent

Function TestCopy_Concurrent

pkg/stream/stream_test.go:120–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

118}
119
120func TestCopy_Concurrent(t *testing.T) {
121 reader, writer := Pipe[int](10)
122
123 // 开始写入
124 go func() {
125 defer writer.Close()
126 for i := range 100 {
127 writer.Send(i, nil)
128 }
129 }()
130
131 copies := reader.Copy(3)
132
133 var wg sync.WaitGroup
134 results := make([][]int, 3)
135
136 for i, cp := range copies {
137 wg.Add(1)
138 go func(idx int, r *Reader[int]) {
139 defer wg.Done()
140 var result []int
141 for {
142 v, err := r.Recv()
143 if errors.Is(err, io.EOF) {
144 break
145 }
146 if err != nil {
147 t.Errorf("copy %d: error: %v", idx, err)
148 return
149 }
150 result = append(result, v)
151 }
152 results[idx] = result
153 r.Close()
154 }(i, cp)
155 }
156
157 wg.Wait()
158
159 // 所有副本应该有相同的值
160 for i, result := range results {
161 if len(result) != 100 {
162 t.Errorf("copy %d: expected 100 items, got %d", i, len(result))
163 }
164 }
165}
166
167func TestMerge_Basic(t *testing.T) {
168 r1 := FromSlice([]int{1, 3, 5})

Callers

nothing calls this directly

Calls 6

CopyMethod · 0.80
RecvMethod · 0.80
WaitMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.65
SendMethod · 0.45

Tested by

no test coverage detected