-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbucketchecker_coalesced_test.go
More file actions
85 lines (67 loc) · 1.99 KB
/
bucketchecker_coalesced_test.go
File metadata and controls
85 lines (67 loc) · 1.99 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
package gocbcorex
import (
"context"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type bucketCheckResult struct {
Result bool
Error error
}
func TestBucketChecker(t *testing.T) {
resultCh := make(chan *bucketCheckResult, 128)
checkSigCh := make(chan struct{}, 128)
baseChecker := &BucketCheckerMock{
HasBucketFunc: func(ctx context.Context, bucketName string) (bool, error) {
log.Printf("HasBucketFunc")
checkSigCh <- struct{}{}
result := <-resultCh
log.Printf("HasBucketFunc returning %t %s", result.Result, result.Error)
return result.Result, result.Error
},
}
ctx := context.Background()
checker := NewBucketCheckerCoalesced(&BucketCheckerCoalescedOptions{
Checker: baseChecker,
})
testReqCh := make(chan string, 128)
testRespCh := make(chan *bucketCheckResult, 128)
// start 4 threads to process requests
for i := 0; i < 4; i++ {
go func() {
for req := range testReqCh {
log.Printf("Calling HasBucket")
res, err := checker.HasBucket(ctx, req)
log.Printf("Called HasBucket")
testRespCh <- &bucketCheckResult{Result: res, Error: err}
}
}()
}
// send the first request
testReqCh <- "test1"
// wait for the check to start with the mock
<-checkSigCh
// send 2 more requests that should be coalesced together
testReqCh <- "test1"
testReqCh <- "test1"
// need to wait 10 millis for those requests to make it to the coalescing
time.Sleep(10 * time.Millisecond)
// send the result for the first check
resultCh <- &bucketCheckResult{Result: true, Error: nil}
// check that our first request returns true as expected
resp := <-testRespCh
assert.NoError(t, resp.Error)
assert.Equal(t, true, resp.Result)
// send the result for the second two checks
resultCh <- &bucketCheckResult{Result: false, Error: nil}
resp = <-testRespCh
assert.NoError(t, resp.Error)
assert.Equal(t, false, resp.Result)
resp = <-testRespCh
assert.NoError(t, resp.Error)
assert.Equal(t, false, resp.Result)
// kill the goroutines
close(testReqCh)
}