From 49f77718aa94c741108a34378cde7e3222dfe63f Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Mon, 7 Oct 2024 08:13:53 -0300 Subject: [PATCH] fix flaky assertion in TestExporterMetrics This assertion was done in a json string. Sometimes the order of fields in the json string changes which causes failures in the test. --- exporter/elasticsearchexporter/exporter_test.go | 1 + exporter/elasticsearchexporter/utils_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/exporter/elasticsearchexporter/exporter_test.go b/exporter/elasticsearchexporter/exporter_test.go index 77ff8e2d0b47..297ed5710dfd 100644 --- a/exporter/elasticsearchexporter/exporter_test.go +++ b/exporter/elasticsearchexporter/exporter_test.go @@ -532,6 +532,7 @@ func TestExporterMetrics(t *testing.T) { rec.Record(docs) return itemsAllOK(docs) }) + defer server.Close() exporter := newTestMetricsExporter(t, server.URL, func(cfg *Config) { cfg.Mapping.Mode = "ecs" diff --git a/exporter/elasticsearchexporter/utils_test.go b/exporter/elasticsearchexporter/utils_test.go index 82fcc0e22cde..05e9d8576a57 100644 --- a/exporter/elasticsearchexporter/utils_test.go +++ b/exporter/elasticsearchexporter/utils_test.go @@ -48,7 +48,13 @@ func assertItemsEqual(t *testing.T, expected, actual []itemRequest, assertOrder copy(actualItems, actual) slices.SortFunc(actualItems, itemRequestsSortFunc) } - assert.Equal(t, expectedItems, actualItems) + + assert.Equal(t, len(expectedItems), len(actualItems), "want %d items, got %d", len(expectedItems), len(actualItems)) + for i, want := range expectedItems { + got := actualItems[i] + assert.JSONEq(t, string(want.Action), string(got.Action), "item %d action", i) + assert.JSONEq(t, string(want.Document), string(got.Document), "item %d document", i) + } } type itemResponse struct {