From 71768c6233d8249425e2a3ae34993be84f245e0a Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 2 Nov 2019 10:41:52 -0400 Subject: nserver: protocol: Define sssample. * nserver/src/protocol.c (sssample): New function. * nserver/tests/protocol_tests.c (test_sssample): New test. (all_tests): Add test_sssample. --- nserver/src/protocol.c | 21 +++++++++++++++++++++ nserver/tests/protocol_tests.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) (limited to 'nserver') diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c index 6963116..a174caf 100644 --- a/nserver/src/protocol.c +++ b/nserver/src/protocol.c @@ -36,3 +36,24 @@ int sscreate(char *key) error: return -1; } + +int sssample(char *key, double s) +{ + check(hash != NULL, "hash not initialized"); + + // 1. create bstring from 'key'. + bstring k = bfromcstr(key); + check(k != NULL, "key creation failed"); + + // 2. try to get Stats for key. + Stats *st = (Stats *) Hashmap_get(hash, k); + check(st != NULL, "stats not found for key"); + + // 3. sample! + Stats_sample(st, s); + + return 0; + error: + return -1; +} + diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c index a1880d2..75bf614 100644 --- a/nserver/tests/protocol_tests.c +++ b/nserver/tests/protocol_tests.c @@ -14,11 +14,40 @@ char *test_sscreate() return NULL; } +char *test_sssample() +{ + int rc = 0; + + rc = sssample("crimson", 3); + mu_assert(rc == 0, "sssample failed 0"); + + rc = sssample("crimson", 9); + mu_assert(rc == 0, "sssample failed 1"); + + rc = sssample("crimson", 12); + mu_assert(rc == 0, "sssample failed 2"); + + rc = sssample("/vermilion", 20); + mu_assert(rc == 0, "sssample failed 3"); + + rc = sssample("/vermilion", 27); + mu_assert(rc == 0, "sssample failed 4"); + + rc = sssample("/vermilion", 4); + mu_assert(rc == 0, "sssample failed 5"); + + rc = sssample("/ruby", 48); + mu_assert(rc == -1, "sssample failed 6"); + + return NULL; +} + char *all_tests() { mu_suite_start(); mu_run_test(test_sscreate); + mu_run_test(test_sssample); return NULL; } -- cgit v1.2.3