package fan import ( "bytes" "sync" "testing" "github.com/drhodes/golorem" ) // We will be reading some random lorem ipsum text. var data = []byte(lorem.Paragraph(500, 600)) func TestReader(t *testing.T) { r := bytes.NewReader(data) fr := Reader{r, // fan.Reader // Because this is an internal test we need to completely intialise // the struct. But note the API would look like `fan.Reader{r}` []byte(nil), *new(sync.Mutex), } wg := new(sync.WaitGroup) // Start 10x goroutines for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { defer wg.Done() buf := new(bytes.Buffer) n, err := buf.ReadFrom(fr.View()) // Check the error if err != nil { t.Errorf("Goroutine %d err: %v", i, err) return } // Validate the result if int(n) != len(data) { t.Errorf("Goroutine %d err: expected %d bytes, got %d", i, len(data), n) return } t.Logf("Goroutine %d ok: got %d bytes\n", i, len(buf.Bytes())) }(i) } wg.Wait() }