|
| 1 | +package eppoclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/mock" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_LruBanditLogger_cacheBanditAction(t *testing.T) { |
| 11 | + innerLogger := new(mockLogger) |
| 12 | + innerLogger.On("LogAssignment", mock.Anything).Return() |
| 13 | + innerLogger.On("LogBanditAction", mock.Anything).Return() |
| 14 | + |
| 15 | + logger, err := NewLruBanditLogger(innerLogger, 1000) |
| 16 | + assert.NoError(t, err) |
| 17 | + |
| 18 | + event := BanditEvent{ |
| 19 | + FlagKey: "flag", |
| 20 | + BanditKey: "bandit", |
| 21 | + Subject: "subject", |
| 22 | + Action: "action", |
| 23 | + ActionProbability: 0.1, |
| 24 | + OptimalityGap: 0.1, |
| 25 | + ModelVersion: "model-version", |
| 26 | + Timestamp: "timestamp", |
| 27 | + SubjectNumericAttributes: map[string]float64{}, |
| 28 | + SubjectCategoricalAttributes: map[string]string{}, |
| 29 | + ActionNumericAttributes: map[string]float64{}, |
| 30 | + ActionCategoricalAttributes: map[string]string{}, |
| 31 | + MetaData: map[string]string{}, |
| 32 | + } |
| 33 | + |
| 34 | + banditLogger := logger.(BanditActionLogger) |
| 35 | + banditLogger.LogBanditAction(event) |
| 36 | + banditLogger.LogBanditAction(event) |
| 37 | + |
| 38 | + innerLogger.AssertNumberOfCalls(t, "LogBanditAction", 1) |
| 39 | +} |
| 40 | + |
| 41 | +func Test_LruBanditLogger_flipFlopAction(t *testing.T) { |
| 42 | + innerLogger := new(mockLogger) |
| 43 | + innerLogger.On("LogAssignment", mock.Anything).Return() |
| 44 | + innerLogger.On("LogBanditAction", mock.Anything).Return() |
| 45 | + |
| 46 | + logger, err := NewLruBanditLogger(innerLogger, 1000) |
| 47 | + assert.NoError(t, err) |
| 48 | + banditLogger := logger.(BanditActionLogger) |
| 49 | + |
| 50 | + event := BanditEvent{ |
| 51 | + FlagKey: "flag", |
| 52 | + BanditKey: "bandit", |
| 53 | + Subject: "subject", |
| 54 | + Action: "action1", |
| 55 | + ActionProbability: 0.1, |
| 56 | + OptimalityGap: 0.1, |
| 57 | + ModelVersion: "model-version", |
| 58 | + Timestamp: "timestamp", |
| 59 | + SubjectNumericAttributes: map[string]float64{}, |
| 60 | + SubjectCategoricalAttributes: map[string]string{}, |
| 61 | + ActionNumericAttributes: map[string]float64{}, |
| 62 | + ActionCategoricalAttributes: map[string]string{}, |
| 63 | + MetaData: map[string]string{}, |
| 64 | + } |
| 65 | + |
| 66 | + event.Action = "action1" |
| 67 | + banditLogger.LogBanditAction(event) |
| 68 | + banditLogger.LogBanditAction(event) |
| 69 | + |
| 70 | + innerLogger.AssertNumberOfCalls(t, "LogBanditAction", 1) |
| 71 | + |
| 72 | + event.Action = "action2" |
| 73 | + banditLogger.LogBanditAction(event) |
| 74 | + |
| 75 | + innerLogger.AssertNumberOfCalls(t, "LogBanditAction", 2) |
| 76 | + |
| 77 | + event.Action = "action1" |
| 78 | + banditLogger.LogBanditAction(event) |
| 79 | + |
| 80 | + innerLogger.AssertNumberOfCalls(t, "LogBanditAction", 3) |
| 81 | +} |
| 82 | + |
| 83 | +func Test_LruBanditLogger_okIfInnerLoggerIsNotBandit(t *testing.T) { |
| 84 | + innerLogger := new(mockNonBanditLogger) |
| 85 | + innerLogger.On("LogAssignment", mock.Anything).Return() |
| 86 | + |
| 87 | + logger, err := NewLruBanditLogger(innerLogger, 1000) |
| 88 | + assert.NoError(t, err) |
| 89 | + |
| 90 | + event := BanditEvent{ |
| 91 | + FlagKey: "flag", |
| 92 | + BanditKey: "bandit", |
| 93 | + Subject: "subject", |
| 94 | + Action: "action", |
| 95 | + ActionProbability: 0.1, |
| 96 | + OptimalityGap: 0.1, |
| 97 | + ModelVersion: "model-version", |
| 98 | + Timestamp: "timestamp", |
| 99 | + SubjectNumericAttributes: map[string]float64{}, |
| 100 | + SubjectCategoricalAttributes: map[string]string{}, |
| 101 | + ActionNumericAttributes: map[string]float64{}, |
| 102 | + ActionCategoricalAttributes: map[string]string{}, |
| 103 | + MetaData: map[string]string{}, |
| 104 | + } |
| 105 | + |
| 106 | + banditLogger := logger.(BanditActionLogger) |
| 107 | + banditLogger.LogBanditAction(event) |
| 108 | + banditLogger.LogBanditAction(event) |
| 109 | + |
| 110 | + innerLogger.AssertNumberOfCalls(t, "LogBanditAction", 0) |
| 111 | +} |
0 commit comments