Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow tests to run memcached's main() with custom args and enabled fo…

…rking
  • Loading branch information...
commit 68a08f0eb5797ebe030c4bae610f5f4e7ed8d1e6 1 parent c44abc9
@alk alk authored
View
8 check_moxi.c
@@ -8,8 +8,6 @@
#include "memcached.h"
#include "cproxy.h"
-int main_check(int argc, char **argv);
-
#define s_len(str) (str), strlen(str)
START_TEST(test_skey)
@@ -459,11 +457,7 @@ static Suite* moxi_suite(void)
return s;
}
-/**
- * Run this like...
- * check_moxi -vvv -p 11211
- */
-int main_check(int argc, char **argv)
+int main(int argc, char **argv)
{
int number_failed;
Suite *s = moxi_suite();
View
71 check_moxi_agent.c
@@ -11,15 +11,12 @@
#include "cproxy.h"
#include "agent.h"
-static pthread_t check_thread_tid;
static proxy_main *pmain = NULL;
extern proxy_behavior behavior_default_g;
static char *empty[] = { NULL };
-int main_check(int argc, char **argv);
-
static kvpair_t *mk_kvpairs(char *spec[]) {
kvpair_t *last = NULL;
kvpair_t *head = NULL;
@@ -219,68 +216,48 @@ START_TEST(test_easy_reconfig)
}
END_TEST
+static
+void setup(void)
+{
+ start_main("moxi", NULL);
+ assert(!is_listen_thread());
+ proxy_behavior pbg = behavior_default_g;
+
+ unlink("t/check_moxi_agent.cfg");
+ pmain = cproxy_init_agent_start("check_moxi_agent@localhost", // Fake JID.
+ "password", // Fake password.
+ "t/check_moxi_agent.cfg",
+ NULL,
+ pbg,
+ settings.num_threads);
+ assert(pmain);
+}
+
static Suite* moxi_agent_suite(void)
{
Suite *s = suite_create("moxi_agent");
/* Core test case */
TCase *tc_core = tcase_create("core");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
tcase_add_test(tc_core, test_first_config);
tcase_add_test(tc_core, test_easy_reconfig);
suite_add_tcase(s, tc_core);
+ if (getenv("CK_DEFAULT_TIMEOUT") == NULL)
+ tcase_set_timeout(tc_core, 20);
+
return s;
}
-// We have a separate thread here to pretend to be the libconflate thread.
-//
-static void *check_thread(void *arg) {
- sleep(1);
-
- assert(!is_listen_thread());
-
+int main(int argc, char **argv)
+{
int number_failed;
Suite *s = moxi_agent_suite();
SRunner *sr = srunner_create(s);
- srunner_set_fork_status(sr, CK_NOFORK);
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
- int rv = (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
- exit(rv);
-
- return NULL;
-}
-
-/**
- * Run this like...
- * check_moxi_agent -vvv -p 11211
- */
-int main_check(int argc, char **argv)
-{
- int ret;
-
- fprintf(stderr, "thread_id %x\n",
- (int) pthread_self());
-
- proxy_behavior pbg = behavior_default_g;
-
- unlink("t/check_moxi_agent.cfg");
- pmain = cproxy_init_agent_start("check_moxi_agent@localhost", // Fake JID.
- "password", // Fake password.
- "t/check_moxi_agent.cfg",
- NULL,
- pbg,
- settings.num_threads);
- assert(pmain);
-
- ret = pthread_create(&check_thread_tid, NULL,
- check_thread, NULL);
- if (ret != 0) {
- fprintf(stderr, "Can't create thread: %s\n", strerror(ret));
- exit(EXIT_FAILURE);
- }
-
- return 0;
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
View
51 check_work.c
@@ -11,8 +11,6 @@
#include "cproxy.h"
#include "work.h"
-static pthread_t check_thread_tid;
-
struct agg {
pthread_mutex_t lock;
int visits;
@@ -85,12 +83,19 @@ START_TEST(test_one)
}
END_TEST
+static
+void setup(void)
+{
+ start_main("moxi", NULL);
+}
+
static Suite* work_suite(void)
{
Suite *s = suite_create("work");
/* Core test case */
TCase *tc_core = tcase_create("core");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
tcase_add_test(tc_core, test_one);
tcase_add_test(tc_core, test_collect);
suite_add_tcase(s, tc_core);
@@ -98,46 +103,18 @@ static Suite* work_suite(void)
return s;
}
-// We have a separate thread here to pretend to be the libconflate thread.
-//
-static void *check_thread(void *arg) {
- sleep(1);
-
- assert(arg == NULL);
-
+/**
+ * Run this like...
+ * check_work -vvv -p 11211
+ */
+int main(int argc, char **argv)
+{
int number_failed;
Suite *s = work_suite();
SRunner *sr = srunner_create(s);
- srunner_set_fork_status(sr, CK_NOFORK);
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
- int rv = (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
- exit(rv);
-
- return NULL;
-}
-
-/**
- * Run this like...
- * check_work -vvv -p 11211
- */
-int main_check(int argc, char **argv)
-{
- int ret;
-
- fprintf(stderr, "thread_id %x\n", (int) pthread_self());
-
- check_thread(NULL);
- return 0;
-
- ret = pthread_create(&check_thread_tid, NULL,
- check_thread, NULL);
- if (ret != 0) {
- fprintf(stderr, "Can't create thread: %s\n", strerror(ret));
- exit(EXIT_FAILURE);
- }
-
- return 0;
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
View
56 memcached.c
@@ -4002,6 +4002,60 @@ static int enable_large_pages(void) {
}
#endif
+#ifdef MAIN_CHECK
+#define main main_memcached
+#endif
+
+int main(int argc, char **argv);
+
+#ifdef MAIN_CHECK
+
+static
+work_collect main_completion;
+
+struct main_data {
+ int argc;
+ char **argv;
+};
+
+static
+void *main_trampoline(void *_data)
+{
+ struct main_data *data = _data;
+ int rv = main_memcached(data->argc, data->argv);
+ printf("Unexpected exit from main with rv = %d\n", rv);
+ abort();
+}
+
+#include <stdarg.h>
+
+void start_main(char *arg0, ...)
+{
+ pthread_t main_thread;
+ va_list args;
+ int rv;
+ char *argv[256] = {0};
+ int argc = 0;
+ char *current_arg = arg0;
+
+ va_start(args, arg0);
+ while (current_arg != NULL) {
+ argv[argc++] = current_arg;
+ current_arg = va_arg(args, char *);
+ }
+ va_end(args);
+
+ work_collect_init(&main_completion, 1, NULL);
+ struct main_data data = {.argc = argc, .argv = argv};
+ rv = pthread_create(&main_thread, NULL, main_trampoline, &data);
+ if (rv) {
+ perror("pthread_create");
+ abort();
+ }
+ work_collect_wait(&main_completion);
+}
+#endif
+
int main (int argc, char **argv) {
int c;
bool lock_memory = false;
@@ -4399,7 +4453,7 @@ int main (int argc, char **argv) {
drop_privileges();
#ifdef MAIN_CHECK
- main_check(argc, argv);
+ work_collect_one(&main_completion);
#endif
/* enter the event loop */
View
3  memcached.h
@@ -584,3 +584,6 @@ extern void drop_privileges();
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
+
+// used by unit tests
+void start_main(char *arg0, ...);
Please sign in to comment.
Something went wrong with that request. Please try again.