Skip to content

Commit

Permalink
Add tests to ensure cache implementations are properly cancellable
Browse files Browse the repository at this point in the history
Signed-off-by: Raven Black <[email protected]>
  • Loading branch information
ravenblackx committed Sep 18, 2024
1 parent ad5d9ae commit 3e160e9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ absl::Status HttpCacheImplementationTest::insert(
return absl::OkStatus();
}

LookupContextPtr HttpCacheImplementationTest::lookupContextWithAllParts() {
absl::string_view path = "/common";
Http::TestResponseHeaderMapImpl response_headers{
{":status", "200"},
{"date", formatter_.fromTime(time_system_.systemTime())},
{"cache-control", "public,max-age=3600"}};
Http::TestResponseTrailerMapImpl response_trailers{
{"common-trailer", "irrelevant value"},
};
EXPECT_THAT(insert(lookup(path), response_headers, "commonbody", response_trailers), IsOk());
LookupRequest request = makeLookupRequest(path);
return cache()->makeLookupContext(std::move(request), decoder_callbacks_);
}

absl::Status HttpCacheImplementationTest::insert(absl::string_view request_path,
const Http::TestResponseHeaderMapImpl& headers,
const absl::string_view body) {
Expand Down Expand Up @@ -777,6 +791,48 @@ TEST_P(HttpCacheImplementationTest, EmptyTrailers) {
EXPECT_TRUE(expectLookupSuccessWithBodyAndTrailers(name_lookup_context.get(), body1));
}

TEST_P(HttpCacheImplementationTest, DoesNotRunHeadersCallbackWhenCancelledAfterPosted) {
bool was_called = false;
{
LookupContextPtr context = lookupContextWithAllParts();
context->getHeaders([&was_called](LookupResult&&, bool) { was_called = true; });
pumpIntoDispatcher();
context->onDestroy();
}
pumpDispatcher();
EXPECT_FALSE(was_called);
}

TEST_P(HttpCacheImplementationTest, DoesNotRunBodyCallbackWhenCancelledAfterPosted) {
bool was_called = false;
{
LookupContextPtr context = lookupContextWithAllParts();
context->getHeaders([](LookupResult&&, bool) {});
pumpDispatcher();
context->getBody({0, 10}, [&was_called](Buffer::InstancePtr&&, bool) { was_called = true; });
pumpIntoDispatcher();
context->onDestroy();
}
pumpDispatcher();
EXPECT_FALSE(was_called);
}

TEST_P(HttpCacheImplementationTest, DoesNotRunTrailersCallbackWhenCancelledAfterPosted) {
bool was_called = false;
{
LookupContextPtr context = lookupContextWithAllParts();
context->getHeaders([](LookupResult&&, bool) {});
pumpDispatcher();
context->getBody({0, 10}, [](Buffer::InstancePtr&&, bool) {});
pumpDispatcher();
context->getTrailers([&was_called](Http::ResponseTrailerMapPtr&&) { was_called = true; });
pumpIntoDispatcher();
context->onDestroy();
}
pumpDispatcher();
EXPECT_FALSE(was_called);
}

} // namespace Cache
} // namespace HttpFilters
} // namespace Extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class HttpCacheImplementationTest

std::shared_ptr<HttpCache> cache() const { return delegate_->cache(); }
bool validationEnabled() const { return delegate_->validationEnabled(); }
void pumpIntoDispatcher() { delegate_->beforePumpingDispatcher(); }
void pumpDispatcher() { delegate_->pumpDispatcher(); }
LookupContextPtr lookup(absl::string_view request_path);

Expand Down Expand Up @@ -91,6 +92,8 @@ class HttpCacheImplementationTest

LookupRequest makeLookupRequest(absl::string_view request_path);

LookupContextPtr lookupContextWithAllParts();

testing::AssertionResult
expectLookupSuccessWithHeaders(LookupContext* lookup_context,
const Http::TestResponseHeaderMapImpl& headers);
Expand Down

0 comments on commit 3e160e9

Please sign in to comment.