Skip to content

Commit

Permalink
add counter reset test for long
Browse files Browse the repository at this point in the history
  • Loading branch information
gpop63 committed Jun 14, 2024
1 parent 5526cab commit 2edbb4d
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pkg/genlib/generator_with_text_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,64 @@ func Test_FieldIPWithTextTemplate(t *testing.T) {
}
}

func Test_FieldLongCounterResetAfterN5WithTextTemplate(t *testing.T) {
fld := Field{
Name: "counter_reset_test",
Type: FieldTypeLong,
}

afterN := 5

template := []byte(`{{$counter_reset_test := generate "counter_reset_test"}}{"counter_reset_test":"{{$counter_reset_test}}"}`)
configYaml := []byte(fmt.Sprintf(`fields:
- name: counter_reset_test
counter: true
counter_reset:
strategy: after_n
reset_after_n: %d`, afterN))
t.Logf("with template: %s", string(template))

cfg, err := config.LoadConfigFromYaml(configYaml)
if err != nil {
t.Fatal(err)
}

g := makeGeneratorWithTextTemplate(t, cfg, []Field{fld}, template, 10)

var buf bytes.Buffer

nSpins := int64(10)

var shouldReset bool

for i := int64(0); i < nSpins; i++ {
if err := g.Emit(&buf); err != nil {
t.Fatal(err)
}

m := unmarshalJSONT[string](t, buf.Bytes())
buf.Reset()

if len(m) != 1 {
t.Errorf("Expected map size 1, got %d", len(m))
}

v, ok := m[fld.Name]
if !ok {
t.Errorf("Missing key %v", fld.Name)
}

if i >= int64(afterN) && !shouldReset {
if v != "0" {
t.Errorf("Expected counter to reset to 0, got %v", v)
}
shouldReset = true
}

t.Logf("counter value: %v", v)
}
}

func Test_FieldFloatsWithTextTemplate(t *testing.T) {
_testNumericWithTextTemplate[float64](t, FieldTypeDouble)
_testNumericWithTextTemplate[float32](t, FieldTypeFloat)
Expand Down

0 comments on commit 2edbb4d

Please sign in to comment.