Skip to content

Commit d5009bc

Browse files
committed
reset every N iteration
1 parent 2edbb4d commit d5009bc

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

pkg/genlib/generator_interface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,8 @@ func bindLongWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]a
10331033
}
10341034
case config.CounterResetStrategyAfterN:
10351035
// Reset after N
1036-
if !state.counterReset && state.counter >= *fieldCfg.CounterReset.ResetAfterN {
1036+
if state.counter%*fieldCfg.CounterReset.ResetAfterN == 0 {
10371037
dummyInt = 0
1038-
state.counterReset = true
10391038
}
10401039
}
10411040
}

pkg/genlib/generator_with_text_template_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,14 @@ func Test_FieldLongCounterResetAfterN5WithTextTemplate(t *testing.T) {
777777
t.Fatal(err)
778778
}
779779

780-
g := makeGeneratorWithTextTemplate(t, cfg, []Field{fld}, template, 10)
780+
g := makeGeneratorWithTextTemplate(t, cfg, []Field{fld}, template, 40)
781781

782782
var buf bytes.Buffer
783783

784-
nSpins := int64(10)
784+
nSpins := int64(40)
785785

786-
var shouldReset bool
786+
var resetCount int64
787+
expectedResetCount := nSpins / int64(afterN) // 8
787788

788789
for i := int64(0); i < nSpins; i++ {
789790
if err := g.Emit(&buf); err != nil {
@@ -802,15 +803,19 @@ func Test_FieldLongCounterResetAfterN5WithTextTemplate(t *testing.T) {
802803
t.Errorf("Missing key %v", fld.Name)
803804
}
804805

805-
if i >= int64(afterN) && !shouldReset {
806+
if i%int64(afterN) == 0 {
806807
if v != "0" {
807808
t.Errorf("Expected counter to reset to 0, got %v", v)
808809
}
809-
shouldReset = true
810+
resetCount++
810811
}
811812

812813
t.Logf("counter value: %v", v)
813814
}
815+
816+
if resetCount != expectedResetCount {
817+
t.Errorf("Expected counter to reset %d times, got %d", expectedResetCount, resetCount)
818+
}
814819
}
815820

816821
func Test_FieldFloatsWithTextTemplate(t *testing.T) {

0 commit comments

Comments
 (0)