Test updates.
[m6w6/libmemcached] / tests / test.c
index e0f50adb7f816d1ea07977b9de3702a34c3c462b..653cc7313a5ffebf80a23889f63283a73f4c43fa 100644 (file)
+/* 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 <memcached.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>
 
-void init_test(memcached_st *not_used)
-{
-  memcached_st memc;
-
-  (void)memcached_create(&memc);
-  memcached_free(&memc);
-}
+#include "test.h"
 
-void allocation_test(memcached_st *not_used)
+static void world_stats_print(world_stats_st *stats)
 {
-  memcached_st *memc;
-  memc= memcached_create(NULL);
-  assert(memc);
-  memcached_free(memc);
+  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);
 }
 
-void connection_test(memcached_st *memc)
+static long int timedif(struct timeval a, struct timeval b)
 {
-  memcached_return rc;
+  register int us, s;
 
-  rc= memcached_server_add(memc, "localhost", 0);
-  assert(rc == MEMCACHED_SUCCESS);
+  us = (int)(a.tv_usec - b.tv_usec);
+  us /= 1000;
+  s = (int)(a.tv_sec - b.tv_sec);
+  s *= 1000;
+  return s + us;
 }
 
-void error_test(memcached_st *memc)
+const char *test_strerror(test_return_t code)
 {
-  memcached_return rc;
-
-  for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++)
-  {
-    printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc));
+  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();
   }
 }
 
-void set_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-}
-
-void add_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  rc= memcached_add(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_NOTSTORED);
-}
-
-void replace_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  rc= memcached_replace(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-}
-
-void delete_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  rc= memcached_delete(memc, key, strlen(key), (time_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-}
-
-void flush_test(memcached_st *memc)
+void create_core(void)
 {
-  memcached_return rc;
-
-  rc= memcached_flush(memc, 0);
-  assert(rc == MEMCACHED_SUCCESS);
-}
-
-void get_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-
-  string= memcached_get(memc, key, strlen(key),
-                        &string_length, &flags, &rc);
-
-  assert(rc == MEMCACHED_NOTFOUND);
-  assert(string_length ==  0);
-  assert(!string);
-}
-
-void get_test2(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  string= memcached_get(memc, key, strlen(key),
-                        &string_length, &flags, &rc);
-
-  assert(string);
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(string_length == strlen(value));
-  assert(!memcmp(string, value, string_length));
-
-  free(string);
-}
-
-void set_test2(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "train in the brain";
-  size_t value_length= strlen(value);
-  unsigned int x;
-
-  for (x= 0; x < 10; x++)
+  if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
   {
-    rc= memcached_set(memc, key, strlen(key), 
-                      value, value_length,
-                      (time_t)0, (uint16_t)0);
-    assert(rc == MEMCACHED_SUCCESS);
+    pid_t pid= fork();
+
+    if (pid == 0)
+    {
+      abort();
+    }
+    else
+    {
+      while (waitpid(pid, NULL, 0) != pid)
+      {
+        ;
+      }
+    }
   }
 }
 
-void set_test3(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value;
-  size_t value_length= 8191;
-  unsigned int x;
-
-  value = (char*)malloc(value_length);
-  assert(value);
-
-  for (x= 0; x < value_length; x++)
-    value[x] = (char) (x % 127);
 
-  for (x= 0; x < 1; x++)
+static test_return_t _runner_default(test_callback_fn func, void *p)
+{
+  if (func)
   {
-    rc= memcached_set(memc, key, strlen(key), 
-                      value, value_length,
-                      (time_t)0, (uint16_t)0);
-    assert(rc == MEMCACHED_SUCCESS);
+    return func(p);
   }
-
-  free(value);
-}
-
-void get_test3(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value;
-  size_t value_length= 8191;
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-  int x;
-
-  value = (char*)malloc(value_length);
-  assert(value);
-
-  for (x= 0; x < value_length; x++)
-    value[x] = (char) (x % 127);
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, value_length,
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  string= memcached_get(memc, key, strlen(key),
-                        &string_length, &flags, &rc);
-
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(string);
-  assert(string_length == value_length);
-  assert(!memcmp(string, value, string_length));
-
-  free(string);
-  free(value);
-}
-
-void get_test4(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "foo";
-  char *value;
-  size_t value_length= 8191;
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-  int x;
-
-  value = (char*)malloc(value_length);
-  assert(value);
-
-  for (x= 0; x < value_length; x++)
-    value[x] = (char) (x % 127);
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, value_length,
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  for (x= 0; x < 10; x++)
+  else
   {
-    string= memcached_get(memc, key, strlen(key),
-                          &string_length, &flags, &rc);
-
-    assert(rc == MEMCACHED_SUCCESS);
-    assert(string);
-    assert(string_length == value_length);
-    assert(!memcmp(string, value, string_length));
-    free(string);
+    return TEST_SUCCESS;
   }
-
-  free(value);
-}
-
-void stats_servername_test(memcached_st *memc)
-{
-  memcached_return rc;
-  memcached_stat_st stat;
-  rc= memcached_stat_servername(&stat, NULL,
-                                 "localhost", 
-                                 MEMCACHED_DEFAULT_PORT);
-}
-
-void increment_test(memcached_st *memc)
-{
-  unsigned int new_number;
-  memcached_return rc;
-  char *key= "number";
-  char *value= "0";
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  rc= memcached_increment(memc, key, strlen(key),
-                          1, &new_number);
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(new_number == 1);
-
-  rc= memcached_increment(memc, key, strlen(key),
-                          1, &new_number);
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(new_number == 2);
 }
 
-void decrement_test(memcached_st *memc)
-{
-  unsigned int new_number;
-  memcached_return rc;
-  char *key= "number";
-  char *value= "3";
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  rc= memcached_decrement(memc, key, strlen(key),
-                          1, &new_number);
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(new_number == 2);
-
-  rc= memcached_decrement(memc, key, strlen(key),
-                          1, &new_number);
-  assert(rc == MEMCACHED_SUCCESS);
-  assert(new_number == 1);
-}
+static world_runner_st defualt_runners= {
+  _runner_default,
+  _runner_default,
+  _runner_default
+};
 
-void quit_test(memcached_st *memc)
-{
-  memcached_return rc;
-  char *key= "fudge";
-  char *value= "sanford and sun";
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)10, (uint16_t)3);
-  assert(rc == MEMCACHED_SUCCESS);
-  memcached_quit(memc);
-
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)50, (uint16_t)9);
-  assert(rc == MEMCACHED_SUCCESS);
-}
 
-void mget_test(memcached_st *memc)
+int main(int argc, char *argv[])
 {
-  memcached_return rc;
-  char *keys[]= {"fudge", "son", "food"};
-  size_t key_length[]= {5, 3, 4};
+  test_return_t return_code;
   unsigned int x;
-  uint16_t flags;
-
-  char return_key[MEMCACHED_MAX_KEY];
-  size_t return_key_length;
-  char *return_value;
-  size_t return_value_length;
+  char *collection_to_run= NULL;
+  char *wildcard= NULL;
+  world_st world;
+  collection_st *collection;
+  collection_st *next;
+  void *world_ptr;
 
-  /* We need to empty the server before continueing test */
-  rc= memcached_flush(memc, 0);
-  assert(rc == MEMCACHED_SUCCESS);
+  world_stats_st stats;
 
