-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_writer_test.go
84 lines (74 loc) · 1.6 KB
/
sync_writer_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package clog
import (
"bytes"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSyncWriterConcurrency(t *testing.T) {
iterations := 5000
buffer := &bytes.Buffer{}
writer := NewSyncWriter(buffer)
wg := sync.WaitGroup{}
wg.Add(iterations)
for i := 0; i < iterations; i++ {
go func(num int) {
n, err := writer.Write([]byte("123456789\n"))
assert.Equal(t, 10, n)
assert.NoError(t, err)
wg.Done()
}(i)
}
wg.Wait()
lines := 0
for line, err := buffer.ReadString('\n'); err == nil; line, err = buffer.ReadString('\n') {
assert.Len(t, line, 10)
lines++
}
assert.Equal(t, iterations, lines)
}
type fdBuffer struct {
bytes.Buffer
}
func (b fdBuffer) Fd() uintptr {
return 0
}
func TestSyncFdWriterConcurrency(t *testing.T) {
iterations := 5000
buffer := &fdBuffer{}
writer := NewSyncWriter(buffer)
wg := sync.WaitGroup{}
wg.Add(iterations)
for i := 0; i < iterations; i++ {
go func(num int) {
n, err := writer.Write([]byte("123456789\n"))
assert.Equal(t, 10, n)
assert.NoError(t, err)
wg.Done()
}(i)
}
wg.Wait()
lines := 0
for line, err := buffer.ReadString('\n'); err == nil; line, err = buffer.ReadString('\n') {
assert.Len(t, line, 10)
lines++
}
assert.Equal(t, iterations, lines)
}
func TestSyncWriterNoFd(t *testing.T) {
_, ok := NewSyncWriter(&bytes.Buffer{}).(interface {
Fd() uintptr
})
if ok {
t.Error("NewSyncWriter should not expose a Fd method")
}
}
func TestSyncWriterFd(t *testing.T) {
_, ok := NewSyncWriter(os.Stdout).(interface {
Fd() uintptr
})
if !ok {
t.Error("NewSyncWriter does not pass through Fd method")
}
}