Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test sortdb with valgrind

  • Loading branch information...
commit 3049e0ca1a9df2726a708a094ba0809be5d4ecdd 1 parent 0a0f984
@jehiah jehiah authored
View
21 sortdb/sortdb.c
@@ -26,6 +26,8 @@ int fd = 0;
void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
void get_cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx);
+void reload_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
+void exit_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
char *prev_line(char *pos);
char *map_search(char *key, size_t keylen, char *lower, char *upper, int *seeks);
void usage();
@@ -120,6 +122,23 @@ void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) {
evhttp_send_reply(req, HTTP_OK, "OK", evb);
}
+void reload_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) {
+ fprintf(stdout, "/reload request recieved\n");
+ close_dbfile();
+ open_dbfile();
+ if (map_base == NULL) {
+ fprintf(stderr, "no mmaped file; exiting\n");
+ exit(1);
+ }
+ evbuffer_add_printf(evb, "db reloaded\n");
+ evhttp_send_reply(req, HTTP_OK, "OK", evb);
+}
+
+void exit_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) {
+ fprintf(stdout, "/exit request recieved\n");
+ exit(0);
+}
+
void info() {
fprintf(stdout, "sortdb: Sorted database server.\n");
fprintf(stdout, "Version %s, https://github.com/bitly/simplehttp/tree/master/sortdb\n", version);
@@ -213,6 +232,8 @@ int main(int argc, char **argv) {
signal(SIGHUP, hup_handler);
simplehttp_set_cb("/get?*", get_cb, NULL);
simplehttp_set_cb("/stats", stats_cb, NULL);
+ simplehttp_set_cb("/reload", reload_cb, NULL);
+ simplehttp_set_cb("/exit", exit_cb, NULL);
simplehttp_main(argc, argv);
return 0;
}
View
1  sortdb/test.expected
@@ -14,6 +14,7 @@ almost-sleepy
very-sleepy
/get?key=zzzzzzzzzzzzzzzzzzzzzzzzzz
already-asleep
+db reloaded
/get?key=a (should be a new key 'new db')
new db
/get?key=b not found
View
43 sortdb/test.sh
@@ -1,13 +1,31 @@
#!/bin/sh
+SCRIPT=$(readlink -f "$0")
+SCRIPTPATH=`dirname "$SCRIPT"`
testsubdir=test_output
rm -rf "$testsubdir" > /dev/null 2>&1
mkdir -p "$testsubdir"
CMP="${CMP-cmp}"
+run_vg (){
+ TEST_COMMAND="$1"
+ TEST_OPTIONS="$2"
+ REDIR_OUTPUT="2>/dev/null 1>/dev/null"
+ # REDIR_OUTPUT=""
+ eval valgrind --tool=memcheck \
+ --trace-children=yes \
+ --demangle=yes \
+ --log-file-exactly="${testsubdir}/vg.out" \
+ --leak-check=full \
+ --show-reachable=yes \
+ --run-libc-freeres=yes \
+ "\"${SCRIPTPATH}/${TEST_COMMAND}\"" $TEST_OPTIONS ${REDIR_OUTPUT} &
+}
+err=$?
+
ln -s -f test.tab test.db
-./sortdb -f test.db -a 127.0.0.1 -p 8080 2>/dev/null 1>/dev/null &
+run_vg sortdb "-f test.db -a 127.0.0.1 -p 8080"
sleep 1
for key in a b c m o zzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzz; do
echo "/get?key=$key" >> $testsubdir/test.out
@@ -16,7 +34,7 @@ done
# now swap the db and check keys again
ln -s -f test2.tab test.db
-kill -s HUP %1
+curl --silent "localhost:8080/reload" >> $testsubdir/test.out
echo "/get?key=a (should be a new key 'new db')" >> $testsubdir/test.out
curl --silent "localhost:8080/get/?key=a" >> $testsubdir/test.out
@@ -29,8 +47,25 @@ if ! "$CMP" -s "test.expected" "${testsubdir}/test.out" ; then
diff "test.expected" "${testsubdir}/test.out" 1>&2
err=1
else
- echo "TEST PASSED"
+ echo "FUNCTIONAL TEST PASSED"
+fi
+
+curl --silent "localhost:8080/exit"
+
+if ! grep -q "ERROR SUMMARY: 0 errors" "${testsubdir}/vg.out" ; then
+ echo "ERROR: valgrind found errors during execution:" 1>&2
+ cat "${testsubdir}/vg.out"
+ err=1
+fi
+if ! grep -q "definitely lost: 0 bytes in 0 blocks." "${testsubdir}/vg.out" ; then
+ echo "ERROR: valgrind found leaks during execution:" 1>&2
+ cat "${testsubdir}/vg.out"
+ err=1
+fi
+if ! grep -q "possibly lost: 0 bytes in 0 blocks." "${testsubdir}/vg.out" ; then
+ echo "ERROR: valgrind found leaks during execution:" 1>&2
+ cat "${testsubdir}/vg.out"
+ err=1
fi
-kill %1
exit $err;
View
1  sortdb/test.tab
@@ -1,6 +1,5 @@
a first record
aa another first
-b second
b third
c d
e f
Please sign in to comment.
Something went wrong with that request. Please try again.