-  rc= memcached_mget(memc, keys, key_length, 3);
-  assert(rc == MEMCACHED_SUCCESS);
+  memset(&stats, 0, sizeof(stats));
+  memset(&world, 0, sizeof(world));
+  get_world(&world);
 
-  while ((return_value= memcached_fetch(memc, return_key, &return_key_length, 
-                      &return_value_length, &flags, &rc)) != NULL)
+  if (! world.runner)
   {
-    assert(return_value);
+    world.runner= &defualt_runners;
   }
-  assert(!return_value);
-  assert(return_value_length == 0);
-  assert(rc == MEMCACHED_NOTFOUND);
 
-  for (x= 0; x < 3; x++)
-  {
-    rc= memcached_set(memc, keys[x], key_length[x], 
-                      keys[x], key_length[x],
-                      (time_t)50, (uint16_t)9);
-    assert(rc == MEMCACHED_SUCCESS);
-  }
-
-  rc= memcached_mget(memc, keys, key_length, 3);
-  assert(rc == MEMCACHED_SUCCESS);
+  collection= world.collections;
 
-  x= 0;
-  while ((return_value= memcached_fetch(memc, return_key, &return_key_length, 
-                                        &return_value_length, &flags, &rc)))
+  if (world.create)
   {
-    assert(return_value);
-    assert(rc == MEMCACHED_SUCCESS);
-    assert(return_key_length == return_value_length);
-    assert(!memcmp(return_value, return_key, return_value_length));
-    free(return_value);
-    x++;
+    test_return_t error;
+    world_ptr= world.create(&error);
+    if (error != TEST_SUCCESS)
+      exit(1);
   }
