-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain_test.go
More file actions
94 lines (76 loc) · 1.51 KB
/
main_test.go
File metadata and controls
94 lines (76 loc) · 1.51 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
package main
import (
"bytes"
"context"
"fmt"
"strings"
"sync"
"testing"
)
type TestResolver struct{}
func (s *TestResolver) IsBucket(_ context.Context, name string) bool {
return strings.Contains(name, "s3")
}
func (s *TestResolver) Stats() Stats {
return Stats{}
}
func TestConsume(t *testing.T) {
input := make(chan string)
results := make(chan string)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
consume(context.Background(), &TestResolver{}, input, results)
}()
var got []string
var collectWg sync.WaitGroup
collectWg.Add(1)
go func() {
defer collectWg.Done()
for j := range results {
got = append(got, j)
}
}()
for k := 1; k <= 5; k++ {
input <- fmt.Sprintf("test%v", k)
}
input <- "foos3"
input <- "foos3asdf"
close(input)
wg.Wait()
close(results)
collectWg.Wait()
expected := []string{"foos3", "foos3asdf"}
if len(expected) != len(got) {
t.Fatalf("expected %v, got %v", expected, got)
}
for i := range got {
if got[i] != expected[i] {
t.Fatalf("expected %v, got %v", expected, got)
}
}
}
func TestPrintResults(t *testing.T) {
channel := make(chan string)
var buf bytes.Buffer
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
printResults(channel, &buf)
}()
for i := 1; i <= 5; i++ {
channel <- fmt.Sprintf("test%v", i)
}
close(channel)
wg.Wait()
expected := "test1\n" +
"test2\n" +
"test3\n" +
"test4\n" +
"test5\n"
if got := buf.String(); got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}