Skip to content

Commit

Permalink
Merge pull request #118 from checkr/zz/make-entity-id-nil-error
Browse files Browse the repository at this point in the history
Use random entity id if it's nil
  • Loading branch information
zhouzhuojie authored May 1, 2018
2 parents 1ddb473 + 269bbca commit c4bb880
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 84 deletions.
7 changes: 3 additions & 4 deletions docs/api_docs/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,14 @@ definitions:
evalContext:
type: object
required:
- entityID
- entityType
- flagID
properties:
entityID:
type: string
minLength: 1
description: >-
entityID is used to deterministically at random to evaluate the flag
result. If it's empty, flagr will randomly generate one.
entityType:
type: string
minLength: 1
Expand Down Expand Up @@ -1045,12 +1046,10 @@ definitions:
evaluationEntity:
type: object
required:
- entityID
- entityType
properties:
entityID:
type: string
minLength: 1
entityType:
type: string
minLength: 1
Expand Down
6 changes: 3 additions & 3 deletions pkg/entity/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type DistributionDebugLog struct {
}

// Rollout rolls out the entity based on the rolloutPercent
func (d DistributionArray) Rollout(entityID *string, salt string, rolloutPercent uint) (variantID *uint, msg string) {
if entityID == nil || *entityID == "" {
func (d DistributionArray) Rollout(entityID string, salt string, rolloutPercent uint) (variantID *uint, msg string) {
if entityID == "" {
return nil, "rollout no. empty entityID"
}

Expand All @@ -59,7 +59,7 @@ func (d DistributionArray) Rollout(entityID *string, salt string, rolloutPercent
return nil, "rollout no. there's no distribution set"
}

num := crc32Num(*entityID, salt)
num := crc32Num(entityID, salt)
vID, index := d.bucketByNum(num)
log := fmt.Sprintf("%+v", DistributionDebugLog{
BucketNum: num,
Expand Down
11 changes: 5 additions & 6 deletions pkg/entity/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package entity
import (
"testing"

"github.com/checkr/flagr/pkg/util"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -148,19 +147,19 @@ func TestRolloutWithEntity(t *testing.T) {
var vID *uint
var msg string

vID, msg = d.Rollout(nil, "salt", uint(0))
vID, msg = d.Rollout("", "salt", uint(0))
assert.Nil(t, vID)
assert.Contains(t, msg, "no")

vID, msg = d.Rollout(util.StringPtr("entity123"), "salt", uint(0))
vID, msg = d.Rollout("entity123", "salt", uint(0))
assert.Nil(t, vID)
assert.Contains(t, msg, "no")

vID, msg = d.Rollout(util.StringPtr("entity123"), "salt", uint(100))
vID, msg = d.Rollout("entity123", "salt", uint(100))
assert.NotNil(t, vID)
assert.Contains(t, msg, "yes")

vID, msg = d.Rollout(util.StringPtr("entity123"), "salt", uint(1))
vID, msg = d.Rollout("entity123", "salt", uint(1))
assert.Nil(t, vID)
assert.Contains(t, msg, "no")
})
Expand All @@ -173,7 +172,7 @@ func TestRolloutWithEntity(t *testing.T) {
var vID *uint
var msg string

vID, msg = d.Rollout(util.StringPtr("entity123"), "salt", uint(100))
vID, msg = d.Rollout("entity123", "salt", uint(100))
assert.Nil(t, vID)
assert.Contains(t, msg, "no")
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/data_recorder_kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestKafkaEvalResult(t *testing.T) {
r := &kafkaEvalResult{
EvalResult: &models.EvalResult{
EvalContext: &models.EvalContext{
EntityID: util.StringPtr("123"),
EntityID: "123",
},
FlagID: util.Int64Ptr(int64(1)),
FlagSnapshotID: 1,
Expand Down
5 changes: 5 additions & 0 deletions pkg/handler/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"encoding/json"
"fmt"
"math/rand"
"time"

"github.com/checkr/flagr/pkg/config"
Expand Down Expand Up @@ -106,6 +107,10 @@ var evalFlag = func(evalContext models.EvalContext) *models.EvalResult {
return BlankResult(f, evalContext, fmt.Sprintf("flagID %v has no segments", flagID))
}

if evalContext.EntityID == "" {
evalContext.EntityID = fmt.Sprintf("randomly_generated_%d", rand.Int31())
}

logs := []*models.SegmentDebugLog{}
var vID *int64
var sID *int64
Expand Down
23 changes: 12 additions & 11 deletions pkg/handler/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestEvalSegment(t *testing.T) {
vID, log := evalSegment(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
}, s)
Expand All @@ -43,7 +43,7 @@ func TestEvalSegment(t *testing.T) {
vID, log := evalSegment(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
}, s)
Expand All @@ -58,7 +58,7 @@ func TestEvalSegment(t *testing.T) {
vID, log := evalSegment(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "NY"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
}, s)
Expand All @@ -73,7 +73,7 @@ func TestEvalSegment(t *testing.T) {
vID, log := evalSegment(models.EvalContext{
EnableDebug: true,
EntityContext: nil,
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
}, s)
Expand All @@ -88,16 +88,17 @@ func TestEvalFlag(t *testing.T) {

t.Run("test empty evalContext", func(t *testing.T) {
defer gostub.StubFunc(&GetEvalCache, GenFixtureEvalCache()).Reset()
result := evalFlag(models.EvalContext{})
result := evalFlag(models.EvalContext{FlagID: util.Int64Ptr(int64(100))})
assert.Nil(t, result.VariantID)
assert.NotEmpty(t, result.EvalContext.EntityID)
})

t.Run("test happy code path", func(t *testing.T) {
defer gostub.StubFunc(&GetEvalCache, GenFixtureEvalCache()).Reset()
result := evalFlag(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
})
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestEvalFlag(t *testing.T) {
result := evalFlag(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA", "state": "CA", "rate": 2000},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
})
Expand Down Expand Up @@ -168,7 +169,7 @@ func TestEvalFlag(t *testing.T) {
result := evalFlag(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA", "state": "NY"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
})
Expand All @@ -184,7 +185,7 @@ func TestEvalFlag(t *testing.T) {
result := evalFlag(models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
})
Expand All @@ -208,7 +209,7 @@ func TestPostEvaluation(t *testing.T) {
Body: &models.EvalContext{
EnableDebug: true,
EntityContext: map[string]interface{}{"dl_state": "CA", "state": "NY"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
FlagID: util.Int64Ptr(int64(100)),
},
Expand All @@ -227,7 +228,7 @@ func TestPostEvaluationBatch(t *testing.T) {
Entities: []*models.EvaluationEntity{
{
EntityContext: map[string]interface{}{"dl_state": "CA", "state": "NY"},
EntityID: util.StringPtr("entityID1"),
EntityID: "entityID1",
EntityType: util.StringPtr("entityType1"),
},
},
Expand Down
5 changes: 1 addition & 4 deletions swagger/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,12 @@ definitions:
evalContext:
type: object
required:
- entityID
- entityType
- flagID
properties:
entityID:
type: string
minLength: 1
description: entityID is used to deterministically at random to evaluate the flag result. If it's empty, flagr will randomly generate one.
entityType:
type: string
minLength: 1
Expand Down Expand Up @@ -427,12 +426,10 @@ definitions:
evaluationEntity:
type: object
required:
- entityID
- entityType
properties:
entityID:
type: string
minLength: 1
entityType:
type: string
minLength: 1
Expand Down
24 changes: 2 additions & 22 deletions swagger_gen/models/eval_context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 1 addition & 21 deletions swagger_gen/models/evaluation_entity.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c4bb880

Please sign in to comment.