Skip to content

Commit e2740bc

Browse files
committed
Fix flaky ETag validation in event log acceptance tests
The test could fail if new events were written to the log between the initial request and the conditional poll, resulting in a 200 OK instead of a 304 Not Modified. The validation now accounts for this by checking if the ETag has changed when a 200 OK is received.
1 parent 25d977d commit e2740bc

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎src/ServiceControl.AcceptanceTests/EventLogs/When_the_event_log_is_polled_with_an_etag.cs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,25 @@ await Define<ScenarioContext>()
4545
return false;
4646
}
4747

48-
// Raw header: an unquoted value fails EntityTagHeaderValue parsing, and this test
49-
// has to observe that rather than throw on it.
50-
if (!first.Headers.TryGetValues("ETag", out var values))
48+
var currentEtag = ReadEtag(first);
49+
50+
if (string.IsNullOrEmpty(currentEtag))
5151
{
5252
return false;
5353
}
5454

55-
etag = values.FirstOrDefault();
55+
var current = await Poll(currentEtag);
5656

57-
if (string.IsNullOrEmpty(etag))
57+
// The endpoint keeps writing startup events, so a 200 here means either the
58+
// validator went stale between the two requests, or the server ignored
59+
// If-None-Match. Only the second is a failure, and the ETag tells them apart.
60+
if (current.StatusCode == HttpStatusCode.OK && ReadEtag(current) != currentEtag)
5861
{
5962
return false;
6063
}
6164

62-
currentEtagStatus = (await Poll(etag)).StatusCode;
65+
etag = currentEtag;
66+
currentEtagStatus = current.StatusCode;
6367

6468
var unknown = await Poll("\"not-an-etag-this-instance-ever-issued\"");
6569
unknownEtagStatus = unknown.StatusCode;
@@ -87,6 +91,11 @@ await Define<ScenarioContext>()
8791
}
8892
}
8993

94+
// Raw header: an unquoted value fails EntityTagHeaderValue parsing, and this test
95+
// has to observe that rather than throw on it.
96+
static string ReadEtag(HttpResponseMessage response) =>
97+
response.Headers.TryGetValues("ETag", out var values) ? values.FirstOrDefault() : null;
98+
9099
Task<HttpResponseMessage> Poll(string ifNoneMatch)
91100
{
92101
var request = new HttpRequestMessage(HttpMethod.Get, "/api/eventlogitems/");

0 commit comments

Comments
 (0)