From e8e4aa6d8e79cd85cde002dc2a8fc850c3ed89fb Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Wed, 5 Oct 2016 18:07:08 -0700 Subject: [PATCH] add valgrind check --- .travis.yml | 13 +++++++++++++ photon_scheduler.c | 33 ++++++++++++++++++++++++++++++--- test/test.py | 28 +++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 45ef9a286..caab2e356 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,19 @@ matrix: install: [] script: - .travis/check-git-clang-format-output.sh + - os: linux + dist: trusty + python: "2.7" + env: VALGRIND=1 + before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq valgrind + script: + - cd common + - make test + - cd .. + - source setup-env.sh + - python test/test.py valgrind install: - make diff --git a/photon_scheduler.c b/photon_scheduler.c index 44022451c..b876bc9f2 100644 --- a/photon_scheduler.c +++ b/photon_scheduler.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -24,6 +25,9 @@ UT_icd task_ptr_icd = {sizeof(task_instance *), NULL, NULL, NULL}; UT_icd worker_icd = {sizeof(available_worker), NULL, NULL, NULL}; struct local_scheduler_state { + /* The local scheduler event loop. */ + event_loop *loop; + /* The handle to the database. */ db_handle *db; /** This is an array of pointers to tasks that are waiting to be scheduled. */ UT_array *task_queue; @@ -35,6 +39,7 @@ struct local_scheduler_state { local_scheduler_state * init_local_scheduler(event_loop *loop, const char *redis_addr, int redis_port) { local_scheduler_state *state = malloc(sizeof(local_scheduler_state)); + state->loop = loop; state->db = db_connect(redis_addr, redis_port, "photon", "", -1); db_attach(state->db, loop); utarray_new(state->task_queue, &task_ptr_icd); @@ -42,6 +47,14 @@ init_local_scheduler(event_loop *loop, const char *redis_addr, int redis_port) { return state; }; +void free_local_scheduler(local_scheduler_state *s) { + db_disconnect(s->db); + utarray_free(s->task_queue); + utarray_free(s->available_worker_queue); + event_loop_destroy(s->loop); + free(s); +} + void handle_submit_task(local_scheduler_state *s, task_spec *task) { /* Create a unique task instance ID. This is different from the task ID and * is used to distinguish between potentially multiple executions of the @@ -146,19 +159,33 @@ void new_client_connection(event_loop *loop, int listener_sock, void *context, LOG_INFO("new connection with fd %d", new_socket); } +/* We need this code so we can clean up when we get a SIGTERM signal. */ + +local_scheduler_state *g_state; + +void signal_handler(int signal) { + if (signal == SIGTERM) { + free_local_scheduler(g_state); + exit(0); + } +} + +/* End of the cleanup code. */ + void start_server(const char *socket_name, const char *redis_addr, int redis_port) { int fd = bind_ipc_sock(socket_name); event_loop *loop = event_loop_create(); - local_scheduler_state *state = - init_local_scheduler(loop, redis_addr, redis_port); + g_state = init_local_scheduler(loop, redis_addr, redis_port); /* Run event loop. */ - event_loop_add_file(loop, fd, EVENT_LOOP_READ, new_client_connection, state); + event_loop_add_file(loop, fd, EVENT_LOOP_READ, new_client_connection, + g_state); event_loop_run(loop); } int main(int argc, char *argv[]) { + signal(SIGTERM, signal_handler); /* Path of the listening socket of the local scheduler. */ char *scheduler_socket_name = NULL; /* IP address and port of redis. */ diff --git a/test/test.py b/test/test.py index 0517c40a5..bbd164c37 100644 --- a/test/test.py +++ b/test/test.py @@ -1,6 +1,7 @@ from __future__ import print_function import os +import signal import subprocess import sys import unittest @@ -9,6 +10,8 @@ import time import photon +USE_VALGRIND = False + class TestPhotonClient(unittest.TestCase): def setUp(self): @@ -18,8 +21,15 @@ class TestPhotonClient(unittest.TestCase): time.sleep(0.1) scheduler_executable = os.path.join(os.path.abspath(os.path.dirname(__file__)), "../build/photon_scheduler") scheduler_name = "/tmp/scheduler{}".format(random.randint(0, 10000)) - self.p2 = subprocess.Popen([scheduler_executable, "-s", scheduler_name, "-r", "127.0.0.1:6379"]) - time.sleep(0.1) + command = [scheduler_executable, "-s", scheduler_name, "-r", "127.0.0.1:6379"] + if USE_VALGRIND: + self.p2 = subprocess.Popen(["valgrind", "--track-origins=yes", "--leak-check=full", "--show-leak-kinds=all"] + command) + else: + self.p2 = subprocess.Popen(command) + if USE_VALGRIND: + time.sleep(1.0) + else: + time.sleep(0.1) # Connect to the scheduler. self.photon_client = photon.PhotonClient(scheduler_name) @@ -27,7 +37,13 @@ class TestPhotonClient(unittest.TestCase): # Kill the Redis server. self.p1.kill() # Kill the local scheduler. - self.p2.kill() + if USE_VALGRIND: + self.p2.send_signal(signal.SIGTERM) + self.p2.wait() + os._exit(self.p2.returncode) + else: + self.p2.kill() + def test_create(self): l = [20 * "a", 20 * "b", 20 * "c"] @@ -38,4 +54,10 @@ class TestPhotonClient(unittest.TestCase): task = self.photon_client.get_task() if __name__ == "__main__": + if len(sys.argv) > 1: + # pop the argument so we don't mess with unittest's own argument parser + arg = sys.argv.pop() + if arg == "valgrind": + USE_VALGRIND = True + print("Using valgrind for tests") unittest.main(verbosity=2)