Skip to content

Commit 755d681

Browse files
committed
[TEST] One set of fake HTTP classes after main added its own
Main's #4071 and #4501 put FakeResponse, FakeRequest, FakeSession and FakeHttpClient in an unnamed namespace at the top of this file, and this branch already had four of those names in a second unnamed namespace lower down. Reopening an unnamed namespace names the same namespace, so the rebase merged both with no conflict at all and left four redefinitions. One set now. The session and the client take a script and default to answering the way main's did, so main's own call site needs no edit, and DeferredSession and DeferredHttpClient are untouched. The default body stays as main wrote it. This branch does not change how the exporter decides success, so what main's cases send still passes here. Verified in both configurations with maintainer mode on: 19 cases, all passing without async export, 3 passing and 16 skipping with it. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 3f68332 commit 755d681

1 file changed

Lines changed: 33 additions & 89 deletions

File tree

‎exporters/elasticsearch/test/es_log_record_exporter_test.cc‎

Lines changed: 33 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ namespace http_client = opentelemetry::ext::http::client;
4040
// A response shaped like a successful Elasticsearch bulk reply: the exporter looks for
4141
// `"failed" : 0` in the body (see ElasticsearchLogRecordExporter::Export) in addition to the
4242
// status code before reporting success.
43+
constexpr const char *kDefaultAcceptedBody = R"({"errors": false, "failed" : 0})";
44+
4345
class FakeResponse final : public http_client::Response
4446
{
4547
public:
46-
FakeResponse()
47-
{
48-
static const std::string kSuccessBody = R"({"errors": false, "failed" : 0})";
49-
body_.assign(kSuccessBody.begin(), kSuccessBody.end());
50-
}
48+
explicit FakeResponse(http_client::StatusCode status = 200,
49+
const std::string &body = kDefaultAcceptedBody)
50+
: status_(status), body_(body.begin(), body.end())
51+
{}
5152

5253
const http_client::Body &GetBody() const noexcept override { return body_; }
5354

@@ -66,9 +67,10 @@ class FakeResponse final : public http_client::Response
6667
return true;
6768
}
6869

69-
http_client::StatusCode GetStatusCode() const noexcept override { return 200; }
70+
http_client::StatusCode GetStatusCode() const noexcept override { return status_; }
7071

7172
private:
73+
http_client::StatusCode status_;
7274
http_client::Body body_;
7375
};
7476

@@ -93,39 +95,59 @@ class FakeRequest final : public http_client::Request
9395
void SetRetryPolicy(const http_client::RetryPolicy &) noexcept override {}
9496
};
9597

96-
// A session whose SendRequest() answers synchronously with a successful FakeResponse, so the
97-
// exporter's own wait for a response returns immediately without needing a real connection.
98+
// What the client does with a request, called from inside SendRequest() so the exporter's own
99+
// wait returns without needing a connection. The default answers once, successfully, which is
100+
// what a case wants when the response is not the thing under test.
101+
using EventScript = std::function<void(http_client::EventHandler &)>;
102+
103+
EventScript AnswerSuccessfully()
104+
{
105+
return [](http_client::EventHandler &handler) {
106+
FakeResponse response;
107+
handler.OnResponse(response);
108+
};
109+
}
110+
98111
class FakeSession final : public http_client::Session
99112
{
100113
public:
114+
explicit FakeSession(EventScript script = AnswerSuccessfully()) : script_(std::move(script)) {}
115+
101116
std::shared_ptr<http_client::Request> CreateRequest() noexcept override
102117
{
103118
return std::make_shared<FakeRequest>();
104119
}
105120

106121
void SendRequest(std::shared_ptr<http_client::EventHandler> handler) noexcept override
107122
{
108-
FakeResponse response;
109-
handler->OnResponse(response);
123+
script_(*handler);
110124
}
111125

112126
bool IsSessionActive() noexcept override { return true; }
113127
bool CancelSession() noexcept override { return true; }
114128
bool FinishSession() noexcept override { return true; }
129+
130+
private:
131+
EventScript script_;
115132
};
116133

117134
class FakeHttpClient final : public http_client::HttpClient
118135
{
119136
public:
137+
explicit FakeHttpClient(EventScript script = AnswerSuccessfully()) : script_(std::move(script)) {}
138+
120139
std::shared_ptr<http_client::Session> CreateSession(
121140
opentelemetry::nostd::string_view) noexcept override
122141
{
123-
return std::make_shared<FakeSession>();
142+
return std::make_shared<FakeSession>(script_);
124143
}
125144

126145
bool CancelAllSessions() noexcept override { return true; }
127146
bool FinishAllSessions() noexcept override { return true; }
128147
void SetMaxSessionsPerConnection(std::size_t) noexcept override {}
148+
149+
private:
150+
EventScript script_;
129151
};
130152

