Skip to content

Commit

Permalink
add munit and basic unit test example
Browse files Browse the repository at this point in the history
  • Loading branch information
carns committed Jul 6, 2021
1 parent 96e4917 commit b8cdfce
Show file tree
Hide file tree
Showing 8 changed files with 2,817 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ include Make.rules
include $(top_srcdir)/src/Makefile.subdir
include $(top_srcdir)/src/swim-fd/Makefile.subdir
include $(top_srcdir)/tests/Makefile.subdir
include $(top_srcdir)/tests/unit-tests/Makefile.subdir
10 changes: 10 additions & 0 deletions tests/unit-tests/Makefile.subdir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
check_PROGRAMS += \
tests/unit-tests/ssg-init

TESTS += \
tests/unit-tests/ssg-init

tests_unit_tests_ssg_init_SOURCES = \
tests/unit-tests/munit/munit.c \
tests/unit-tests/ssg-init.c \
tests/unit-tests/helper-server.c
70 changes: 70 additions & 0 deletions tests/unit-tests/helper-server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* (C) 2020 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#include "helper-server.h"
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

int HS_start(const char* server_addr,
struct margo_init_info* margo_args,
HS_function_t init_server_fn,
HS_function_t run_server_fn,
void* uargs, char* addr,
hg_size_t* addr_size)
{
int p[2], pid = 0;
if(pipe(p) < 0)
exit(-1);

if((pid = fork()) == 0) {
// child process (server)
close(p[0]);
margo_instance_id mid = margo_init_ext(
server_addr, MARGO_SERVER_MODE, margo_args);
margo_enable_remote_shutdown(mid);
if(init_server_fn) {
init_server_fn(mid, uargs);
}
if(addr) {
hg_addr_t self_addr = HG_ADDR_NULL;
margo_addr_self(mid, &self_addr);
margo_addr_to_string(mid, addr, addr_size, self_addr);
margo_addr_free(mid, self_addr);
write(p[1], addr_size, sizeof(*addr_size));
write(p[1], addr, *addr_size);
}
close(p[1]);
int ret = 0;
if(run_server_fn) {
ret = run_server_fn(mid, uargs);
} else {
margo_wait_for_finalize(mid);
}
exit(ret);
} else {
// parent process (unit test)
close(p[1]);
if(addr) {
memset(addr, 0, *addr_size);
read(p[0], addr_size, sizeof(*addr_size));
read(p[0], addr, *addr_size);
}
close(p[0]);
}
return pid;
}

int HS_stop(int pid, int k)
{
int kret;
if(k) {
kret = kill(pid, SIGKILL);
}
int status;
waitpid(pid, &status, 0);
return k ? kret : WIFEXITED(status);
}
63 changes: 63 additions & 0 deletions tests/unit-tests/helper-server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* (C) 2020 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __HELPER_SERVER
#define __HELPER_SERVER

#include <stdio.h>
#include <margo.h>

/**
* @brief Type of a server function.
*/
typedef int (*HS_function_t)(margo_instance_id, void*);

/**
* @brief Fork the calling process to start the server function.
* This function will not return on the child process.
*
* IMPORTANT: it is the responsibility of the server_fn to call
* margo_finalize or margo_wait_for_finalize on the provided
* margo instance.
*
* Passing NULL as server_fn will simply create a margo instance
* and call margo_wait_for_finalize on it, which can be useful if
* the server just needs to have a margo instance running.
*
* @param[in] server_addr Server address or procotol (e.g. "na+sm")
* @param[in] margo_args Arguments for the server's margo instance
* @param[in] server_fn Server function
* @param[in] uargs User arguments for the server function
* @param[out] addr Address of the server (relevant on parent only)
* @param[inout] addr_size Size of the server's address (idem)
*
* @return PID of the server
*/
int HS_start(const char* server_addr,
struct margo_init_info* margo_args,
HS_function_t init_server_fn,
HS_function_t run_server_fn,
void* uargs, char* addr,
hg_size_t* addr_size);

/**
* @brief Stops the server. If kill == 0, this function
* will simply wait for the server to terminate, and return
* the exit value of the the server. If kill == 1 this funcion
* will send a SIGKILL to the server and return the return value
* of the kill function.
*
* Note that if kill == 0, it is the responsibility of the caller
* to put mechanisms in place to signal the server that it should
* terminate (e.g. sending a margo_shutdown_remote_instance).
*
* @param pid PID of the server
* @param k whether to kill the server or not.
*
* @return The exit value of the server process, or of the kill function.
*/
int HS_stop(int pid, int k);

#endif
27 changes: 27 additions & 0 deletions tests/unit-tests/munit/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
The content of this directory originates from https://github.com/nemequ/munit/
and is licensed under an MIT license separately from the rest of this repority.
The original license is copied hereafter.

------------------------------------------------------------------------------

µnit Testing Framework
Copyright (c) 2013-2016 Evan Nemerson <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit b8cdfce

Please sign in to comment.