+/* uTest
+ * Copyright (C) 2006-2009 Brian Aker
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license. See
+ * the COPYING file in the parent directory for full text.
+ */
+
/*
Sample test application.
*/
#include <assert.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
+#include <fnmatch.h>
+#include <stdint.h>
#include "test.h"
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
-#define TEST_SERVERS 1
+static void world_stats_print(world_stats_st *stats)
+{
+ fputc('\n', stderr);
+ fprintf(stderr, "Total Collections\t\t\t\t%u\n", stats->collection_total);
+ fprintf(stderr, "\tFailed Collections\t\t\t%u\n", stats->collection_failed);
+ fprintf(stderr, "\tSkipped Collections\t\t\t%u\n", stats->collection_skipped);
+ fprintf(stderr, "\tSucceeded Collections\t\t%u\n", stats->collection_success);
+ fputc('\n', stderr);
+ fprintf(stderr, "Total\t\t\t\t%u\n", stats->total);
+ fprintf(stderr, "\tFailed\t\t\t%u\n", stats->failed);
+ fprintf(stderr, "\tSkipped\t\t\t%u\n", stats->skipped);
+ fprintf(stderr, "\tSucceeded\t\t%u\n", stats->success);
+}
-long int timedif(struct timeval a, struct timeval b)
+static long int timedif(struct timeval a, struct timeval b)
{
- register int us, s;
+ long us, s;
- us = a.tv_usec - b.tv_usec;
+ us = (int)(a.tv_usec - b.tv_usec);
us /= 1000;
- s = a.tv_sec - b.tv_sec;
+ s = (int)(a.tv_sec - b.tv_sec);
s *= 1000;
return s + us;
}
-char *server_startup()
+const char *test_strerror(test_return_t code)
{
- unsigned int x;
- char server_string_buffer[8096];
- char *end_ptr;
-
- end_ptr= server_string_buffer;
+ switch (code) {
+ case TEST_SUCCESS:
+ return "ok";
+ case TEST_FAILURE:
+ return "failed";
+ case TEST_MEMORY_ALLOCATION_FAILURE:
+ return "memory allocation";
+ case TEST_SKIPPED:
+ return "skipped";
+ case TEST_MAXIMUM_RETURN:
+ default:
+ fprintf(stderr, "Unknown return value\n");
+ abort();
+ }
+}
- for (x= 0; x < TEST_SERVERS; x++)
+void create_core(void)
+{
+ if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
{
- char buffer[1024]; /* Nothing special for number */
- int count;
+ pid_t pid= fork();
- sprintf(buffer, "memcached -d -P /tmp/%umemc.pid -p %u", x, x+ TEST_PORT_BASE);
- system(buffer);
- count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
- end_ptr+= count;
+ if (pid == 0)
+ {
+ abort();
+ }
+ else
+ {
+ while (waitpid(pid, NULL, 0) != pid)
+ {
+ ;
+ }
+ }
}
- *end_ptr= 0;
+}
- return strdup(server_string_buffer);
+
+static test_return_t _runner_default(test_callback_fn func, void *p)
+{
+ if (func)
+ {
+ return func(p);
+ }
+ else
+ {
+ return TEST_SUCCESS;
+ }
}
-void server_shutdown(char *server_string)
+static world_runner_st defualt_runners= {
+ _runner_default,
+ _runner_default,
+ _runner_default
+};
+
+static test_return_t _default_callback(void *p)
{
- unsigned int x;
+ (void)p;
+
+ return TEST_SUCCESS;
+}
- for (x= 0; x < TEST_SERVERS; x++)
+static inline void set_default_fn(test_callback_fn *fn)
+{
+ if (*fn == NULL)
{
- char buffer[1024]; /* Nothing special for number */
- sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
- system(buffer);
+ *fn= _default_callback;
}
- if (server_string)
- free(server_string);
}
+static collection_st *init_world(world_st *world)
+{
+ if (! world->runner)
+ {
+ world->runner= &defualt_runners;
+ }
+
+ set_default_fn(&world->collection.startup);
+ set_default_fn(&world->collection.shutdown);
+
+ return world->collections;
+}
+
+
int main(int argc, char *argv[])
{
+ test_return_t return_code;
unsigned int x;
- char *server_list;
char *collection_to_run= NULL;
char *wildcard= NULL;
- memcached_server_st *servers;
+ world_st world;
collection_st *collection;
- uint8_t failed;
+ collection_st *next;
+ void *world_ptr;
- collection= gets_collections();
+ world_stats_st stats;
+ memset(&stats, 0, sizeof(stats));
+ memset(&world, 0, sizeof(world));
+ get_world(&world);
- if (argc > 1)
- collection_to_run= argv[1];
+ collection= init_world(&world);
- if (argc == 3)
- wildcard= argv[2];
-
- if ((server_list= getenv("MEMCACHED_SERVERS")))
+ if (world.create)
{
- printf("servers %s\n", server_list);
- servers= memcached_servers_parse(server_list);
- server_list= NULL;
+ test_return_t error;
+ world_ptr= world.create(&error);
+ if (error != TEST_SUCCESS)
+ exit(1);
}
else
{
- server_list= server_startup();
- printf("servers %s\n", server_list);
- servers= memcached_servers_parse(server_list);
+ world_ptr= NULL;
}
- assert(servers);
-
- srandom(time(NULL));
+ if (argc > 1)
+ collection_to_run= argv[1];
- for (x= 0; x < memcached_server_list_count(servers); x++)
- {
- printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
- assert(servers[x].stack_responses == 0);
- assert(servers[x].fd == -1);
- assert(servers[x].cursor_active == 0);
- }
-
- printf("\n");
+ if (argc == 3)
+ wildcard= argv[2];
- collection_st *next;
for (next= collection; next->name; next++)
{
+ test_return_t collection_rc= TEST_SUCCESS;
test_st *run;
+ bool failed= false;
+ bool skipped= false;
run= next->tests;
- if (collection_to_run && strcmp(collection_to_run, next->name))
+ if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
continue;
- fprintf(stderr, "\n%s\n\n", next->name);
+ stats.collection_total++;
- for (x= 0; run->name; run++)
+ collection_rc= world.collection.startup(world_ptr);
+
+ switch (collection_rc)
{
- if (wildcard && strcmp(wildcard, run->name))
+ case TEST_SUCCESS:
+ fprintf(stderr, "\n%s\n\n", next->name);
+ break;
+ case TEST_FAILURE:
+ fprintf(stderr, "\n%s [ failed ]\n\n", next->name);
+ stats.failed++;
continue;
+ case TEST_SKIPPED:
+ fprintf(stderr, "\n%s [ skipping ]\n\n", next->name);
+ stats.skipped++;
+ continue;
+ case TEST_MEMORY_ALLOCATION_FAILURE:
+ case TEST_MAXIMUM_RETURN:
+ default:
+ assert(0);
+ break;
+ }
- fprintf(stderr, "Testing %s", run->name);
-
- memcached_st *memc;
- memcached_return rc;
+ for (x= 0; run->name; run++)
+ {
struct timeval start_time, end_time;
+ long int load_time= 0;
- memc= memcached_create(NULL);
- assert(memc);
+ if (wildcard && fnmatch(wildcard, run->name, 0))
+ continue;
+
+ fprintf(stderr, "Testing %s", run->name);
- if (run->requires_flush)
- memcached_flush(memc, 0);
+ if (world.test.startup)
+ {
+ world.test.startup(world_ptr);
+ }
- rc= memcached_server_push(memc, servers);
- assert(rc == MEMCACHED_SUCCESS);
+ if (run->requires_flush && world.test.flush)
+ {
+ world.test.flush(world_ptr);
+ }
- unsigned int loop;
- for (loop= 0; loop < memcached_server_list_count(servers); loop++)
+ if (world.test.pre_run)
{
- assert(memc->hosts[loop].stack_responses == 0);
- assert(memc->hosts[loop].fd == -1);
- assert(memc->hosts[loop].cursor_active == 0);
+ world.test.pre_run(world_ptr);
}
- if (next->pre)
+
+ // Runner code
{
- memcached_return rc;
- rc= next->pre(memc);
+ if (next->pre && world.runner->pre)
+ {
+ return_code= world.runner->pre(next->pre, world_ptr);
- if (rc != MEMCACHED_SUCCESS)
+ if (return_code != TEST_SUCCESS)
+ {
+ goto error;
+ }
+ }
+
+ gettimeofday(&start_time, NULL);
+ return_code= world.runner->run(run->test_fn, world_ptr);
+ gettimeofday(&end_time, NULL);
+ load_time= timedif(end_time, start_time);
+
+ if (next->post && world.runner->post)
{
- fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
- goto error;
+ (void) world.runner->post(next->post, world_ptr);
}
}
- gettimeofday(&start_time, NULL);
- failed= run->function(memc);
- gettimeofday(&end_time, NULL);
- long int load_time= timedif(end_time, start_time);
- if (failed)
- fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
- load_time % 1000);
- else
- fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
- load_time % 1000);
-
- if (next->post)
- (void)next->post(memc);
-
- assert(memc);
+ if (world.test.post_run)
+ {
+ world.test.post_run(world_ptr);
+ }
+
error:
- memcached_free(memc);
+ stats.total++;
+
+ fprintf(stderr, "\t\t\t\t\t");
+
+ switch (return_code)
+ {
+ case TEST_SUCCESS:
+ fprintf(stderr, "%ld.%03ld ", load_time / 1000, load_time % 1000);
+ stats.success++;
+ break;
+ case TEST_FAILURE:
+ stats.failed++;
+ failed= true;
+ break;
+ case TEST_SKIPPED:
+ stats.skipped++;
+ skipped= true;
+ break;
+ case TEST_MEMORY_ALLOCATION_FAILURE:
+ fprintf(stderr, "Exhausted memory, quitting\n");
+ abort();
+ case TEST_MAXIMUM_RETURN:
+ default:
+ assert(0); // Coding error.
+ break;
+ }
+
+ fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
+
+ if (world.test.on_error)
+ {
+ test_return_t rc;
+ rc= world.test.on_error(return_code, world_ptr);
+
+ if (rc != TEST_SUCCESS)
+ break;
+ }
}
+
+ if (failed)
+ {
+ stats.collection_failed++;
+ }
+
+ if (skipped)
+ {
+ stats.collection_skipped++;
+ }
+
+ if (! failed && ! skipped)
+ {
+ stats.collection_success++;
+ }
+
+ world.collection.shutdown(world_ptr);
+ }
+
+ if (stats.collection_failed || stats.collection_skipped)
+ {
+ fprintf(stderr, "Some test failures and/or skipped test occurred.\n\n");
+ }
+ else
+ {
+ fprintf(stderr, "All tests completed successfully\n\n");
}
- fprintf(stderr, "All tests completed successfully\n\n");
+ if (world.destroy)
+ {
+ test_return_t error;
+ error= world.destroy(world_ptr);
- memcached_server_list_free(servers);
+ if (error != TEST_SUCCESS)
+ {
+ fprintf(stderr, "Failure during shutdown.\n");
+ stats.failed++; // We do this to make our exit code return 1
+ }
+ }
- server_shutdown(server_list);
+ world_stats_print(&stats);
- return 0;
+ return stats.failed == 0 ? 0 : 1;
}