Skip to content

Commit b890969

Browse files
authored
[TEST] Fix sync problems in OTLP File exporter tests. (#3031)
1 parent 1203bcf commit b890969

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

exporters/otlp/test/otlp_file_exporter_test.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "nlohmann/json.hpp"
2626

2727
#include <chrono>
28+
#include <iostream>
2829
#include <sstream>
2930

3031
using namespace testing;
@@ -112,13 +113,24 @@ class OtlpFileExporterTestPeer : public ::testing::Test
112113

113114
provider->ForceFlush();
114115

116+
output.flush();
117+
output.sync();
118+
auto check_json_text = output.str();
119+
if (!check_json_text.empty())
115120
{
116-
auto check_json = nlohmann::json::parse(output.str(), nullptr, false);
117-
auto resource_span = *check_json["resourceSpans"].begin();
118-
auto scope_span = *resource_span["scopeSpans"].begin();
119-
auto span = *scope_span["spans"].begin();
120-
auto received_trace_id = span["traceId"].get<std::string>();
121-
EXPECT_EQ(received_trace_id, report_trace_id);
121+
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
122+
if (!check_json.is_discarded())
123+
{
124+
auto resource_span = *check_json["resourceSpans"].begin();
125+
auto scope_span = *resource_span["scopeSpans"].begin();
126+
auto span = *scope_span["spans"].begin();
127+
auto received_trace_id = span["traceId"].get<std::string>();
128+
EXPECT_EQ(received_trace_id, report_trace_id);
129+
}
130+
else
131+
{
132+
FAIL() << "Failed to parse json:" << check_json_text;
133+
}
122134
}
123135
}
124136
};

exporters/otlp/test/otlp_file_log_record_exporter_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ class OtlpFileLogRecordExporterTestPeer : public ::testing::Test
122122

123123
provider->ForceFlush();
124124

125+
output.flush();
126+
output.sync();
127+
auto check_json_text = output.str();
128+
if (!check_json_text.empty())
125129
{
126-
auto check_json = nlohmann::json::parse(output.str(), nullptr, false);
130+
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
127131
auto resource_logs = *check_json["resourceLogs"].begin();
128132
auto scope_logs = *resource_logs["scopeLogs"].begin();
129133
auto scope = scope_logs["scope"];

exporters/otlp/test/otlp_file_metric_exporter_test.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
106106

107107
exporter->ForceFlush();
108108

109+
output.flush();
110+
output.sync();
111+
auto check_json_text = output.str();
112+
if (!check_json_text.empty())
109113
{
110-
auto check_json = nlohmann::json::parse(output.str(), nullptr, false);
114+
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
111115

112116
auto resource_metrics = *check_json["resourceMetrics"].begin();
113117
auto scope_metrics = *resource_metrics["scopeMetrics"].begin();
@@ -167,8 +171,12 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
167171

168172
exporter->ForceFlush();
169173

174+
output.flush();
175+
output.sync();
176+
auto check_json_text = output.str();
177+
if (!check_json_text.empty())
170178
{
171-
auto check_json = nlohmann::json::parse(output.str(), nullptr, false);
179+
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
172180

173181
auto resource_metrics = *check_json["resourceMetrics"].begin();
174182
auto scope_metrics = *resource_metrics["scopeMetrics"].begin();
@@ -233,8 +241,12 @@ class OtlpFileMetricExporterTestPeer : public ::testing::Test
233241

234242
exporter->ForceFlush();
235243

244+
output.flush();
245+
output.sync();
246+
auto check_json_text = output.str();
247+
if (!check_json_text.empty())
236248
{
237-
auto check_json = nlohmann::json::parse(output.str(), nullptr, false);
249+
auto check_json = nlohmann::json::parse(check_json_text, nullptr, false);
238250

239251
auto resource_metrics = *check_json["resourceMetrics"].begin();
240252
auto scope_metrics = *resource_metrics["scopeMetrics"].begin();

0 commit comments

Comments
 (0)