131153
} // namespace
@@ -286,68 +308,6 @@ namespace http_client = opentelemetry::ext::http::client;
286308
constexpr const char *kAcceptedBody =
287309
R"({"took":30,"errors":false,"items":[{"index":{"_index":"logs","status":201,"_shards":{"failed" : 0}}}]})";
288310

289-
class FakeResponse : public http_client::Response
290-
{
291-
public:
292-
FakeResponse(http_client::StatusCode status, const std::string &body)
293-
: status_(status), body_(body.begin(), body.end())
294-
{}
295-
const http_client::Body &GetBody() const noexcept override { return body_; }
296-
bool ForEachHeader(
297-
nostd::function_ref<bool(nostd::string_view, nostd::string_view)>) const noexcept override
298-
{
299-
return true;
300-
}
301-
bool ForEachHeader(
302-
const nostd::string_view &,
303-
nostd::function_ref<bool(nostd::string_view, nostd::string_view)>) const noexcept override
304-
{
305-
return true;
306-
}
307-
http_client::StatusCode GetStatusCode() const noexcept override { return status_; }
308-
309-
private:
310-
http_client::StatusCode status_;
311-
http_client::Body body_;
312-
};
313-
314-
class FakeRequest : public http_client::Request
315-
{
316-
public:
317-
void SetMethod(http_client::Method) noexcept override {}
318-
void SetUri(nostd::string_view) noexcept override {}
319-
void SetSslOptions(const http_client::HttpSslOptions &) noexcept override {}
320-
void SetBody(http_client::Body &) noexcept override {}
321-
void AddHeader(nostd::string_view, nostd::string_view) noexcept override {}
322-
void ReplaceHeader(nostd::string_view, nostd::string_view) noexcept override {}
323-
void SetTimeoutMs(std::chrono::milliseconds) noexcept override {}
324-
void SetCompression(const http_client::Compression &) noexcept override {}
325-
void EnableLogging(bool) noexcept override {}
326-
void SetRetryPolicy(const http_client::RetryPolicy &) noexcept override {}
327-
};
328-
329-
using EventScript = std::function<void(http_client::EventHandler &)>;
330-
331-
class FakeSession : public http_client::Session
332-
{
333-
public:
334-
explicit FakeSession(EventScript script) : script_(std::move(script)) {}
335-
std::shared_ptr<http_client::Request> CreateRequest() noexcept override
336-
{
337-
return std::make_shared<FakeRequest>();
338-
}
339-
void SendRequest(std::shared_ptr<http_client::EventHandler> handler) noexcept override
340-
{
341-
script_(*handler);
342-
}
343-
bool IsSessionActive() noexcept override { return false; }
344-
bool CancelSession() noexcept override { return true; }
345-
bool FinishSession() noexcept override { return true; }
346-
347-
private:
348-
EventScript script_;
349-
};
350-
351311
// Keeps the handler and returns, so the export reaches its wait with nothing recorded and only a
352312
// notification can end it.
353313
class DeferredSession : public http_client::Session
@@ -395,22 +355,6 @@ class DeferredHttpClient : public http_client::HttpClient
395355
std::promise<std::shared_ptr<http_client::EventHandler>> *arrived_;
396356
};
397357

398-
class FakeHttpClient : public http_client::HttpClient
399-
{
400-
public:
401-
explicit FakeHttpClient(EventScript script) : script_(std::move(script)) {}
402-
std::shared_ptr<http_client::Session> CreateSession(nostd::string_view) noexcept override
403-
{
404-
return std::make_shared<FakeSession>(script_);
405-
}
406-
bool CancelAllSessions() noexcept override { return true; }
407-
bool FinishAllSessions() noexcept override { return true; }
408-
void SetMaxSessionsPerConnection(std::size_t) noexcept override {}
409-
410-
private:
411-
EventScript script_;
412-
};
413-
414358
opentelemetry::sdk::common::ExportResult ExportWith(EventScript script)
415359
{
416360
auto client = std::make_shared<FakeHttpClient>(std::move(script));

0 commit comments

Comments
 (0)