Skip to content

Commit

Permalink
Fix decision group on empty standard assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-abtasty committed Jan 11, 2024
1 parent d7130ad commit fc2ddaf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
8 changes: 6 additions & 2 deletions decision.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func getCache(
}(cacheChan)
}

assignmentsDG := &VisitorAssignments{}
var assignmentsDG *VisitorAssignments
for i := 0; i < nbRoutines; i++ {
r := <-cacheChan
switch r.visitorType {
Expand All @@ -91,7 +91,11 @@ func getCache(
}

// Assign decision group assignments to visitor
if allAssignments.Standard.getAssignments() != nil {
if assignmentsDG != nil {
if allAssignments.Standard == nil {
allAssignments.Standard = &VisitorAssignments{Assignments: map[string]*VisitorCache{}}
}
// Override standard assignments with decision group ones
for k, v := range assignmentsDG.getAssignments() {
allAssignments.Standard.Assignments[k] = v
}
Expand Down
37 changes: 30 additions & 7 deletions decision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package decision

import (
"fmt"
"maps"

Check failure on line 5 in decision_test.go

View workflow job for this annotation

GitHub Actions / Test & Coverage

package maps is not in GOROOT (/opt/hostedtoolcache/go/1.17.13/x64/src/maps)
"sync"
"testing"

Expand Down Expand Up @@ -111,32 +112,54 @@ func TestGetCache(t *testing.T) {
VariationID: "v_id",
Activated: true,
},
}
newAssignmentsDG := map[string]*VisitorCache{
"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)
// Check that decision group assignment filled out the empty standard assignment
assert.EqualValues(t, newAssignmentsDG, assignments.Standard.getAssignments())
assert.Nil(t, assignments.Anonymous)

cache[envID+visitorID] = &VisitorAssignments{
Assignments: newAssignments,
Assignments: maps.Clone(newAssignments),
}

assignments, err = getCache(envID, visitorID, anonymousID, decisionGroup, false, localGetCache)
assert.Nil(t, err)
assert.EqualValues(t, newAssignments, assignments.Standard.getAssignments())
// Check that the standard assignments got overriden by the decision group assignments
assert.EqualValues(t, newAssignmentsDG["vg_id"], assignments.Standard.getAssignments()["vg_id"])
assert.EqualValues(t, newAssignments["vg2_id"], assignments.Standard.getAssignments()["vg2_id"])
assert.EqualValues(t, newAssignmentsDG["vg3_id"], assignments.Standard.getAssignments()["vg3_id"])
assert.Nil(t, assignments.Anonymous)

cache[envID+anonymousID] = &VisitorAssignments{
Assignments: newAssignments,
Assignments: maps.Clone(newAssignments),
}
cache[envID+decisionGroup] = &VisitorAssignments{
Assignments: newAssignmentsDG,
Assignments: maps.Clone(newAssignmentsDG),
}

assignments, err = getCache(envID, visitorID, anonymousID, decisionGroup, true, localGetCache)
assert.Nil(t, err)
assert.Equal(t, 2, len(assignments.Standard.getAssignments()))
assert.Equal(t, 3, len(assignments.Standard.getAssignments()))
assert.EqualValues(t, newAssignments, assignments.Anonymous.getAssignments())
}

Expand Down
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module github.com/flagship-io/flagship-common

go 1.15
go 1.18

require (
github.com/flagship-io/flagship-proto v0.0.21
github.com/golang/protobuf v1.5.2
github.com/sirupsen/logrus v1.8.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.27.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/flagship-io/flagship-proto v0.0.15 h1:2sK9DWtnTkUEBiGtjLdwhMaaGsXuU+QRmAOhxlwj7bQ=
github.com/flagship-io/flagship-proto v0.0.15/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.19-0.20230412145243-82050c29248b h1:jVjCmSnMkDRSE3bo9EYssKfd5m5XGeBGO5SFZ4Wot9c=
github.com/flagship-io/flagship-proto v0.0.19-0.20230412145243-82050c29248b/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.20-0.20230413081732-238fddc879d3 h1:Guyp1wTfegwFy9VeJsDmTdP0J/MVthW2+yocWAu1+PY=
github.com/flagship-io/flagship-proto v0.0.20-0.20230413081732-238fddc879d3/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.20 h1:uR+wLlp8vc7qQbxAPbsPZ6lfznkCqHW6NycMbcdMHyM=
github.com/flagship-io/flagship-proto v0.0.20/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.21-0.20230822124416-5a308c3f96f9 h1:Acv8HaJJTPgcR32exUEXAtxLOlhv//HqhLMYrPegBqU=
github.com/flagship-io/flagship-proto v0.0.21-0.20230822124416-5a308c3f96f9/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.21-0.20230822124742-9802426d3b70 h1:gvRuGoeajA47173QY769V97YI5vKLtPEdRssn6lmMHI=
github.com/flagship-io/flagship-proto v0.0.21-0.20230822124742-9802426d3b70/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/flagship-io/flagship-proto v0.0.21 h1:zVFFRCHTOonXD1/xVrHvUShVF0UNuH5BXsWjaySJ5IA=
github.com/flagship-io/flagship-proto v0.0.21/go.mod h1:Nv5epf8wbbAEx6sAnjPumFy+YQOClXyXlxZzUMUqidw=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down

0 comments on commit fc2ddaf

Please sign in to comment.