Skip to content

Commit 95eae42

Browse files
committed
Fixed #12 Added unit-test for read yml like docker-compose samples
1 parent 0b57a80 commit 95eae42

File tree

5 files changed

+131
-6
lines changed

5 files changed

+131
-6
lines changed

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_yaml_parser_arra
4545
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_for_map.cpp")
4646
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_remove_element_in_array.cpp")
4747
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp")
48+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_read_yaml.cpp")
4849

4950
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
5051

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# https://github.com/dockersamples/example-voting-app/blob/master/docker-compose.yml
2+
3+
version: "3"
4+
5+
services:
6+
vote:
7+
build: ./vote
8+
command: python app.py
9+
volumes:
10+
- ./vote:/app
11+
ports:
12+
- "5000:80"
13+
networks:
14+
- front-tier
15+
- back-tier
16+
17+
result:
18+
build: ./result
19+
command: nodemon server.js
20+
volumes:
21+
- ./result:/app
22+
ports:
23+
- "5001:80"
24+
- "5858:5858"
25+
networks:
26+
- front-tier
27+
- back-tier
28+
29+
worker:
30+
build:
31+
context: ./worker
32+
depends_on:
33+
- "redis"
34+
- "db"
35+
networks:
36+
- back-tier
37+
38+
redis:
39+
image: redis:alpine
40+
container_name: redis
41+
ports: ["6379"]
42+
networks:
43+
- back-tier
44+
45+
db:
46+
image: postgres:9.4
47+
container_name: db
48+
environment:
49+
POSTGRES_USER: "postgres"
50+
POSTGRES_PASSWORD: "postgres"
51+
volumes:
52+
- "db-data:/var/lib/postgresql/data"
53+
networks:
54+
- back-tier
55+
56+
volumes:
57+
db-data:
58+
59+
networks:
60+
front-tier:
61+
back-tier:

unit-tests.wsjcpp/src/unit_test_memory_leaks.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <fstream>
88
#include <string>
99
#include <wsjcpp_yaml.h>
10+
#include <chrono>
11+
#include <thread>
1012

1113
//////////////////////////////////////////////////////////////////////////////
1214
//
@@ -81,23 +83,24 @@ bool UnitTestMemoryLeaks::doBeforeTest() {
8183

8284
void UnitTestMemoryLeaks::executeTest() {
8385
double nBeforeVm, nBeforeRss;
86+
double nAfterVm, nAfterRss;
87+
std::string sFilepath = "./data-tests/for-memory-leak/some.yml";
88+
std::string sError;
89+
8490
process_mem_usage(nBeforeVm, nBeforeRss);
8591
// std::cout << "nBeforeVm: " << nBeforeVm << std::endl;
8692
// std::cout << "nBeforeRss: " << nBeforeRss << std::endl;
8793
compare("memory vm not null", (int)nBeforeVm > 0, true);
8894
compare("memory vm not null", (int)nBeforeRss > 0, true);
8995

9096
for (int i = 0; i < 1000; i++) {
91-
WsjcppYaml *pYaml = new WsjcppYaml();
92-
std::string sError;
93-
if (!compare("Error parsing", pYaml->loadFromFile("./data-tests/for-memory-leak/some.yml", sError), true)) {
97+
WsjcppYaml yaml;
98+
if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) {
9499
WsjcppLog::err(TAG, sError);
95100
return;
96101
}
97-
delete pYaml;
98102
}
99-
100-
double nAfterVm, nAfterRss;
103+
// std::this_thread::sleep_for(std::chrono::seconds(1));
101104
process_mem_usage(nAfterVm, nAfterRss);
102105
// std::cout << "nAfterVm: " << nAfterVm << std::endl;
103106
// std::cout << "nAfterRss: " << nAfterRss << std::endl;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include <wsjcpp_core.h>
3+
#include <wsjcpp_unit_tests.h>
4+
#include <wsjcpp_yaml.h>
5+
6+
// ---------------------------------------------------------------------
7+
// UnitTestReadYaml
8+
9+
class UnitTestReadYaml : public WsjcppUnitTestBase {
10+
public:
11+
UnitTestReadYaml();
12+
virtual bool doBeforeTest() override;
13+
virtual void executeTest() override;
14+
virtual bool doAfterTest() override;
15+
};
16+
17+
REGISTRY_WSJCPP_UNIT_TEST(UnitTestReadYaml)
18+
19+
UnitTestReadYaml::UnitTestReadYaml()
20+
: WsjcppUnitTestBase("UnitTestReadYaml") {
21+
}
22+
23+
// ---------------------------------------------------------------------
24+
25+
bool UnitTestReadYaml::doBeforeTest() {
26+
// do something before test
27+
return true;
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void UnitTestReadYaml::executeTest() {
33+
WsjcppYaml yaml;
34+
std::string sFilepath = "./data-tests/read-file/example-voiting-app/docker-compose.yml";
35+
std::string sError;
36+
if (!compare("Error parsing", yaml.loadFromFile(sFilepath, sError), true)) {
37+
WsjcppLog::err(TAG, sError);
38+
return;
39+
}
40+
41+
compare("has version", yaml.getRoot()->hasElement("version"), true);
42+
compare("has volumes", yaml.getRoot()->hasElement("volumes"), true);
43+
compare("has networks", yaml.getRoot()->hasElement("networks"), true);
44+
compare("has services", yaml.getRoot()->hasElement("services"), true);
45+
46+
compare("version-value", yaml.getRoot()->getElement("version")->getValue(), "3");
47+
48+
// TODO unit test source code here
49+
}
50+
51+
// ---------------------------------------------------------------------
52+
53+
bool UnitTestReadYaml::doAfterTest() {
54+
// do somethig after test
55+
return true;
56+
}
57+
58+

wsjcpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ unit-tests:
5757
description: ""
5858
- name: "MemoryLeaks"
5959
description: ""
60+
- name: "ReadYaml"
61+
description: ""

0 commit comments

Comments
 (0)