-}
-
-void get_stats_keys(memcached_st *memc)
-{
- char **list;
- char **ptr;
- memcached_stat_st stat;
- memcached_return rc;
-
- list= memcached_stat_get_keys(memc, &stat, &rc);
- assert(rc == MEMCACHED_SUCCESS);
- for (ptr= list; *ptr; ptr++)
-   printf("Found key %s\n", *ptr);
-
- free(list);
-}
-
-void get_stats(memcached_st *memc)
-{
- unsigned int x;
- char **list;
- char **ptr;
- memcached_return rc;
- memcached_stat_st *stat;
-
- stat= memcached_stat(memc, NULL, &rc);
- assert(rc == MEMCACHED_SUCCESS);
-
- assert(rc == MEMCACHED_SUCCESS);
- assert(stat);
-
- for (x= 0; x < memcached_server_count(memc); x++)
- {
-   list= memcached_stat_get_keys(memc, &stat[x], &rc);
-   assert(rc == MEMCACHED_SUCCESS);
-   for (ptr= list; *ptr; ptr++)
-     printf("Found key %s\n", *ptr);
-
-   free(list);
- }
-
- free(stat);
-}
-
-void add_host_test(memcached_st *memc)
-{
-  unsigned int x;
-  memcached_server_st *servers;
-  memcached_return rc;
-  char servername[]= "0.example.com";
-
-  servers= memcached_server_list_append(NULL, servername, 400, &rc);
-  assert(servers);
-  assert(1 == memcached_server_list_count(servers));
-
-  for (x= 2; x < 20; x++)
+  else
   {
-    char buffer[SMALL_STRING_LEN];
-
-    snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
-    servers= memcached_server_list_append(servers, buffer, 401, 
-                                     &rc);
-    assert(rc == MEMCACHED_SUCCESS);
-    assert(x == memcached_server_list_count(servers));
+    world_ptr= NULL;
   }
 
-  rc= memcached_server_push(memc, servers);
-  assert(rc == MEMCACHED_SUCCESS);
-  rc= memcached_server_push(memc, servers);
-  assert(rc == MEMCACHED_SUCCESS);
+  if (argc > 1)
+    collection_to_run= argv[1];
 
-  memcached_server_list_free(servers);
-}
+  if (argc == 3)
+    wildcard= argv[2];
 
-void add_host_test1(memcached_st *memc)
-{
-  unsigned int x;
-  memcached_return rc;
-  char servername[]= "0.example.com";
-  memcached_server_st *servers;
-
-  servers= memcached_server_list_append(NULL, servername, 400, &rc);
-  assert(servers);
-  assert(1 == memcached_server_list_count(servers));
-
-  for (x= 2; x < 20; x++)
+  for (next= collection; next->name; next++)
   {
-    char buffer[SMALL_STRING_LEN];
-
-    snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
-    servers= memcached_server_list_append(servers, buffer, 401, 
-                                     &rc);
-    assert(rc == MEMCACHED_SUCCESS);
-    assert(x == memcached_server_list_count(servers));
+    test_st *run;
+
+    run= next->tests;
+    if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
+      continue;
+
+    fprintf(stderr, "\n%s\n\n", next->name);
+
+    for (x= 0; run->name; run++)
+    {
+      struct timeval start_time, end_time;
+      long int load_time= 0;
+
+      if (wildcard && fnmatch(wildcard, run->name, 0))
+        continue;
+
+      fprintf(stderr, "Testing %s", run->name);
+
+      if (world.collection_startup)
+      {
+        world.collection_startup(world_ptr);
+      }
+
+      if (run->requires_flush && world.flush)
+      {
+        world.flush(world_ptr);
+      }
+
+      if (world.pre_run)
+      {
+        world.pre_run(world_ptr);
+      }
+
+
+      if (next->pre && world.runner->pre)
+      {
+        return_code= world.runner->pre(next->pre, world_ptr);
+
+        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)
+      {
+        (void) world.runner->post(next->post, world_ptr);
+      }
+
+      if (world.post_run)
+      {
+        world.post_run(world_ptr);
+      }
+
+error:
+      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++;
+        break;
+      case TEST_SKIPPED:
+        stats.skipped++;
+        break;
+      case TEST_MEMORY_ALLOCATION_FAILURE:
+      case TEST_MAXIMUM_RETURN:
+      default:
+        break;
+      }
+
+      fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
+
+      if (world.on_error)
+      {
+        test_return_t rc;
+        rc= world.on_error(return_code, world_ptr);
+
+        if (rc != TEST_SUCCESS)
+          break;
+      }
+    }
   }
 
