Skip to content

Commit 543a575

Browse files
committed
Adding unit tests
1 parent 94ba49b commit 543a575

7 files changed

Lines changed: 159 additions & 40 deletions

File tree

src/CMakeLists.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,32 @@ set(unit_test_protocol_helper_SRCS
294294
)
295295
set(unit_test_protocol_helper_LIBS
296296
${GLIB_LIBRARIES}
297-
${INIPARSER_LIBRARIES}
298297
)
299298
add_executable(unit_test_protocol_helper ${unit_test_protocol_helper_SRCS})
300299
set_target_properties(unit_test_protocol_helper PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
301-
target_compile_definitions(unit_test_protocol_helper PRIVATE
302-
RENDERD_CONF="${PROJECT_SOURCE_DIR}/etc/renderd/renderd.conf.examples"
303-
)
304300
target_include_directories(unit_test_protocol_helper PRIVATE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/tests)
305301
target_link_libraries(unit_test_protocol_helper ${unit_test_protocol_helper_LIBS})
306302

307303

304+
#-----------------------------------------------------------------------------
305+
#
306+
# unit_test_render_submit_queue
307+
#
308+
#-----------------------------------------------------------------------------
309+
310+
set(unit_test_render_submit_queue_SRCS
311+
$<TARGET_OBJECTS:catch_main_o>
312+
${PROJECT_SOURCE_DIR}/tests/unit_test_render_submit_queue.cpp
313+
)
314+
set(unit_test_render_submit_queue_LIBS
315+
${GLIB_LIBRARIES}
316+
)
317+
add_executable(unit_test_render_submit_queue ${unit_test_render_submit_queue_SRCS})
318+
set_target_properties(unit_test_render_submit_queue PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
319+
target_include_directories(unit_test_render_submit_queue PRIVATE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/tests)
320+
target_link_libraries(unit_test_render_submit_queue ${unit_test_render_submit_queue_LIBS})
321+
322+
308323
#-----------------------------------------------------------------------------
309324
#
310325
# unit_test_renderd_config

src/cache_expire.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
* URL.
3333
* RFC for HTCP can be found at http://www.htcp.org/
3434
*/
35-
static void cache_expire_url(int sock, char * url)
35+
static void cache_expire_url(int sock, char *url)
3636
{
37-
char * buf;
37+
char *buf;
3838

3939
if (sock < 0) {
4040
return;
@@ -44,66 +44,66 @@ static void cache_expire_url(int sock, char * url)
4444
int url_len;
4545

4646
url_len = strlen(url);
47-
buf = (char *) malloc(12 + 22 + url_len);
47+
buf = (char *)malloc(12 + 22 + url_len);
4848

4949
if (!buf) {
5050
return;
5151
}
5252

5353
idx = 0;
5454

55-
//16 bit: Overall length of the datagram packet, including this header
55+
// 16 bit: Overall length of the datagram packet, including this header
5656
*((uint16_t *)(&buf[idx])) = htons(12 + 22 + url_len);
5757
idx += 2;
5858

59-
//HTCP version. Currently at 0.0
60-
buf[idx++] = 0; //Major version
61-
buf[idx++] = 0; //Minor version
59+
// HTCP version. Currently at 0.0
60+
buf[idx++] = 0; // Major version
61+
buf[idx++] = 0; // Minor version
6262

63-
//Length of HTCP data, including this field
63+
// Length of HTCP data, including this field
6464
*((uint16_t *)(&buf[idx])) = htons(8 + 22 + url_len);
6565
idx += 2;
6666

67-
//HTCP opcode CLR=4
67+
// HTCP opcode CLR=4
6868
buf[idx++] = 4;
69-
//Reserved
69+
// Reserved
7070
buf[idx++] = 0;
7171

72-
//32 bit transaction id;
72+
// 32 bit transaction id;
7373
*((uint32_t *)(&buf[idx])) = htonl(255);
7474
idx += 4;
7575

7676
buf[idx++] = 0;
77-
buf[idx++] = 0; //HTCP reason
77+
buf[idx++] = 0; // HTCP reason
7878

79-
//Length of the Method string
79+
// Length of the Method string
8080
*((uint16_t *)(&buf[idx])) = htons(4);
8181
idx += 2;
8282

83-
///Method string
83+
/// Method string
8484
memcpy(&buf[idx], "HEAD", 4);
8585
idx += 4;
8686

87-
//Length of the url string
87+
// Length of the url string
8888
*((uint16_t *)(&buf[idx])) = htons(url_len);
8989
idx += 2;
9090

91-
//Url string
91+
// Url string
9292
memcpy(&buf[idx], url, url_len);
9393
idx += url_len;
9494

95-
//Length of version string
95+
// Length of version string
9696
*((uint16_t *)(&buf[idx])) = htons(8);
9797
idx += 2;
9898

99-
//version string
99+
// version string
100100
memcpy(&buf[idx], "HTTP/1.1", 8);
101101
idx += 8;
102102

103-
//Length of request headers. Currently 0 as we don't have any headers to send
103+
// Length of request headers. Currently 0 as we don't have any headers to send
104104
*((uint16_t *)(&buf[idx])) = htons(0);
105105

106-
if (send(sock, (void *) buf, (12 + 22 + url_len), 0) < (12 + 22 + url_len)) {
106+
if (send(sock, (void *)buf, (12 + 22 + url_len), 0) < (12 + 22 + url_len)) {
107107
g_logger(G_LOG_LEVEL_ERROR, "Failed to send HTCP purge for %s", url);
108108
};
109109

@@ -117,13 +117,13 @@ void cache_expire(int sock, const char *host, const char *uri, int x, int y, int
117117
return;
118118
}
119119

120-
char * url = (char *)malloc(1024);
120+
char *url = (char *)malloc(1024);
121121
snprintf(url, 1024, "http://%s%s%i/%i/%i.png", host, uri, z, x, y);
122122
cache_expire_url(sock, url);
123123
free(url);
124124
}
125125

126-
int init_cache_expire(const char * htcphost)
126+
int init_cache_expire(const char *htcphost)
127127
{
128128
struct addrinfo hints;
129129
struct addrinfo *result, *rp;
@@ -132,7 +132,7 @@ int init_cache_expire(const char * htcphost)
132132
/* Obtain address(es) matching host/port */
133133

134134
memset(&hints, 0, sizeof(struct addrinfo));
135-
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
135+
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
136136
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
137137
hints.ai_flags = 0;
138138
hints.ai_protocol = 0; /* Any protocol */
@@ -157,7 +157,7 @@ int init_cache_expire(const char * htcphost)
157157
}
158158

159159
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
160-
break; /* Success */
160+
break; /* Success */
161161
}
162162

163163
close(sfd);
@@ -171,5 +171,4 @@ int init_cache_expire(const char * htcphost)
171171
freeaddrinfo(result); /* No longer needed */
172172

173173
return sfd;
174-
175174
}

src/metatile.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
#include "render_config.h"
2929
#include "store.h"
3030

31-
metaTile::metaTile(const std::string &xmlconfig, const std::string &options, int x, int y, int z):
32-
x_(x), y_(y), z_(z), xmlconfig_(xmlconfig), options_(options)
31+
metaTile::metaTile(const std::string &xmlconfig, const std::string &options, int x, int y, int z) : x_(x), y_(y), z_(z), xmlconfig_(xmlconfig), options_(options)
3332
{
3433
clear();
3534
}
@@ -59,13 +58,13 @@ int metaTile::xyz_to_meta_offset(int x, int y, int z)
5958
return (x & mask) * METATILE + (y & mask);
6059
}
6160

62-
void metaTile::save(struct storage_backend * store)
61+
void metaTile::save(struct storage_backend *store)
6362
{
6463
int ox, oy, limit;
6564
ssize_t offset;
6665
struct meta_layout m;
6766
struct entry offsets[METATILE * METATILE];
68-
char * metatilebuffer;
67+
char *metatilebuffer;
6968
char *tmp;
7069

7170
memset(&m, 0, sizeof(m));
@@ -86,12 +85,12 @@ void metaTile::save(struct storage_backend * store)
8685
for (oy = 0; oy < limit; oy++) {
8786
int mt = xyz_to_meta_offset(x_ + ox, y_ + oy, z_);
8887
offsets[mt].offset = offset;
89-
offsets[mt].size = tile[ox][oy].size();
88+
offsets[mt].size = tile[ox][oy].size();
9089
offset += offsets[mt].size;
9190
}
9291
}
9392

94-
metatilebuffer = (char *) malloc(offset);
93+
metatilebuffer = (char *)malloc(offset);
9594

9695
if (metatilebuffer == 0) {
9796
g_logger(G_LOG_LEVEL_WARNING, "Failed to write metatile. Out of memory");
@@ -118,7 +117,6 @@ void metaTile::save(struct storage_backend * store)
118117
free(metatilebuffer);
119118
}
120119

121-
122120
void metaTile::expire_tiles(int sock, const char *host, const char *uri)
123121
{
124122
if (sock < 0) {

src/render_submit_queue.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static struct protocol *fetch(void)
174174
pthread_cond_signal(&qCondNotFull);
175175
pthread_mutex_unlock(&qLock);
176176

177-
struct protocol *cmd = malloc(sizeof(struct protocol));
177+
struct protocol *cmd = (struct protocol *)malloc(sizeof(struct protocol));
178178

179179
cmd->ver = 2;
180180
cmd->cmd = cmdRenderBulk;
@@ -192,7 +192,7 @@ static struct protocol *fetch(void)
192192
void enqueue(const char *xmlname, int x, int y, int z)
193193
{
194194
// Add this path in the local render queue
195-
struct qItem *e = malloc(sizeof(struct qItem));
195+
struct qItem *e = (struct qItem *)malloc(sizeof(struct qItem));
196196

197197
e->mapname = strdup(xmlname);
198198
e->x = x;
@@ -265,7 +265,7 @@ int make_connection(const char *spath)
265265
if (!hostname_len) {
266266
hostname = strdup(RENDERD_HOST);
267267
} else {
268-
hostname = malloc(hostname_len + sizeof('\0'));
268+
hostname = (char *)malloc(hostname_len + sizeof('\0'));
269269
assert(hostname != NULL);
270270
strncpy(hostname, spath, hostname_len);
271271
}
@@ -408,7 +408,7 @@ void spawn_workers(int num, const char *spath, int max_load)
408408
qMaxLen = no_workers;
409409

410410
g_logger(G_LOG_LEVEL_MESSAGE, "Starting %d rendering threads", no_workers);
411-
workers = calloc(sizeof(pthread_t), no_workers);
411+
workers = (pthread_t *)calloc(sizeof(pthread_t), no_workers);
412412

413413
if (!workers) {
414414
g_logger(G_LOG_LEVEL_CRITICAL, "Error allocating worker memory: %s", strerror(errno));

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ add_test(
240240
COMMAND unit_test_protocol_helper
241241
)
242242

243+
add_test(
244+
NAME unit_test_render_submit_queue
245+
COMMAND unit_test_render_submit_queue
246+
)
247+
243248
add_test(
244249
NAME unit_test_renderd_config
245250
COMMAND unit_test_renderd_config
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <chrono>
2+
#include <fcntl.h>
3+
#include <setjmp.h>
4+
#include <stdlib.h>
5+
#include <string>
6+
#include <unistd.h>
7+
8+
#include "catch/catch.hpp"
9+
#include "catch_test_common.hpp"
10+
#include "render_submit_queue.h"
11+
#include "sys_utils.h"
12+
13+
extern int exit_status;
14+
extern bool fail_next_getloadavg;
15+
extern bool fail_next_socket;
16+
extern jmp_buf exit_jump;
17+
extern std::string err_log_lines;
18+
19+
extern "C" {
20+
#define exit mocked_exit
21+
#define g_logger mocked_g_logger
22+
#define getloadavg mocked_getloadavg
23+
#define socket mocked_socket
24+
25+
#include "protocol_helper.c"
26+
#include "render_submit_queue.c"
27+
#include "sys_utils.c"
28+
29+
#undef exit
30+
#undef g_logger
31+
#undef getloadavg
32+
#undef socket
33+
}
34+
35+
TEST_CASE("check_load function", "[check_load]")
36+
{
37+
err_log_lines.clear();
38+
39+
maxLoad = 999;
40+
auto start = std::chrono::high_resolution_clock::now();
41+
42+
SECTION("check_load with max load of 999", "should return") {
43+
check_load();
44+
}
45+
46+
SECTION("check_load with max load of 999 and unobtainable load average (which returns 1000)", "should return after sleeping 5 seconds") {
47+
fail_next_getloadavg = true;
48+
check_load();
49+
REQUIRE(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - start).count() >= 5);
50+
}
51+
}
52+
53+
TEST_CASE("process function", "[process]")
54+
{
55+
int fd, ret;
56+
int pipefd[2];
57+
pipe(pipefd);
58+
struct protocol *cmd = (struct protocol *)malloc(sizeof(struct protocol));
59+
60+
err_log_lines.clear();
61+
62+
fd = pipefd[0];
63+
64+
SECTION("process", "should return positive") {
65+
ret = process(cmd, fd);
66+
67+
REQUIRE(ret == 0);
68+
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("Sending request"));
69+
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("send error: Success"));
70+
}
71+
}
72+
73+
TEST_CASE("make_connection function", "[make_connection]")
74+
{
75+
int ret;
76+
std::string socket_path = std::string(P_tmpdir) + "/renderd.sock";
77+
78+
err_log_lines.clear();
79+
exit_status = 0;
80+
81+
SECTION("make_connection", "should return positive") {
82+
ret = make_connection(socket_path.c_str());
83+
84+
// REQUIRE(ret > 0);
85+
}
86+
87+
SECTION("make_connection handles socket failure by exiting", "should exit 2") {
88+
fail_next_socket = true;
89+
90+
if (setjmp(exit_jump) == 0) {
91+
make_connection(socket_path.c_str());
92+
FAIL("make_connection should have called exit() but didn't");
93+
} else {
94+
SUCCEED("Captured expected exit() call due to socket failure");
95+
}
96+
97+
REQUIRE(exit_status == 2);
98+
REQUIRE_THAT(err_log_lines, Catch::Matchers::Contains("failed to create unix socket"));
99+
}
100+
}

tests/unit_test_sys_utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
extern bool fail_next_getloadavg;
1111
extern std::string err_log_lines;
1212

13+
extern "C" {
1314
#define g_logger mocked_g_logger
1415
#define getloadavg mocked_getloadavg
1516

1617
#include "sys_utils.c"
1718

1819
#undef g_logger
1920
#undef getloadavg
21+
}
2022

2123
TEST_CASE("get_load_avg function", "[get_load_avg]")
2224
{

0 commit comments

Comments
 (0)