-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio_test.go
53 lines (46 loc) · 910 Bytes
/
io_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
48
49
50
51
52
53
package contextio_test
import (
"bytes"
"context"
"testing"
"github.com/dolmen-go/contextio"
)
func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := contextio.NewWriter(context.Background(), &buf)
n, err := w.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatal("5 bytes written expected")
}
if buf.String() != "hello" {
t.Fatal("Bad content")
}
buf.Reset()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
w = contextio.NewWriter(ctx, &buf)
n, err = w.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatal("5 bytes written expected")
}
if buf.String() != "hello" {
t.Fatal("Bad content")
}
cancel()
n, err = w.Write([]byte(", world"))
if err != context.Canceled {
t.Fatal(err)
}
if n != 0 {
t.Fatal("0 bytes written expected")
}
if buf.String() != "hello" {
t.Fatal("Bad content")
}
}