forked from charmbracelet/wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplex_test.go
47 lines (37 loc) · 950 Bytes
/
multiplex_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package wishlist
import (
"bytes"
"io"
"testing"
"testing/iotest"
"github.com/stretchr/testify/require"
)
func TestMultiplex(t *testing.T) {
t.Run("clean", func(t *testing.T) {
var b bytes.Buffer
const s = "this is not a test, this is not a test"
_, err := b.WriteString(s)
require.NoError(t, err)
done := make(chan bool, 1)
t.Cleanup(func() { done <- true })
r1, r2 := multiplex(&b, done)
b1, err := io.ReadAll(r1)
require.NoError(t, err)
require.Equal(t, s, string(b1))
b2, err := io.ReadAll(r2)
require.NoError(t, err)
require.Equal(t, s, string(b2))
})
t.Run("err", func(t *testing.T) {
r := iotest.ErrReader(io.ErrClosedPipe)
done := make(chan bool, 1)
t.Cleanup(func() { done <- true })
r1, r2 := multiplex(r, done)
b1, err := io.ReadAll(r1)
require.NoError(t, err)
require.Empty(t, string(b1))
b2, err := io.ReadAll(r2)
require.NoError(t, err)
require.Empty(t, string(b2))
})
}