| 93 | } |
| 94 | |
| 95 | func TestCopy_Independent(t *testing.T) { |
| 96 | reader := FromSlice([]int{1, 2, 3}) |
| 97 | copies := reader.Copy(3) |
| 98 | |
| 99 | if len(copies) != 3 { |
| 100 | t.Fatalf("expected 3 copies, got %d", len(copies)) |
| 101 | } |
| 102 | |
| 103 | // 每个副本应该能独立读取所有值 |
| 104 | for i, cp := range copies { |
| 105 | result, err := cp.Collect() |
| 106 | if err != nil { |
| 107 | t.Errorf("copy %d: unexpected error: %v", i, err) |
| 108 | } |
| 109 | if len(result) != 3 { |
| 110 | t.Errorf("copy %d: expected 3 items, got %d", i, len(result)) |
| 111 | } |
| 112 | for j, v := range result { |
| 113 | if v != j+1 { |
| 114 | t.Errorf("copy %d, index %d: expected %d, got %d", i, j, j+1, v) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestCopy_Concurrent(t *testing.T) { |
| 121 | reader, writer := Pipe[int](10) |