-  rc= memcached_server_push(memc, servers);
-  assert(rc == MEMCACHED_SUCCESS);
-  rc= memcached_server_push(memc, servers);
-  assert(rc == MEMCACHED_SUCCESS);
-
-  memcached_server_list_free(servers);
-}
-
-typedef struct test_st test_st;
+  fprintf(stderr, "All tests completed successfully\n\n");
 
-struct test_st {
-  char *function_name;
-  unsigned int requires_flush;
-  void (*function)(memcached_st *memc);
-};
-
-int main(int argc, char *argv[])
-{
-  unsigned int x;
-  char *server_list;
-  memcached_server_st *servers;
-
-  if (!(server_list= getenv("MEMCACHED_SERVERS")))
-    server_list= "localhost";
-
-  printf("servers %s\n", server_list);
-
-  servers= memcached_servers_parse(server_list);
-  assert(servers);
-
-  /* Clean the server before beginning testing */
-  test_st tests[] ={
-    {"flush", 0, flush_test },
-    {"init", 0, init_test },
-    {"allocation", 0, allocation_test },
-    {"error", 0, error_test },
-    {"set", 0, set_test },
-    {"set2", 0, set_test2 },
-    {"set3", 0, set_test3 },
-    {"add", 0, add_test },
-    {"replace", 0, replace_test },
-    {"delete", 1, delete_test },
-    {"get", 0, get_test },
-    {"get2", 0, get_test2 },
-    {"get3", 0, get_test3 },
-    {"get4", 0, get_test4 },
-    {"stats_servername", 0, stats_servername_test },
-    {"increment", 0, increment_test },
-    {"decrement", 0, decrement_test },
-    {"quit", 0, quit_test },
-    {"mget", 0, mget_test },
-    {"get_stats", 0, get_stats },
-    {"add_host_test", 0, add_host_test },
-    {"get_stats_keys", 0, get_stats_keys },
-    {0, 0, 0}
-  };
-
-  fprintf(stderr, "\nBlock tests\n\n");
-  for (x= 0; tests[x].function_name; x++)
-  {
-    memcached_st *memc;
-    memcached_return rc;
-    memc= memcached_create(NULL);
-    assert(memc);
-
-    rc= memcached_server_push(memc, servers);
-    assert(rc == MEMCACHED_SUCCESS);
-
-    fprintf(stderr, "Testing %s", tests[x].function_name);
-    tests[x].function(memc);
-    fprintf(stderr, "\t\t\t\t\t[ ok ]\n");
-    assert(memc);
-    memcached_free(memc);
-  }
-
-  fprintf(stderr, "\nNonblock tests\n\n");
-  for (x= 0; tests[x].function_name; x++)
+  if (world.destroy)
   {
-    memcached_st *memc;
-    memcached_return rc;
-    memc= memcached_create(NULL);
-    assert(memc);
-
-    rc= memcached_server_push(memc, servers);
-    assert(rc == MEMCACHED_SUCCESS);
-
-    fprintf(stderr, "Testing %s", tests[x].function_name);
-    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
-    tests[x].function(memc);
-    fprintf(stderr, "\t\t\t\t\t[ ok ]\n");
-    assert(memc);
-    memcached_free(memc);
+    test_return_t error;
+    error= world.destroy(world_ptr);
+
+    if (error != TEST_SUCCESS)
+    {
+      fprintf(stderr, "Failure during shutdown.\n");
+      stats.failed++; // We do this to make our exit code return 1
+    }
   }
 
-  fprintf(stderr, "\nTCP Nodelay tests\n\n");
-  for (x= 0; tests[x].function_name; x++)
-  {
-    memcached_st *memc;
-    memcached_return rc;
-    memc= memcached_create(NULL);
-    assert(memc);
-
-    rc= memcached_server_push(memc, servers);
-    assert(rc == MEMCACHED_SUCCESS);
-
-    fprintf(stderr, "Testing %s", tests[x].function_name);
-    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
-    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, NULL);
-    tests[x].function(memc);
-    fprintf(stderr, "\t\t\t\t\t[ ok ]\n");
-    assert(memc);
-    memcached_free(memc);
-  }
+  world_stats_print(&stats);
 
-  /* Clean up whatever we might have left */
-  {
-    memcached_st *memc;
-    memc= memcached_create(NULL);
-    assert(memc);
-    flush_test(memc);
-    memcached_free(memc);
-  }
-  return 0;
+  return stats.failed == 0 ? 0 : 1;
 }