-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
169 lines (150 loc) · 3.33 KB
/
pool_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
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
package permz
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
func ExampleResolverFactory() {
var GetUserProjectPermissions func(context.Context, int, int) ([]int, error)
var NewResolverFromPermissions func([]int) PermissionResolver
type Pair struct {
UserID, ProjectID int
}
var factory ResolverFactory
factory = func(ctx context.Context, scope Scope) (PermissionResolver, error) {
// fetch the set of permissions of this user on this project
p, ok := scope.(Pair)
if !ok {
return nil, errors.New("invalid scope type")
}
perms, err := GetUserProjectPermissions(ctx, p.UserID, p.ProjectID)
if err != nil {
return nil, err
}
// the resolver is now scoped for the userID and projectID
resolver := NewResolverFromPermissions(perms)
return resolver, nil
}
resolver, err := factory(context.TODO(), Pair{4, 1})
if err != nil {
// ...
}
_ = resolver // this is the permission resolver for the user 4 on the project 1
}
func TestPool_GetResolver(t *testing.T) {
// keep track of number of time slow is called.
var called int32
// slow resolver, to test the lock behaviour.
slow := func(ctx context.Context, scope Scope) (PermissionResolver, error) {
atomic.AddInt32(&called, 1)
time.Sleep(time.Millisecond * 10)
return True, nil
}
// modulo resolver returns true r % scope == 0
modulo := func(ctx context.Context, scope Scope) (PermissionResolver, error) {
return func(r Right) bool {
return r.(int)%scope.(int) == 0
}, nil
}
fail := func(ctx context.Context, scope Scope) (PermissionResolver, error) {
return nil, errors.New("boom")
}
type Given struct {
Factory ResolverFactory
Scope Scope
Right Right
}
type Want struct {
Bool bool
Err error
}
type Case struct {
Sentence string
Given
Want
}
var cases = []Case{
{
Sentence: "the factory fails",
Given: Given{
Factory: fail,
Scope: nil,
Right: nil,
},
Want: Want{
Bool: false,
Err: errors.New("boom"),
},
},
{
Sentence: "the factory is modulo",
Given: Given{
Factory: modulo,
Scope: 3,
Right: 9,
},
Want: Want{
Bool: true,
Err: nil,
},
},
{
Sentence: "the factory is slow",
Given: Given{
Factory: slow,
Scope: "foo",
Right: nil,
},
Want: Want{
Bool: true,
Err: nil,
},
},
{
Sentence: "the factory is slow",
Given: Given{
Factory: slow,
Scope: "bar",
Right: nil,
},
Want: Want{
Bool: true,
Err: nil,
},
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("As a pool, when %s and the scope is %v, given %v", c.Sentence, c.Scope, c.Right), func(t *testing.T) {
p := NewPool(c.Given.Factory)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r, err := p.GetResolver(context.TODO(), c.Given.Scope)
if (c.Want.Err != nil) != (err != nil) {
t.Error("errors should match")
t.Error("want:", c.Want.Err)
t.Error("got: ", err)
return
}
if err != nil {
return
}
if r(c.Given.Right) != c.Want.Bool {
t.Errorf("permission should be %v", c.Want.Bool)
return
}
}()
}
wg.Wait()
})
}
if total := atomic.LoadInt32(&called); total != 2 {
t.Errorf("slow factory should be 2 time, total of %d calls", total)
}
}