diff --git a/plugins/processors/noise/noise_test.go b/plugins/processors/noise/noise_test.go index 12b3b8273..e40bc0e2f 100644 --- a/plugins/processors/noise/noise_test.go +++ b/plugins/processors/noise/noise_test.go @@ -2,10 +2,12 @@ package noise import ( "math" + "sync" "testing" "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" "gonum.org/v1/gonum/stat/distuv" @@ -376,3 +378,89 @@ func TestInvalidDistributionFunction(t *testing.T) { err := p.Init() require.EqualError(t, err, "unknown distribution type \"invalid\"") } + +func TestTracking(t *testing.T) { + // Setup raw input and expected output + inputRaw := []telegraf.Metric{ + metric.New( + "zero_uint64", + map[string]string{}, + map[string]interface{}{"value": uint64(0)}, + time.Unix(0, 0), + ), + metric.New( + "zero_int64", + map[string]string{}, + map[string]interface{}{"value": int64(0)}, + time.Unix(0, 0), + ), + metric.New( + "zero_float", + map[string]string{}, + map[string]interface{}{"value": float64(0.0)}, + time.Unix(0, 0), + ), + } + + expected := []telegraf.Metric{ + metric.New( + "zero_uint64", + map[string]string{}, + map[string]interface{}{"value": uint64(13)}, + time.Unix(0, 0), + ), + metric.New( + "zero_int64", + map[string]string{}, + map[string]interface{}{"value": int64(13)}, + time.Unix(0, 0), + ), + metric.New( + "zero_float", + map[string]string{}, + map[string]interface{}{"value": float64(13.37)}, + time.Unix(0, 0), + ), + } + + // Create fake notification for testing + var mu sync.Mutex + delivered := make([]telegraf.DeliveryInfo, 0, len(inputRaw)) + notify := func(di telegraf.DeliveryInfo) { + mu.Lock() + defer mu.Unlock() + delivered = append(delivered, di) + } + + // Convert raw input to tracking metric + input := make([]telegraf.Metric, 0, len(inputRaw)) + for _, m := range inputRaw { + tm, _ := metric.WithTracking(m, notify) + input = append(input, tm) + } + + // Prepare and start the plugin + plugin := &Noise{ + NoiseType: "laplacian", + Scale: 1.0, + Log: testutil.Logger{}, + } + require.NoError(t, plugin.Init()) + plugin.generator = &testDistribution{value: 13.37} + + // Process expected metrics and compare with resulting metrics + actual := plugin.Apply(input...) + testutil.RequireMetricsEqual(t, expected, actual) + + // Simulate output acknowledging delivery + for _, m := range actual { + m.Accept() + } + + // Check delivery + require.Eventuallyf(t, func() bool { + mu.Lock() + defer mu.Unlock() + return len(input) == len(delivered) + }, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(expected)) +}