Skip to content

Commit aa328f2

Browse files
authored
Add CI pipeline and a very basic test (#3)
* Add a very basic test of the statsd client * Setup a simple test and a CI pipeline
1 parent f5a0445 commit aa328f2

File tree

7 files changed

+120
-31
lines changed

7 files changed

+120
-31
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
build/
1+
bin

.travis.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
language: cpp
2+
3+
matrix:
4+
include:
5+
- os: linux
6+
addons:
7+
apt:
8+
sources:
9+
- ubuntu-toolchain-r-test
10+
packages:
11+
- g++-7
12+
env:
13+
- MATRIX_EVAL="BUILD_MODE=Debug && CC=gcc-7 && CXX=g++-7 && CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++17"
14+
15+
- os: linux
16+
addons:
17+
apt:
18+
sources:
19+
- ubuntu-toolchain-r-test
20+
packages:
21+
- g++-7
22+
env:
23+
- MATRIX_EVAL="BUILD_MODE=Release && CC=gcc-7 && CXX=g++-7 && CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++17"
24+
25+
- os: linux
26+
addons:
27+
apt:
28+
sources:
29+
- ubuntu-toolchain-r-test
30+
packages:
31+
- g++-7
32+
env:
33+
- MATRIX_EVAL="BUILD_MODE=Debug && CC=gcc-7 && CXX=g++-7 && CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++14"
34+
35+
- os: linux
36+
addons:
37+
apt:
38+
sources:
39+
- ubuntu-toolchain-r-test
40+
packages:
41+
- g++-7
42+
env:
43+
- MATRIX_EVAL="BUILD_MODE=Release && CC=gcc-7 && CXX=g++-7 && CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++14"
44+
45+
before_install:
46+
- eval "${MATRIX_EVAL}"
47+
48+
script: make test

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.2)
2+
3+
project(StatsdClient)
4+
5+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
6+
7+
install(FILES src/* DESTINATION include)
8+
9+
find_package (Threads)
10+
add_executable(testStatsdClient ${CMAKE_CURRENT_SOURCE_DIR}/tests/testStatsdClient.cpp)
11+
target_link_libraries(testStatsdClient ${CMAKE_THREAD_LIBS_INIT})
12+
13+
enable_testing()
14+
add_test(testSuite testStatsdClient)

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# simple makefile to build, test and clean
2+
3+
BUILD_MODE ?= Release
4+
5+
build: clean
6+
@echo "Build in ${BUILD_MODE} mode"
7+
mkdir -p bin/${BUILD_MODE}
8+
@cd bin/${BUILD_MODE}; cmake ../../ -DCMAKE_BUILD_TYPE=${BUILD_MODE}
9+
@cd bin/${BUILD_MODE}; make
10+
11+
test: build
12+
@cd bin/${BUILD_MODE}; make test
13+
14+
install: build
15+
@cd bin/${BUILD_MODE}; make install
16+
17+
clean:
18+
@rm -rf bin

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using namespace Statsd;
2020
int main()
2121
{
2222
// Define the client
23-
StatsdClient client{ "127.0.0.1", 5005, "myPrefix_", true };
23+
StatsdClient client{ "127.0.0.1", 5005, "myPrefix.", 3 };
2424

2525
// Increment "coco"
2626
client.increment("coco");

src/StatsdClient.hpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ class StatsdClient
6565

6666
//!@}
6767

68-
private:
69-
70-
// @name Private methods
71-
// @{
72-
73-
//! Returns a cleaned key
74-
inline std::string clean(const std::string& key) const noexcept;
75-
76-
// @}
77-
7868
private:
7969

8070
//! The prefix to be used for metrics
@@ -166,9 +156,6 @@ send(const std::string& key, const int value, const std::string& type, const flo
166156
}
167157
}
168158

169-
// Clean the key
170-
clean(key);
171-
172159
// Prepare the buffer, with a sampling rate if specified different from 1.0f
173160
char buffer[256];
174161
if (isFrequencyOne(frequency))
@@ -186,22 +173,6 @@ send(const std::string& key, const int value, const std::string& type, const flo
186173
m_sender.send(buffer);
187174
}
188175

189-
std::string
190-
StatsdClient::
191-
clean(const std::string& key) const noexcept
192-
{
193-
std::string cleanKey = key;
194-
size_t pos = key.find_first_of(":|@");
195-
196-
// Add the '_' appropriately to the key
197-
while (pos != std::string::npos)
198-
{
199-
cleanKey[pos] = '_';
200-
pos = cleanKey.find_first_of(":|@");
201-
}
202-
return cleanKey;
203-
}
204-
205176
}
206177

207178
#endif

tests/testStatsdClient.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <StatsdClient.hpp>
2+
#include <cassert>
3+
4+
using namespace Statsd;
5+
6+
int main()
7+
{
8+
StatsdClient client{ "localhost", 8080, "myPrefix.", 1 };
9+
10+
// set a new config with a different port
11+
client.setConfig("localhost", 80, "myPrefix.");
12+
13+
// Increment "coco"
14+
client.increment("coco");
15+
assert(!client.errorMessage());
16+
17+
// Decrement "kiki"
18+
client.decrement("kiki");
19+
assert(!client.errorMessage());
20+
21+
// Adjusts "toto" by +3
22+
client.count("toto", 2, 0.1f);
23+
assert(!client.errorMessage());
24+
25+
// Record a gauge "titi" to 3
26+
client.gauge("titi", 3);
27+
assert(!client.errorMessage());
28+
29+
// Record a timing of 2ms for "myTiming"
30+
client.timing("myTiming", 2, 0.1f);
31+
assert(!client.errorMessage());
32+
33+
// Send a metric explicitly
34+
client.send("tutu", 4, "c", 2.0f);
35+
assert(!client.errorMessage());
36+
37+
return EXIT_SUCCESS;
38+
}

0 commit comments

Comments
 (0)