Skip to content

Commit 5d5cbf7

Browse files
committed
Implement ping
1 parent fdb5bb0 commit 5d5cbf7

File tree

9 files changed

+66
-0
lines changed

9 files changed

+66
-0
lines changed

include/InfluxDB/InfluxDB.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ namespace influxdb
9292
/// \param precision
9393
void setTimePrecision(TimePrecision precision);
9494

95+
/// Check instance is up and running
96+
bool ping();
97+
9598
private:
9699
void addPointToBatch(Point&& point);
97100
std::string joinLineProtocolBatch() const;

include/InfluxDB/Transport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ namespace influxdb
7575
{
7676
throw InfluxDBException{"Time precision is not supported by the selected transport"};
7777
}
78+
79+
virtual bool ping()
80+
{
81+
throw InfluxDBException{"Ping is not supported by the selected transport"};
82+
}
7883
};
7984

8085
} // namespace influxdb

src/HTTP.cxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ namespace influxdb::transports
173173
}
174174
}
175175

176+
bool HTTP::ping()
177+
{
178+
session.SetUrl(cpr::Url{endpointUrl + "/ping"});
179+
180+
const auto response = session.Get();
181+
return response.status_code == cpr::status::HTTP_NO_CONTENT;
182+
}
183+
176184
std::string HTTP::execute(const std::string& cmd)
177185
{
178186
session.SetUrl(cpr::Url{endpointUrl + "/query"});

src/HTTP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace influxdb::transports
7575
void setVerifyCertificate(bool verify);
7676
void setTimeout(std::chrono::milliseconds timeout);
7777
void setTimePrecision(TimePrecision precision) override;
78+
bool ping() override;
7879

7980
private:
8081
std::string endpointUrl;

src/InfluxDB.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ namespace influxdb
151151
mTransport->setTimePrecision(precision);
152152
}
153153

154+
bool InfluxDB::ping()
155+
{
156+
return mTransport->ping();
157+
}
158+
154159
void InfluxDB::addPointToBatch(Point&& point)
155160
{
156161
mPointBatch.emplace_back(std::move(point));

test/HttpTest.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,21 @@ namespace influxdb::test
399399
REQUIRE_THROWS_AS(http.execute("fail-execution"), InfluxDBException);
400400
}
401401

402+
TEST_CASE("Ping returns true if successful", "[HttpTest]")
403+
{
404+
REQUIRE_CALL(sessionMock, Get()).RETURN(createResponse(cpr::ErrorCode::OK, cpr::status::HTTP_NO_CONTENT, "response-content"));
405+
ALLOW_CALL(sessionMock, SetUrl(_));
406+
407+
auto http = createHttp();
408+
CHECK(http.ping());
409+
}
410+
411+
TEST_CASE("Ping returns false if unsuccessful", "[HttpTest]")
412+
{
413+
REQUIRE_CALL(sessionMock, Get()).RETURN(createResponse(cpr::ErrorCode::COULDNT_CONNECT, cpr::status::HTTP_NOT_FOUND, "response-content"));
414+
ALLOW_CALL(sessionMock, SetUrl(_));
415+
416+
auto http = createHttp();
417+
CHECK_FALSE(http.ping());
418+
}
402419
}

test/InfluxDBTest.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,13 @@ namespace influxdb::test
208208
db.write(Point{"p"}.addField("f", 1).setTimestamp(std::chrono::time_point<std::chrono::system_clock>{std::chrono::milliseconds{67}}));
209209
}
210210

211+
TEST_CASE("Ping instance", "[InfluxDBTest]")
212+
{
213+
auto mock = std::make_shared<TransportMock>();
214+
REQUIRE_CALL(*mock, ping()).RETURN(true);
215+
216+
InfluxDB db{std::make_unique<TransportAdapter>(mock)};
217+
CHECK(db.ping());
218+
}
219+
211220
}

test/mock/TransportMock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace influxdb::test
3535
MAKE_MOCK0(createDatabase, void(), override);
3636
MAKE_MOCK1(execute, std::string(const std::string&), override);
3737
MAKE_MOCK1(setTimePrecision, void(TimePrecision), override);
38+
MAKE_MOCK0(ping, bool(), override);
3839
};
3940

4041

@@ -71,6 +72,11 @@ namespace influxdb::test
7172
mockImpl->setTimePrecision(precision);
7273
}
7374

75+
bool ping() override
76+
{
77+
return mockImpl->ping();
78+
}
79+
7480
private:
7581
std::shared_ptr<TransportMock> mockImpl;
7682
};

test/system/InfluxDBST.cxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ namespace influxdb::test
6666

6767
auto db = configure("st_db");
6868

69+
SECTION("Ping up and running instance")
70+
{
71+
CHECK(db->ping());
72+
}
73+
74+
SECTION("Ping on an up and running instance")
75+
{
76+
auto nonexistent = InfluxDBBuilder::http("http://not-existing-instance:8086?db=ignore")
77+
.setTimeout(std::chrono::seconds{2})
78+
.connect();
79+
CHECK_FALSE(nonexistent->ping());
80+
}
6981

7082
SECTION("Database does not exist")
7183
{

0 commit comments

Comments
 (0)