Skip to content

Commit aa7e3c8

Browse files
systemtest: fix flaky TestRUMRoutingIntegration (#21196) (#21213)
* add min docs to estest.ExpectSourcemapError * fix flaky test We were not waiting long enough, thus the test were sometimes looking at too few documents. * fix fmt (cherry picked from commit c0bd5c1) Co-authored-by: Edoardo Tenani <526307+endorama@users.noreply.github.com>
1 parent b6e375e commit aa7e3c8

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

systemtest/estest/search.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ func ExpectMinDocs(t testing.TB, es *espoll.Client, min int, index string, query
7777
return result
7878
}
7979

80-
func ExpectSourcemapError(t testing.TB, es *espoll.Client, index string, retry func(), query interface{}, updated bool) espoll.SearchResult {
80+
// ExpectSourcemapError retries sending events via retry until the sourcemap
81+
// fetcher is available (i.e. no "fetcher unavailable" errors appear in the
82+
// indexed documents), then returns the search result.
83+
//
84+
// minDocs is the minimum number of documents expected in the index after a
85+
// successful retry. Callers must pass the exact count they expect so that the
86+
// underlying poll does not return early when Elasticsearch has only indexed a
87+
// partial batch (see: TotalHitsCondition race).
88+
func ExpectSourcemapError(t testing.TB, es *espoll.Client, index string, minDocs int, retry func(), query interface{}, updated bool) espoll.SearchResult {
8189
t.Helper()
8290

8391
deadline := time.After(5 * time.Second)
@@ -99,7 +107,7 @@ func ExpectSourcemapError(t testing.TB, es *espoll.Client, index string, retry f
99107

100108
retry()
101109

102-
result := ExpectDocs(t, es, index, query)
110+
result := ExpectMinDocs(t, es, minDocs, index, query)
103111

104112
if isFetcherAvailable(t, result) {
105113
assertSourcemapUpdated(t, result, updated)

systemtest/rum_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ func TestRUMRoutingIntegration(t *testing.T) {
220220
require.NoError(t, err)
221221
defer resp.Body.Close()
222222
}
223-
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm.rum*", retry, nil, false)
223+
// 9 = 1 transaction + 8 spans produced by testdata/intake-v3/rum_events.ndjson.
224+
// Passing the exact count prevents ExpectSourcemapError from returning early
225+
// when Elasticsearch has only indexed a partial batch (TotalHitsCondition race).
226+
const wantDocs = 9
227+
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm.rum*", wantDocs, retry, nil, false)
224228
approvaltest.ApproveFields(
225229
t, t.Name(), result.Hits.Hits, "@timestamp", "timestamp.us",
226230
"source.port", "source.ip", "client.ip",

systemtest/sourcemap_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestRUMErrorSourcemapping(t *testing.T) {
4343
retry := func() {
4444
systemtest.SendRUMEventsPayload(t, serverURL, "../testdata/intake-v2/errors_rum.ndjson")
4545
}
46-
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", retry, nil, true)
46+
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", 1, retry, nil, true)
4747
approvaltest.ApproveFields(
4848
t, t.Name(), result.Hits.Hits,
4949
// RUM timestamps are set by the server based on the time the payload is received.
@@ -91,7 +91,7 @@ func TestRUMSpanSourcemapping(t *testing.T) {
9191
retry := func() {
9292
systemtest.SendRUMEventsPayload(t, srv.URL, "../testdata/intake-v2/transactions_spans_rum_2.ndjson")
9393
}
94-
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm*", retry, espoll.TermQuery{
94+
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm*", 1, retry, espoll.TermQuery{
9595
Field: "processor.event",
9696
Value: "span",
9797
}, true)
@@ -124,7 +124,7 @@ func TestNoMatchingSourcemap(t *testing.T) {
124124
retry := func() {
125125
systemtest.SendRUMEventsPayload(t, srv.URL, "../testdata/intake-v2/transactions_spans_rum_2.ndjson")
126126
}
127-
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm*", retry, espoll.TermQuery{
127+
result := estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "traces-apm*", 1, retry, espoll.TermQuery{
128128
Field: "processor.event",
129129
Value: "span",
130130
}, false)
@@ -157,11 +157,11 @@ func TestSourcemapCaching(t *testing.T) {
157157
retry := func() {
158158
systemtest.SendRUMEventsPayload(t, srv.URL, "../testdata/intake-v2/errors_rum.ndjson")
159159
}
160-
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", retry, nil, true)
160+
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", 1, retry, nil, true)
161161

162162
// Delete the source map and error, and try again.
163163
systemtest.CleanupElasticsearch(t)
164-
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", retry, nil, true)
164+
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", 1, retry, nil, true)
165165
}
166166

167167
func TestSourcemapFetcher(t *testing.T) {
@@ -216,7 +216,7 @@ func TestSourcemapFetcher(t *testing.T) {
216216
retry := func() {
217217
systemtest.SendRUMEventsPayload(t, srv.URL, "../testdata/intake-v2/errors_rum.ndjson")
218218
}
219-
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", retry, nil, true)
219+
estest.ExpectSourcemapError(t, systemtest.Elasticsearch, "logs-apm.error-*", 1, retry, nil, true)
220220
})
221221
}
222222
}

0 commit comments

Comments
 (0)