Skip to content

Commit 2068587

Browse files
pcmoritzrobertnishihara
authored andcommitted
Fix socket bind collisions in manager_tests, race condition bind/listen/subscribe and memory leaks (#18)
* Fix socket bind collisions in manager_tests * bind manager sockets before connecting to the store * fix memory leak in tests * fix valgrind early process termination * fix bind/listen/subscribe race condition * fix photon * fix other tests * make it that all of common is tested * fix clang-format * fix
1 parent d56c1a0 commit 2068587

File tree

16 files changed

+147
-73
lines changed

16 files changed

+147
-73
lines changed

src/common/Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $(BUILD)/io_tests: test/io_tests.c $(BUILD)/libcommon.a
2323
$(CC) -o $@ $^ $(CFLAGS)
2424

2525
$(BUILD)/task_tests: test/task_tests.c $(BUILD)/libcommon.a
26-
$(CC) -o $@ $^ $(CFLAGS)
26+
$(CC) -o $@ $^ thirdparty/hiredis/libhiredis.a $(CFLAGS)
2727

2828
$(BUILD)/redis_tests: hiredis test/redis_tests.c $(BUILD)/libcommon.a logging.h
2929
$(CC) -o $@ test/redis_tests.c logging.c $(BUILD)/libcommon.a thirdparty/hiredis/libhiredis.a $(CFLAGS)
@@ -40,7 +40,14 @@ hiredis:
4040

4141
test: hiredis redis $(BUILD)/common_tests $(BUILD)/task_log_tests $(BUILD)/object_table_tests $(BUILD)/db_tests $(BUILD)/io_tests $(BUILD)/task_tests $(BUILD)/redis_tests FORCE
4242
./thirdparty/redis-3.2.3/src/redis-server &
43-
sleep 1s ; ./build/common_tests ; ./build/db_tests ; ./build/task_log_tests ; ./build/object_table_tests ; ./build/io_tests ; ./build/task_tests ; ./build/redis_tests
43+
sleep 1s
44+
./build/common_tests
45+
./build/db_tests
46+
./build/io_tests
47+
./build/task_tests
48+
./build/redis_tests
49+
./build/task_log_tests
50+
./build/object_table_tests
4451

4552
valgrind: test
4653
valgrind --leak-check=full --error-exitcode=1 ./build/common_tests

src/common/common.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@
4444

4545
#define UNIQUE_ID_SIZE 20
4646

47-
/* Cleanup method for running tests with the greatest library.
48-
* Runs the test, then clears the Redis database. */
49-
#define RUN_REDIS_TEST(context, test) \
50-
RUN_TEST(test); \
51-
freeReplyObject(redisCommand(context, "FLUSHALL"));
52-
5347
typedef struct { unsigned char id[UNIQUE_ID_SIZE]; } unique_id;
5448

5549
extern const unique_id NIL_ID;

src/common/io.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
* write_message and read_message methods.
2626
*
2727
* @param port The port to bind to.
28+
* @param shall_listen Are we also starting to listen on the socket?
2829
* @return A non-blocking file descriptor for the socket, or -1 if an error
2930
* occurs.
3031
*/
31-
int bind_inet_sock(const int port) {
32+
int bind_inet_sock(const int port, bool shall_listen) {
3233
struct sockaddr_in name;
3334
int socket_fd = socket(PF_INET, SOCK_STREAM, 0);
3435
if (socket_fd < 0) {
@@ -55,7 +56,7 @@ int bind_inet_sock(const int port) {
5556
close(socket_fd);
5657
return -1;
5758
}
58-
if (listen(socket_fd, 5) == -1) {
59+
if (shall_listen && listen(socket_fd, 5) == -1) {
5960
LOG_ERR("Could not listen to socket %d", port);
6061
close(socket_fd);
6162
return -1;
@@ -68,10 +69,11 @@ int bind_inet_sock(const int port) {
6869
* pathname. Removes any existing file at the pathname.
6970
*
7071
* @param socket_pathname The pathname for the socket.
72+
* @param shall_listen Are we also starting to listen on the socket?
7173
* @return A blocking file descriptor for the socket, or -1 if an error
7274
* occurs.
7375
*/
74-
int bind_ipc_sock(const char *socket_pathname) {
76+
int bind_ipc_sock(const char *socket_pathname, bool shall_listen) {
7577
struct sockaddr_un socket_address;
7678
int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
7779
if (socket_fd < 0) {
@@ -104,7 +106,7 @@ int bind_ipc_sock(const char *socket_pathname) {
104106
close(socket_fd);
105107
return -1;
106108
}
107-
if (listen(socket_fd, 5) == -1) {
109+
if (shall_listen && listen(socket_fd, 5) == -1) {
108110
LOG_ERR("Could not listen to socket %s", socket_pathname);
109111
close(socket_fd);
110112
return -1;

src/common/io.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef IO_H
22
#define IO_H
33

4+
#include <stdbool.h>
45
#include <stdint.h>
56

67
enum common_message_type {
@@ -14,8 +15,8 @@ enum common_message_type {
1415

1516
/* Helper functions for socket communication. */
1617

17-
int bind_inet_sock(const int port);
18-
int bind_ipc_sock(const char *socket_pathname);
18+
int bind_inet_sock(const int port, bool shall_listen);
19+
int bind_ipc_sock(const char *socket_pathname, bool shall_listen);
1920
int connect_ipc_sock(const char *socket_pathname);
2021

2122
int accept_client(int socket_fd);
@@ -29,4 +30,4 @@ void write_log_message(int fd, char *message);
2930
void write_formatted_log_message(int fd, const char *format, ...);
3031
char *read_log_message(int fd);
3132

32-
#endif
33+
#endif /* IO_H */

src/common/test/db_tests.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <sys/wait.h>
66

77
#include "event_loop.h"
8-
#include "test/example_task.h"
8+
#include "test_common.h"
99
#include "state/db.h"
1010
#include "state/object_table.h"
1111
#include "state/task_log.h"
@@ -197,13 +197,10 @@ TEST unique_client_id_test(void) {
197197
}
198198

199199
SUITE(db_tests) {
200-
redisContext *context = redisConnect("127.0.0.1", 6379);
201-
freeReplyObject(redisCommand(context, "FLUSHALL"));
202-
RUN_REDIS_TEST(context, object_table_lookup_test);
203-
RUN_REDIS_TEST(context, task_log_test);
204-
RUN_REDIS_TEST(context, task_log_all_test);
205-
RUN_REDIS_TEST(context, unique_client_id_test);
206-
redisFree(context);
200+
RUN_REDIS_TEST(object_table_lookup_test);
201+
RUN_REDIS_TEST(task_log_test);
202+
RUN_REDIS_TEST(task_log_all_test);
203+
RUN_REDIS_TEST(unique_client_id_test);
207204
}
208205

209206
GREATEST_MAIN_DEFS();

src/common/test/io_tests.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SUITE(io_tests);
1111

1212
TEST ipc_socket_test(void) {
1313
const char *socket_pathname = "test-socket";
14-
int socket_fd = bind_ipc_sock(socket_pathname);
14+
int socket_fd = bind_ipc_sock(socket_pathname, true);
1515
ASSERT(socket_fd >= 0);
1616

1717
char *test_string = "hello world";
@@ -50,7 +50,7 @@ TEST ipc_socket_test(void) {
5050

5151
TEST long_ipc_socket_test(void) {
5252
const char *socket_pathname = "long-test-socket";
53-
int socket_fd = bind_ipc_sock(socket_pathname);
53+
int socket_fd = bind_ipc_sock(socket_pathname, true);
5454
ASSERT(socket_fd >= 0);
5555

5656
UT_string *test_string;

src/common/test/object_table_tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "greatest.h"
22

33
#include "event_loop.h"
4-
#include "example_task.h"
4+
#include "test_common.h"
55
#include "common.h"
66
#include "state/object_table.h"
77
#include "state/redis.h"

src/common/test/redis_tests.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "state/redis.h"
1111
#include "io.h"
1212
#include "logging.h"
13+
#include "test_common.h"
1314

1415
SUITE(redis_tests);
1516

@@ -40,7 +41,7 @@ TEST redis_socket_test(void) {
4041
const char *socket_pathname = "redis-test-socket";
4142
redisContext *context = redisConnect("127.0.0.1", 6379);
4243
ASSERT(context != NULL);
43-
int socket_fd = bind_ipc_sock(socket_pathname);
44+
int socket_fd = bind_ipc_sock(socket_pathname, true);
4445
ASSERT(socket_fd >= 0);
4546

4647
int client_fd = connect_ipc_sock(socket_pathname);
@@ -97,7 +98,7 @@ TEST async_redis_socket_test(void) {
9798

9899
/* Start IPC channel. */
99100
const char *socket_pathname = "async-redis-test-socket";
100-
int socket_fd = bind_ipc_sock(socket_pathname);
101+
int socket_fd = bind_ipc_sock(socket_pathname, true);
101102
ASSERT(socket_fd >= 0);
102103
utarray_push_back(connections, &socket_fd);
103104

@@ -171,7 +172,7 @@ TEST logging_test(void) {
171172

172173
/* Start IPC channel. */
173174
const char *socket_pathname = "logging-test-socket";
174-
int socket_fd = bind_ipc_sock(socket_pathname);
175+
int socket_fd = bind_ipc_sock(socket_pathname, true);
175176
ASSERT(socket_fd >= 0);
176177
utarray_push_back(connections, &socket_fd);
177178

@@ -208,12 +209,9 @@ TEST logging_test(void) {
208209
}
209210

210211
SUITE(redis_tests) {
211-
redisContext *context = redisConnect("127.0.0.1", 6379);
212-
freeReplyObject(redisCommand(context, "FLUSHALL"));
213-
RUN_REDIS_TEST(context, redis_socket_test);
214-
RUN_REDIS_TEST(context, async_redis_socket_test);
215-
RUN_REDIS_TEST(context, logging_test);
216-
redisFree(context);
212+
RUN_REDIS_TEST(redis_socket_test);
213+
RUN_REDIS_TEST(async_redis_socket_test);
214+
RUN_REDIS_TEST(logging_test);
217215
}
218216

219217
GREATEST_MAIN_DEFS();

src/common/test/task_log_tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "greatest.h"
22

33
#include "event_loop.h"
4-
#include "example_task.h"
4+
#include "test_common.h"
55
#include "common.h"
66
#include "state/object_table.h"
77
#include "state/redis.h"

src/common/test/task_tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <sys/socket.h>
66

77
#include "common.h"
8-
#include "test/example_task.h"
8+
#include "test_common.h"
99
#include "task.h"
1010
#include "io.h"
1111

0 commit comments

Comments
 (0)