-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcookiemonster_test.go
More file actions
79 lines (66 loc) · 1.78 KB
/
cookiemonster_test.go
File metadata and controls
79 lines (66 loc) · 1.78 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
package cookiemonster_test
import (
"sync/atomic"
"testing"
"time"
"github.com/cookiemonsterproject/cookie-monster"
"github.com/cookiemonsterproject/cookie-monster/mock"
)
func TestCookieJar(t *testing.T) {
t.Parallel()
expectedContent := "test content"
mockCookie := &mock.Cookie{
IDFn: func() string {
return "test-cookie"
},
ContentFn: func() interface{} {
return expectedContent
},
}
var cookies atomic.Value
cookies.Store([]cookiemonster.Cookie{mockCookie})
mockJar := &mock.Jar{
RetrieveFn: func() ([]cookiemonster.Cookie, error) {
return cookies.Load().([]cookiemonster.Cookie), nil
},
RetireFn: func(cookie cookiemonster.Cookie) error {
cookies.Store([]cookiemonster.Cookie{})
return nil
},
}
mockBackoff := &mock.Backoff{
NextFn: func() {},
CurrentFn: func() time.Duration {
return time.Millisecond
},
ResetFn: func() {},
}
digestFn := func(cookie cookiemonster.Cookie) error {
got := cookie.Content()
assertEqual(t, expectedContent, got.(string))
return nil
}
d := cookiemonster.NewDigester(mockJar, cookiemonster.SetWorkers(1), cookiemonster.SetBackoff(mockBackoff))
err := d.Start(digestFn)
assertNil(t, err)
time.Sleep(2 * time.Millisecond)
d.Stop()
assertTrue(t, mockCookie.IDInvoked)
assertTrue(t, mockCookie.ContentInvoked)
assertTrue(t, mockJar.RetrieveInvoked)
assertTrue(t, mockJar.RetireInvoked)
assertTrue(t, mockBackoff.NextInvoked)
assertTrue(t, mockBackoff.CurrentInvoked)
assertTrue(t, mockBackoff.ResetInvoked)
}
func assertEqual(t *testing.T, expected, got interface{}) {
if expected != got {
t.Errorf("expected %v, got %v", expected, got)
}
}
func assertTrue(t *testing.T, got interface{}) {
assertEqual(t, true, got)
}
func assertNil(t *testing.T, got interface{}) {
assertEqual(t, nil, got)
}