1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-30 14:07:11 +02:00

Update POCO library.

This commit is contained in:
Sandu Liviu Catalin
2023-03-23 20:19:11 +02:00
parent 8d15f4b6e9
commit 233fc103f9
2521 changed files with 257092 additions and 72789 deletions

View File

@ -0,0 +1,79 @@
//
// CallbackMetricTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CallbackMetricTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/CallbackMetric.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Exception.h"
#include <sstream>
using namespace Poco::Prometheus;
using namespace std::string_literals;
CallbackMetricTest::CallbackMetricTest(const std::string& name):
CppUnit::TestCase("CallbackMetricTest"s)
{
}
void CallbackMetricTest::testExport()
{
CallbackIntCounter counter1("counter_1"s, []()
{
return 42;
});
counter1.help("A test counter"s);
CallbackIntGauge gauge1("gauge_1"s, []()
{
return 2112;
});
gauge1.help("A test gauge"s);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# HELP counter_1 A test counter\n"
"# TYPE counter_1 counter\n"
"counter_1 42\n"
"# HELP gauge_1 A test gauge\n"
"# TYPE gauge_1 gauge\n"
"gauge_1 2112\n"s,
text);
}
void CallbackMetricTest::setUp()
{
Registry::defaultRegistry().clear();
}
void CallbackMetricTest::tearDown()
{
}
CppUnit::Test* CallbackMetricTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CallbackMetricTest");
CppUnit_addTest(pSuite, CallbackMetricTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,35 @@
//
// CallbackMetricTest.h
//
// Definition of the CallbackMetricTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef CallbackMetricTest_INCLUDED
#define CallbackMetricTest_INCLUDED
#include "CppUnit/TestCase.h"
class CallbackMetricTest: public CppUnit::TestCase
{
public:
CallbackMetricTest(const std::string& name);
~CallbackMetricTest() = default;
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // CallbackMetricTest_INCLUDED

View File

@ -0,0 +1,240 @@
//
// CounterTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CounterTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/Counter.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Exception.h"
#include <sstream>
using namespace Poco::Prometheus;
using namespace std::string_literals;
CounterTest::CounterTest(const std::string& name):
CppUnit::TestCase("CounterTest"s)
{
}
void CounterTest::testBasicBehavior()
{
Counter counter("counter"s);
assertEqualDelta(0.0, counter.value(), 0.0);
counter.inc();
assertEqualDelta(1.0, counter.value(), 0.0);
counter.inc(2);
assertEqualDelta(3.0, counter.value(), 0.0);
}
void CounterTest::testInvalidName()
{
try
{
Counter counter("invalid.name"s);
fail("invalid name - must throw"s);
}
catch (Poco::SyntaxException&)
{
}
}
void CounterTest::testLabels()
{
const std::vector<std::string> labelNames{"label1"s, "label2"};
Counter counter("counter"s);
counter.labelNames(labelNames);
assertTrue(labelNames == counter.labelNames());
counter.labels({"value11"s, "value21"s}).inc();
counter.labels({"value12"s, "value22"s}).inc(2);
assertEqualDelta(1.0, counter.labels({"value11"s, "value21"s}).value(), 0.0);
assertEqualDelta(2.0, counter.labels({"value12"s, "value22"s}).value(), 0.0);
assertEqual(2, counter.sampleCount());
counter.remove({"value12"s, "value22"s});
assertEqual(1, counter.sampleCount());
counter.clear();
assertEqual(0, counter.sampleCount());
try
{
counter.labels({"value11"s}).inc();
fail("missing label values - must throw"s);
}
catch (Poco::InvalidArgumentException&)
{
}
try
{
counter.labels({"value11"s, "value21"s, "value31"s}).inc();
fail("too many label values - must throw"s);
}
catch (Poco::InvalidArgumentException&)
{
}
try
{
counter.labelNames({"label3"s, "label4"s});
fail("cannot change label values - must throw");
}
catch (Poco::IllegalStateException&)
{
}
try
{
Counter counter2("counter2"s);
counter2.labelNames({"invalid.value"s});
fail("invalid label name - must throw"s);
}
catch (Poco::SyntaxException&)
{
}
}
void CounterTest::testConcurrency()
{
class R: public Poco::Runnable
{
public:
R(Counter& c, int n):
_c(c),
_n(n)
{
}
void run()
{
for (int i = 0; i < _n; i++)
{
_c.inc();
}
}
private:
Counter& _c;
int _n;
};
Poco::Thread t1;
Poco::Thread t2;
Poco::Thread t3;
Poco::Thread t4;
Counter counter("counter1"s);
const int n = 1000000;
R r(counter, n);
t1.start(r);
t2.start(r);
t3.start(r);
t4.start(r);
t1.join();
t2.join();
t3.join();
t4.join();
assertEqualDelta(4.0*n, counter.value(), 0.0);
}
void CounterTest::testExport()
{
Counter counter1("counter_1"s);
counter1.help("A test counter"s);
Counter counter2("counter_2"s);
counter2.help("Another test counter"s);
Counter counter3("counter_3"s, {
/*.help =*/ "A test counter with one label"s,
/*.labelNames =*/ {"label1"s}
});
Counter counter4("counter_4"s, {
/*.help =*/ "A test counter with two labels"s,
/*.labelNames =*/ {"label1"s, "label2"s}
});
counter2.inc();
counter3.labels({"value11"}).inc(2);
counter3.labels({"value12"}).inc(3);
counter4.labels({"value11"s, "value21"s}).inc(4);
counter4.labels({"value12"s, "value22"s}).inc(5);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# HELP counter_1 A test counter\n"
"# TYPE counter_1 counter\n"
"counter_1 0\n"
"# HELP counter_2 Another test counter\n"
"# TYPE counter_2 counter\n"
"counter_2 1\n"
"# HELP counter_3 A test counter with one label\n"
"# TYPE counter_3 counter\n"
"counter_3{label1=\"value11\"} 2\n"
"counter_3{label1=\"value12\"} 3\n"
"# HELP counter_4 A test counter with two labels\n"
"# TYPE counter_4 counter\n"
"counter_4{label1=\"value11\",label2=\"value21\"} 4\n"
"counter_4{label1=\"value12\",label2=\"value22\"} 5\n"s,
text);
}
void CounterTest::setUp()
{
Registry::defaultRegistry().clear();
}
void CounterTest::tearDown()
{
}
CppUnit::Test* CounterTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CounterTest");
CppUnit_addTest(pSuite, CounterTest, testBasicBehavior);
CppUnit_addTest(pSuite, CounterTest, testInvalidName);
CppUnit_addTest(pSuite, CounterTest, testLabels);
CppUnit_addTest(pSuite, CounterTest, testConcurrency);
CppUnit_addTest(pSuite, CounterTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,39 @@
//
// CounterTest.h
//
// Definition of the CounterTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef CounterTest_INCLUDED
#define CounterTest_INCLUDED
#include "CppUnit/TestCase.h"
class CounterTest: public CppUnit::TestCase
{
public:
CounterTest(const std::string& name);
~CounterTest() = default;
void testBasicBehavior();
void testInvalidName();
void testLabels();
void testConcurrency();
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // CounterTest_INCLUDED

View File

@ -0,0 +1,17 @@
//
// Driver.cpp
//
// Console-based test driver for Poco Prometheus.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CppUnit/TestRunner.h"
#include "PrometheusTestSuite.h"
CppUnitMain(PrometheusTestSuite)

View File

@ -0,0 +1,204 @@
//
// GaugeTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "GaugeTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/Gauge.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Exception.h"
#include <sstream>
#include <cmath>
using namespace Poco::Prometheus;
using namespace std::string_literals;
GaugeTest::GaugeTest(const std::string& name):
CppUnit::TestCase("GaugeTest"s)
{
}
void GaugeTest::testBasicBehavior()
{
Gauge gauge("gauge"s);
assertEqualDelta(0.0, gauge.value(), 0.0);
gauge.inc();
assertEqualDelta(1.0, gauge.value(), 0.0);
gauge.inc(2.0);
assertEqualDelta(3.0, gauge.value(), 0.0);
gauge.dec(2.0);
assertEqualDelta(1.0, gauge.value(), 0.0);
gauge.set(5.0);
assertEqualDelta(5.0, gauge.value(), 0.0);
}
void GaugeTest::testInvalidName()
{
try
{
Gauge gauge("invalid.name"s);
fail("invalid name - must throw"s);
}
catch (Poco::SyntaxException&)
{
}
}
void GaugeTest::testLabels()
{
const std::vector<std::string> labelNames{"label1"s, "label2"};
Gauge gauge("gauge"s);
gauge.labelNames(labelNames);
assertTrue(labelNames == gauge.labelNames());
gauge.labels({"value11"s, "value21"s}).inc();
gauge.labels({"value12"s, "value22"s}).inc(2);
assertEqualDelta(1.0, gauge.labels({"value11"s, "value21"s}).value(), 0.0);
assertEqualDelta(2.0, gauge.labels({"value12"s, "value22"s}).value(), 0.0);
try
{
gauge.labels({"value11"s}).inc();
fail("no label values - must throw"s);
}
catch (Poco::InvalidArgumentException&)
{
}
try
{
gauge.labels({"value11"s, "value21"s, "value31"s}).inc();
fail("too many label values - must throw"s);
}
catch (Poco::InvalidArgumentException&)
{
}
try
{
gauge.labelNames({"label3"s, "label4"s});
fail("cannot change label values - must throw");
}
catch (Poco::IllegalStateException&)
{
}
try
{
Gauge gauge2("Gauge2"s);
gauge2.labelNames({"invalid.value"s});
fail("invalid label name - must throw"s);
}
catch (Poco::SyntaxException&)
{
}
}
void GaugeTest::testExport()
{
Gauge gauge1("gauge_1"s);
gauge1.help("A test gauge"s);
Gauge gauge2("gauge_2"s);
gauge2.help("Another test gauge"s);
Gauge gauge3("gauge_3"s, {
/*.help =*/ "A test gauge with one label"s,
/*.labelNames =*/ {"label1"s}
});
Gauge gauge4("gauge_4"s, {
/*.help =*/ "A test gauge with two labels"s,
/*.labelNames =*/ {"label1"s, "label2"s}
});
gauge2.inc();
gauge3.labels({"value11"}).inc(2);
gauge3.labels({"value12"}).inc(3);
gauge4.labels({"value11"s, "value21"s}).inc(4);
gauge4.labels({"value12"s, "value22"s}).inc(5);
Gauge gauge5("gauge_5"s, {
/*.help =*/ "A test for special values"s,
/*.labelNames =*/ {"test"s}
});
gauge5.labels({"+Inf"}).set(INFINITY);
gauge5.labels({"-Inf"}).set(-INFINITY);
gauge5.labels({"NaN"}).set(NAN);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# HELP gauge_1 A test gauge\n"
"# TYPE gauge_1 gauge\n"
"gauge_1 0\n"
"# HELP gauge_2 Another test gauge\n"
"# TYPE gauge_2 gauge\n"
"gauge_2 1\n"
"# HELP gauge_3 A test gauge with one label\n"
"# TYPE gauge_3 gauge\n"
"gauge_3{label1=\"value11\"} 2\n"
"gauge_3{label1=\"value12\"} 3\n"
"# HELP gauge_4 A test gauge with two labels\n"
"# TYPE gauge_4 gauge\n"
"gauge_4{label1=\"value11\",label2=\"value21\"} 4\n"
"gauge_4{label1=\"value12\",label2=\"value22\"} 5\n"
"# HELP gauge_5 A test for special values\n"
"# TYPE gauge_5 gauge\n"
"gauge_5{test=\"+Inf\"} +Inf\n"
"gauge_5{test=\"-Inf\"} -Inf\n"
"gauge_5{test=\"NaN\"} NaN\n"s,
text);
}
void GaugeTest::setUp()
{
Registry::defaultRegistry().clear();
}
void GaugeTest::tearDown()
{
}
CppUnit::Test* GaugeTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("GaugeTest");
CppUnit_addTest(pSuite, GaugeTest, testBasicBehavior);
CppUnit_addTest(pSuite, GaugeTest, testInvalidName);
CppUnit_addTest(pSuite, GaugeTest, testLabels);
CppUnit_addTest(pSuite, GaugeTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,38 @@
//
// GaugeTest.h
//
// Definition of the GaugeTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef GaugeTest_INCLUDED
#define GaugeTest_INCLUDED
#include "CppUnit/TestCase.h"
class GaugeTest: public CppUnit::TestCase
{
public:
GaugeTest(const std::string& name);
~GaugeTest() = default;
void testBasicBehavior();
void testInvalidName();
void testLabels();
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // GaugeTest_INCLUDED

View File

@ -0,0 +1,178 @@
//
// HistogramTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "HistogramTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/Histogram.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Exception.h"
#include <sstream>
using namespace Poco::Prometheus;
using namespace std::string_literals;
HistogramTest::HistogramTest(const std::string& name):
CppUnit::TestCase("HistogramTest"s)
{
}
void HistogramTest::testNoBuckets()
{
Histogram histo("histo"s);
histo.observe(1.0);
histo.observe(2.0);
const auto data = histo.data();
assertEqual(0, data.bucketCounts.size());
assertEqual(2, data.count);
assertEqualDelta(3.0, data.sum, 0.0);
}
void HistogramTest::testBuckets()
{
Histogram histo("histo"s);
histo.buckets({1.0, 2.0, 3.0});
histo.observe(1.1);
histo.observe(2.0);
histo.observe(2.5);
histo.observe(4.0);
const auto data = histo.data();
assertEqual(3, data.bucketCounts.size());
assertEqual(0, data.bucketCounts[0]);
assertEqual(2, data.bucketCounts[1]);
assertEqual(3, data.bucketCounts[2]);
assertEqual(4, data.count);
assertEqualDelta(9.6, data.sum, 0.001);
}
void HistogramTest::testLabels()
{
Histogram histo("histo"s);
histo
.buckets({1.0, 2.0, 3.0})
.labelNames({"label"s});
histo.labels({"value1"s}).observe(1.1);
histo.labels({"value1"s}).observe(2.0);
histo.labels({"value1"s}).observe(2.5);
histo.labels({"value1"s}).observe(4.0);
histo.labels({"value2"s}).observe(3.0);
histo.labels({"value2"s}).observe(5.0);
const auto data1 = histo.labels({"value1"s}).data();
assertEqual(3, data1.bucketCounts.size());
assertEqual(0, data1.bucketCounts[0]);
assertEqual(2, data1.bucketCounts[1]);
assertEqual(3, data1.bucketCounts[2]);
assertEqual(4, data1.count);
assertEqualDelta(9.6, data1.sum, 0.001);
const auto data2 = histo.labels({"value2"s}).data();
assertEqual(3, data2.bucketCounts.size());
assertEqual(0, data2.bucketCounts[0]);
assertEqual(0, data2.bucketCounts[1]);
assertEqual(1, data2.bucketCounts[2]);
assertEqual(2, data2.count);
assertEqualDelta(8.0, data2.sum, 0.001);
}
void HistogramTest::testExport()
{
Histogram histo1("histo_1"s);
histo1.observe(1.0);
histo1.observe(2.0);
Histogram histo2("histo_2"s, {
/*.help =*/ "A histogram with buckets"s,
/* .labelNames =*/ {},
/*.buckets =*/ {1.0, 2.0, 3.0}
});
histo2.observe(1.1);
histo2.observe(2.0);
histo2.observe(2.5);
histo2.observe(4.0);
Histogram histo3("histo_3"s, {
/*.help =*/ "A histogram with labels"s,
/*.labelNames =*/ {"label"s},
/*.buckets =*/ {1.0, 2.0}
});
histo3.labels({"value1"s}).observe(1.0);
histo3.labels({"value1"s}).observe(2.0);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# TYPE histo_1 histogram\n"
"histo_1_bucket{le=\"+Inf\"} 2\n"
"histo_1_sum 3\n"
"histo_1_count 2\n"
"# HELP histo_2 A histogram with buckets\n"
"# TYPE histo_2 histogram\n"
"histo_2_bucket{le=\"1\"} 0\n"
"histo_2_bucket{le=\"2\"} 2\n"
"histo_2_bucket{le=\"3\"} 3\n"
"histo_2_bucket{le=\"+Inf\"} 4\n"
"histo_2_sum 9.6\n"
"histo_2_count 4\n"
"# HELP histo_3 A histogram with labels\n"
"# TYPE histo_3 histogram\n"
"histo_3_bucket{label=\"value1\",le=\"1\"} 1\n"
"histo_3_bucket{label=\"value1\",le=\"2\"} 2\n"
"histo_3_bucket{label=\"value1\",le=\"+Inf\"} 2\n"
"histo_3_sum{label=\"value1\"} 3\n"
"histo_3_count{label=\"value1\"} 2\n"s,
text);
}
void HistogramTest::setUp()
{
Registry::defaultRegistry().clear();
}
void HistogramTest::tearDown()
{
}
CppUnit::Test* HistogramTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HistogramTest");
CppUnit_addTest(pSuite, HistogramTest, testNoBuckets);
CppUnit_addTest(pSuite, HistogramTest, testBuckets);
CppUnit_addTest(pSuite, HistogramTest, testLabels);
CppUnit_addTest(pSuite, HistogramTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,38 @@
//
// HistogramTest.h
//
// Definition of the HistogramTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef HistogramTest_INCLUDED
#define HistogramTest_INCLUDED
#include "CppUnit/TestCase.h"
class HistogramTest: public CppUnit::TestCase
{
public:
HistogramTest(const std::string& name);
~HistogramTest() = default;
void testNoBuckets();
void testBuckets();
void testLabels();
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // HistogramTest_INCLUDED

View File

@ -0,0 +1,90 @@
//
// IntCounterTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "IntCounterTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/IntCounter.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Exception.h"
#include <sstream>
using namespace Poco::Prometheus;
using namespace std::string_literals;
IntCounterTest::IntCounterTest(const std::string& name):
CppUnit::TestCase("IntCounterTest"s)
{
}
void IntCounterTest::testBasicBehavior()
{
IntCounter counter("counter"s);
assertEqual(0, counter.value());
counter.inc();
assertEqual(1, counter.value());
counter.inc(2);
assertEqual(3, counter.value());
}
void IntCounterTest::testExport()
{
IntCounter counter1("counter_1"s);
counter1.help("A test counter"s);
IntCounter counter2("counter_2"s);
counter2.help("Another test counter"s);
counter2.inc(42);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# HELP counter_1 A test counter\n"
"# TYPE counter_1 counter\n"
"counter_1 0\n"
"# HELP counter_2 Another test counter\n"
"# TYPE counter_2 counter\n"
"counter_2 42\n"s,
text);
}
void IntCounterTest::setUp()
{
Registry::defaultRegistry().clear();
}
void IntCounterTest::tearDown()
{
}
CppUnit::Test* IntCounterTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("IntCounterTest");
CppUnit_addTest(pSuite, IntCounterTest, testBasicBehavior);
CppUnit_addTest(pSuite, IntCounterTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,36 @@
//
// IntCounterTest.h
//
// Definition of the IntCounterTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef IntCounterTest_INCLUDED
#define IntCounterTest_INCLUDED
#include "CppUnit/TestCase.h"
class IntCounterTest: public CppUnit::TestCase
{
public:
IntCounterTest(const std::string& name);
~IntCounterTest() = default;
void testBasicBehavior();
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // IntCounterTest_INCLUDED

View File

@ -0,0 +1,96 @@
//
// IntGaugeTest.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "IntGaugeTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Prometheus/IntGauge.h"
#include "Poco/Prometheus/Registry.h"
#include "Poco/Prometheus/TextExporter.h"
#include "Poco/Exception.h"
#include <sstream>
using namespace Poco::Prometheus;
using namespace std::string_literals;
IntGaugeTest::IntGaugeTest(const std::string& name):
CppUnit::TestCase("IntGaugeTest"s)
{
}
void IntGaugeTest::testBasicBehavior()
{
IntGauge gauge("gauge"s);
assertEqual(0, gauge.value());
gauge.inc();
assertEqual(1, gauge.value());
gauge.inc(2);
assertEqual(3, gauge.value());
gauge.dec(2);
assertEqual(1, gauge.value());
gauge.set(5);
assertEqual(5, gauge.value());
}
void IntGaugeTest::testExport()
{
IntGauge gauge1("gauge_1"s);
gauge1.help("A test gauge"s);
IntGauge gauge2("gauge_2"s);
gauge2.help("Another test gauge"s);
gauge2.inc(42);
std::ostringstream stream;
TextExporter exporter(stream);
Registry::defaultRegistry().exportTo(exporter);
const std::string text = stream.str();
assertEqual(
"# HELP gauge_1 A test gauge\n"
"# TYPE gauge_1 gauge\n"
"gauge_1 0\n"
"# HELP gauge_2 Another test gauge\n"
"# TYPE gauge_2 gauge\n"
"gauge_2 42\n"s,
text);
}
void IntGaugeTest::setUp()
{
Registry::defaultRegistry().clear();
}
void IntGaugeTest::tearDown()
{
}
CppUnit::Test* IntGaugeTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("IntGaugeTest");
CppUnit_addTest(pSuite, IntGaugeTest, testBasicBehavior);
CppUnit_addTest(pSuite, IntGaugeTest, testExport);
return pSuite;
}

View File

@ -0,0 +1,36 @@
//
// IntGaugeTest.h
//
// Definition of the IntGaugeTest class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef IntGaugeTest_INCLUDED
#define IntGaugeTest_INCLUDED
#include "CppUnit/TestCase.h"
class IntGaugeTest: public CppUnit::TestCase
{
public:
IntGaugeTest(const std::string& name);
~IntGaugeTest() = default;
void testBasicBehavior();
void testExport();
void setUp();
void tearDown();
static CppUnit::Test* suite();
};
#endif // IntGaugeTest_INCLUDED

View File

@ -0,0 +1,32 @@
//
// PrometheusTestSuite.cpp
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "PrometheusTestSuite.h"
#include "CounterTest.h"
#include "GaugeTest.h"
#include "IntCounterTest.h"
#include "IntGaugeTest.h"
#include "CallbackMetricTest.h"
#include "HistogramTest.h"
CppUnit::Test* PrometheusTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PrometheusTestSuite");
pSuite->addTest(CounterTest::suite());
pSuite->addTest(GaugeTest::suite());
pSuite->addTest(IntCounterTest::suite());
pSuite->addTest(IntGaugeTest::suite());
pSuite->addTest(CallbackMetricTest::suite());
pSuite->addTest(HistogramTest::suite());
return pSuite;
}

View File

@ -0,0 +1,27 @@
//
// PrometheusTestSuite.h
//
// Definition of the PrometheusTestSuite class.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef PrometheusTestSuite_INCLUDED
#define PrometheusTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class PrometheusTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // PrometheusTestSuite_INCLUDED

View File

@ -0,0 +1,30 @@
//
// WinCEDriver.cpp
//
// Console-based test driver for Windows CE.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CppUnit/TestRunner.h"
#include "PrometheusTestSuite.h"
#include <cstdlib>
int _tmain(int argc, wchar_t* argv[])
{
std::vector<std::string> args;
for (int i = 0; i < argc; ++i)
{
char buffer[1024];
std::wcstombs(buffer, argv[i], sizeof(buffer));
args.push_back(std::string(buffer));
}
CppUnit::TestRunner runner;
runner.addTest("PrometheusTestSuite", PrometheusTestSuite::suite());
return runner.run(args) ? 0 : 1;
}

View File

@ -0,0 +1,28 @@
//
// WinDriver.cpp
//
// Windows test driver for Poco Prometheus.
//
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "WinTestRunner/WinTestRunner.h"
#include "PrometheusTestSuite.h"
class TestDriver: public CppUnit::WinTestRunnerApp
{
void TestMain()
{
CppUnit::WinTestRunner runner;
runner.addTest(PrometheusTestSuite::suite());
runner.run();
}
};
TestDriver theDriver;