-
Notifications
You must be signed in to change notification settings - Fork 41
/
consumer_test.go
73 lines (56 loc) · 1.15 KB
/
consumer_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
package main
import (
"fmt"
"strings"
"testing"
)
func NewTestResolver() *TestResolver {
return &TestResolver{}
}
type TestResolver struct{}
func (s *TestResolver) IsBucket(name string) bool {
return strings.Contains(name, "s3")
}
func TestConsume(t *testing.T) {
inputChannel := make(chan string)
resultChannel := make(chan string)
done2 := make(chan bool)
done3 := make(chan bool)
resolver := NewTestResolver()
consumer := NewConsumer(resolver, inputChannel, resultChannel, done2)
go consumer.Consume()
var got []string
// consumer
go func() {
for {
j, more := <-resultChannel
if more {
got = append(got, j)
} else {
done3 <- true
return
}
}
}()
// producer
for k := 1; k <= 5; k++ {
inputChannel <- fmt.Sprintf("test%v", k)
}
inputChannel <- "foos3"
inputChannel <- "foos3asdf"
close(inputChannel)
<-done2
close(resultChannel)
// block
<-done3
// assertions
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)
}
}
}