-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecision_test.go
310 lines (260 loc) · 8.74 KB
/
decision_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package decision
import (
"fmt"
"maps"
"sync"
"testing"
"github.com/flagship-io/flagship-common/targeting"
"github.com/flagship-io/flagship-proto/decision_response"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)
var mu = sync.Mutex{}
var campaigns = []*Campaign{
{
ID: "a1",
BucketRanges: [][]float64{{0., 100.}},
VariationGroups: []*VariationGroup{
{
ID: "vga",
Targetings: createBoolTargeting(),
Variations: []*Variation{
{
ID: "vgav1",
Allocation: 50,
Modifications: &decision_response.Modifications{
Type: decision_response.ModificationsType_FLAG,
Value: structpb.NewStringValue("toto1").GetStructValue(),
},
}, {
ID: "vgav2",
Allocation: 50,
Modifications: &decision_response.Modifications{
Type: decision_response.ModificationsType_FLAG,
Value: structpb.NewStringValue("toto2").GetStructValue(),
},
},
},
},
},
},
{
ID: "a2",
BucketRanges: [][]float64{{20., 30.}},
VariationGroups: []*VariationGroup{
{
ID: "vgb",
Targetings: createBoolTargeting(),
Variations: []*Variation{
{
ID: "vgbv1",
Allocation: 100,
Modifications: &decision_response.Modifications{
Type: decision_response.ModificationsType_FLAG,
Value: structpb.NewStringValue("tata").GetStructValue(),
},
},
},
},
},
},
}
func mockGetCache(environmentID string, id string) (*VisitorAssignments, error) {
vi := VisitorAssignments{}
return &vi, nil
}
func mockSaveCache(environmentID string, id string, assignment *VisitorAssignments) error {
fmt.Println("Save cache environment", environmentID, "id", id, "assignments", assignment)
return nil
}
func mockActivateCampaigns(activations []*VisitorActivation) error {
fmt.Println("Activate campaigns", activations)
return nil
}
var cache = map[string]*VisitorAssignments{}
func localGetCache(environmentID string, id string) (*VisitorAssignments, error) {
mu.Lock()
defer func() {
mu.Unlock()
}()
return cache[environmentID+id], nil
}
func localSetCache(environmentID string, id string, assignment *VisitorAssignments) error {
mu.Lock()
cache[environmentID+id] = assignment
mu.Unlock()
return nil
}
func TestGetCache(t *testing.T) {
envID := "env_id"
visitorID := "visitor_id"
anonymousID := "anonymous_id"
decisionGroup := "decisionGroup"
assignments, err := getCache(envID, visitorID, anonymousID, decisionGroup, false, localGetCache)
assert.Nil(t, err)
assert.Nil(t, assignments.Standard)
assert.Nil(t, assignments.Anonymous)
newAssignments := map[string]*VisitorCache{
"vg_id": {
VariationID: "v_id",
Activated: true,
},
"vg2_id": {
VariationID: "v2_id",
Activated: true,
},
}
newAssignmentsDG := map[string]*VisitorCache{
"vg_id": {
VariationID: "vdg_id",
Activated: true,
},
"vg3_id": {
VariationID: "v3_id",
Activated: true,
},
}
cache[envID+decisionGroup] = &VisitorAssignments{
Assignments: newAssignmentsDG,
}
assignments, err = getCache(envID, visitorID, anonymousID, decisionGroup, false, localGetCache)
assert.Nil(t, err)
assert.EqualValues(t, newAssignmentsDG, assignments.DecisionGroup.getAssignments())
assert.Nil(t, assignments.Standard)
assert.Nil(t, assignments.Anonymous)
cache[envID+visitorID] = &VisitorAssignments{
Assignments: maps.Clone(newAssignments),
}
assignments, err = getCache(envID, visitorID, anonymousID, decisionGroup, false, localGetCache)
assert.Nil(t, err)
assert.EqualValues(t, newAssignmentsDG, assignments.DecisionGroup.getAssignments())
assert.EqualValues(t, newAssignments, assignments.Standard.getAssignments())
assert.Nil(t, assignments.Anonymous)
cache[envID+anonymousID] = &VisitorAssignments{
Assignments: maps.Clone(newAssignments),
}
assignments, err = getCache(envID, visitorID, anonymousID, decisionGroup, true, localGetCache)
assert.Nil(t, err)
assert.EqualValues(t, newAssignmentsDG, assignments.DecisionGroup.getAssignments())
assert.EqualValues(t, newAssignments, assignments.Standard.getAssignments())
assert.EqualValues(t, newAssignments, assignments.Anonymous.getAssignments())
}
func TestDecisionCache(t *testing.T) {
vi := Visitor{}
vi.ID = "v1"
vi.Context = &targeting.Context{
Standard: targeting.ContextMap{
"isVIP": structpb.NewBoolValue(true),
},
}
ei := Environment{}
ei.ID = "e123"
ei.Campaigns = campaigns
for _, vg := range ei.Campaigns[0].VariationGroups {
vg.Campaign = ei.Campaigns[0]
}
options := DecisionOptions{
TriggerHit: true,
}
// no options
handlers := DecisionHandlers{}
handlers.GetCache = mockGetCache
handlers.SaveCache = mockSaveCache
handlers.ActivateCampaigns = mockActivateCampaigns
decision, err := GetDecision(vi, ei, options, handlers)
// check that campaign matching visitor is returned. Also check that the second variation is set
assert.Nil(t, err)
assert.Len(t, decision.Campaigns, 1)
assert.Equal(t, "vgav2", decision.Campaigns[0].Variation.Id.Value)
vi.DecisionGroup = "dg"
decision, err = GetDecision(vi, ei, options, handlers)
// check that campaign matching visitor is returned. Also check that the second variation is set
assert.Nil(t, err)
assert.Len(t, decision.Campaigns, 1)
assert.Equal(t, "vgav1", decision.Campaigns[0].Variation.Id.Value)
// change the allocation so that visitor should change variation if the cache is disabled
ei.Campaigns[0].VariationGroups[0].Variations[0].Allocation = 10
ei.Campaigns[0].VariationGroups[0].Variations[1].Allocation = 90
decision, err = GetDecision(vi, ei, options, handlers)
// check that campaign matching visitor is returned. Also check that the first variation is not chosen
assert.Nil(t, err)
assert.Len(t, decision.Campaigns, 1)
assert.Equal(t, "vgav2", decision.Campaigns[0].Variation.Id.Value)
// Reset the allocations
ei.Campaigns[0].VariationGroups[0].Variations[0].Allocation = 50
ei.Campaigns[0].VariationGroups[0].Variations[1].Allocation = 50
// Set "real" local cache to persist visitor allocation
handlers.GetCache = localGetCache
handlers.SaveCache = localSetCache
ei.CacheEnabled = true
decision, _ = GetDecision(vi, ei, options, handlers)
// check that campaign matching visitor is returned. Also check that the first variation is not chosen
assert.Equal(t, decision.Campaigns[0].Variation.Id.Value, "vgav1")
// change the allocation so that visitor should change variation if the cache is disabled
ei.Campaigns[0].VariationGroups[0].Variations[0].Allocation = 10
ei.Campaigns[0].VariationGroups[0].Variations[1].Allocation = 90
decision, _ = GetDecision(vi, ei, options, handlers)
// check that campaign matching visitor is returned. Also check that the first variation is not chosen
assert.Equal(t, decision.Campaigns[0].Variation.Id.Value, "vgav1")
// Reset the allocations
ei.Campaigns[0].VariationGroups[0].Variations[0].Allocation = 50
ei.Campaigns[0].VariationGroups[0].Variations[1].Allocation = 50
}
func TestDecisionBucketInNoCache(t *testing.T) {
vi := Visitor{}
vi.ID = "v1"
vi.Context = &targeting.Context{
Standard: targeting.ContextMap{
"isVIP": structpb.NewBoolValue(true),
},
}
vi.DecisionGroup = "decision"
ei := Environment{}
ei.ID = "e123"
ei.Campaigns = campaigns
for _, vg := range ei.Campaigns[0].VariationGroups {
vg.Campaign = ei.Campaigns[0]
}
for _, vg := range ei.Campaigns[1].VariationGroups {
vg.Campaign = ei.Campaigns[1]
}
options := DecisionOptions{}
// no options
handlers := DecisionHandlers{}
handlers.GetCache = mockGetCache
handlers.SaveCache = mockSaveCache
handlers.ActivateCampaigns = mockActivateCampaigns
decision, err := GetDecision(vi, ei, options, handlers)
// only one campaign should be returned, they have the same targeting and allocation but the second one has not the right bucket allocation
assert.Nil(t, err)
assert.Len(t, decision.Campaigns, 1)
}
func TestVisitorShouldNotBeAssignedWhenVariationDeleted(t *testing.T) {
vi := Visitor{}
vi.ID = "v1"
vi.Context = &targeting.Context{
Standard: targeting.ContextMap{
"isVIP": structpb.NewBoolValue(true),
},
}
ei := Environment{}
ei.ID = "e123"
ei.CacheEnabled = true
ei.Campaigns = campaigns
for _, vg := range ei.Campaigns[0].VariationGroups {
vg.Campaign = ei.Campaigns[0]
}
options := DecisionOptions{
TriggerHit: true,
}
// no options
handlers := DecisionHandlers{}
handlers.GetCache = mockGetCache
handlers.SaveCache = mockSaveCache
handlers.ActivateCampaigns = mockActivateCampaigns
// delete variation and check that visitor is not returned
campaignVars := ei.Campaigns[0].VariationGroups[0].Variations
ei.Campaigns[0].VariationGroups[0].Variations = campaignVars[1:]
decision, _ := GetDecision(vi, ei, options, handlers)
assert.Len(t, decision.Campaigns, 0)
}