Skip to content

Commit 5526cab

Browse files
committed
add counter reset for long
1 parent 34239fe commit 5526cab

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

pkg/genlib/generator_interface.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ type genState struct {
8282
// previous cardinality value cache; necessary for cardinality
8383
prevCacheCardinality map[string][]any
8484
// internal buffer pool to decrease load on GC
85-
pool sync.Pool
85+
pool sync.Pool
86+
counterReset bool
8687
}
8788

8889
func newGenState() *genState {
@@ -99,7 +100,6 @@ func newGenState() *genState {
99100
}
100101

101102
func bindField(cfg Config, field Field, fieldMap map[string]any, withReturn bool) error {
102-
103103
// Check for hardcoded field value
104104
if len(field.Value) > 0 {
105105
if withReturn {
@@ -194,7 +194,6 @@ func bindByType(cfg Config, field Field, fieldMap map[string]any) (err error) {
194194
}
195195

196196
func bindByTypeWithReturn(cfg Config, field Field, fieldMap map[string]any) (err error) {
197-
198197
fieldCfg, _ := cfg.GetField(field.Name)
199198

200199
switch field.Type {
@@ -971,6 +970,18 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
971970
return err
972971
}
973972

973+
if err := fieldCfg.ValidateCounterResetStrategy(); err != nil {
974+
return err
975+
}
976+
977+
if err := fieldCfg.ValidateCounterResetAfterN(); err != nil {
978+
return err
979+
}
980+
981+
if err := fieldCfg.ValidateCounterResetProbabilistic(); err != nil {
982+
return err
983+
}
984+
974985
if len(fieldCfg.Enum) > 0 {
975986
var emitF emitF
976987
idx := customRand.Intn(len(fieldCfg.Enum))
@@ -1008,6 +1019,27 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
10081019
dummyInt = fuzzyIntCounter(previous, fieldCfg.Fuzziness)
10091020
}
10101021

1022+
if fieldCfg.CounterReset != nil {
1023+
switch fieldCfg.CounterReset.Strategy {
1024+
case config.CounterResetStrategyRandom:
1025+
// 50% chance to reset
1026+
if customRand.Intn(2) == 0 {
1027+
dummyInt = 0
1028+
}
1029+
case config.CounterResetStrategyProbabilistic:
1030+
// Probability% chance to reset
1031+
if customRand.Intn(100) < int(*fieldCfg.CounterReset.Probability) {
1032+
dummyInt = 0
1033+
}
1034+
case config.CounterResetStrategyAfterN:
1035+
// Reset after N
1036+
if !state.counterReset && state.counter >= *fieldCfg.CounterReset.ResetAfterN {
1037+
dummyInt = 0
1038+
state.counterReset = true
1039+
}
1040+
}
1041+
}
1042+
10111043
state.prevCache[field.Name] = dummyInt
10121044
return dummyInt
10131045
}

0 commit comments

Comments
 (0)