-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshared_test.go
More file actions
171 lines (151 loc) · 3.8 KB
/
Copy pathshared_test.go
File metadata and controls
171 lines (151 loc) · 3.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package fastsync
import (
"bytes"
"io"
"net"
"sort"
"sync"
"testing"
"time"
)
func TestCompressedReadWriteCloserRoundTrip(t *testing.T) {
left, right := net.Pipe()
leftCompressed := CompressedReadWriteCloser(left)
rightCompressed := CompressedReadWriteCloser(right)
want := []byte("compressed payload")
errCh := make(chan error, 1)
go func() {
_, err := leftCompressed.Write(want)
errCh <- err
}()
got := make([]byte, len(want))
if _, err := io.ReadFull(rightCompressed, got); err != nil {
t.Fatalf("read compressed payload: %v", err)
}
if err := <-errCh; err != nil {
t.Fatalf("write compressed payload: %v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("payload = %q, want %q", got, want)
}
if err := leftCompressed.Close(); err != nil {
t.Fatalf("close left compressed conn: %v", err)
}
if err := rightCompressed.Close(); err != nil {
t.Fatalf("close right compressed conn: %v", err)
}
}
func TestPerformanceHistoryDoesNotLoseConcurrentAdds(t *testing.T) {
p := NewPerformance()
const goroutines = 8
const additions = 1000
var workers sync.WaitGroup
workers.Add(goroutines)
for range goroutines {
go func() {
defer workers.Done()
for range additions {
p.Add(WrittenBytes, 1)
}
}()
}
var total PerformanceEntry
done := make(chan struct{})
go func() {
workers.Wait()
close(done)
}()
for {
total = total.Add(p.NextHistory())
select {
case <-done:
total = total.Add(p.NextHistory())
if got, want := total.Get(WrittenBytes), uint64(goroutines*additions); got != want {
t.Fatalf("collected WrittenBytes = %d, want %d", got, want)
}
return
default:
}
}
}
func TestPerformanceHistoryBound(t *testing.T) {
p := NewPerformance()
p.maxhistory = 2
for i := 0; i < 5; i++ {
p.Add(FilesProcessed, uint64(i+1))
p.NextHistory()
}
if got, want := len(p.entries), p.maxhistory; got != want {
t.Fatalf("history length = %d, want %d", got, want)
}
}
func TestPerformanceCounters(t *testing.T) {
p := NewPerformance()
p.Add(FilesProcessed, 2)
p.GetAtomicAdder(FilesProcessed)(3)
p.Add(WrittenBytes, 7)
if got, want := p.Get(FilesProcessed), uint64(5); got != want {
t.Fatalf("FilesProcessed = %d, want %d", got, want)
}
if got, want := p.Get(WrittenBytes), uint64(7); got != want {
t.Fatalf("WrittenBytes = %d, want %d", got, want)
}
history := p.NextHistory()
if got, want := history.Get(FilesProcessed), uint64(5); got != want {
t.Fatalf("history FilesProcessed = %d, want %d", got, want)
}
if got := p.Get(FilesProcessed); got != 0 {
t.Fatalf("current FilesProcessed after NextHistory = %d, want 0", got)
}
total := history.Add(PerformanceEntry{})
if got, want := total.Get(WrittenBytes), uint64(7); got != want {
t.Fatalf("added WrittenBytes = %d, want %d", got, want)
}
}
func TestStackEmitsSubmittedItemsAndCloses(t *testing.T) {
stack, out, in := NewStack[int](8, 2)
for i := 0; i < 10; i++ {
in <- i
}
if got := stack.Len(); got == 0 {
t.Fatal("stack length = 0 while items are queued, want non-zero")
}
stack.Close()
var got []int
for item := range out {
got = append(got, item)
}
sort.Ints(got)
if len(got) != 10 {
t.Fatalf("got %d items, want 10: %v", len(got), got)
}
for i, item := range got {
if item != i {
t.Fatalf("sorted item %d = %d, want %d; all items %v", i, item, i, got)
}
}
}
func TestStackAcceptsRecursiveWorkWhenOutputIsBackpressured(t *testing.T) {
stack, out, in := NewStack[int](1, 1)
in <- 1
in <- 2
queued := make(chan struct{})
go func() {
in <- 3
in <- 4
close(queued)
}()
select {
case <-queued:
case <-time.After(time.Second):
t.Fatal("stack stopped accepting input while output was backpressured")
}
stack.Close()
var got []int
for item := range out {
got = append(got, item)
}
if len(got) != 4 {
t.Fatalf("received %d items, want 4: %v", len(got), got)
}
}