Additional cleanup for config.h
[m6w6/libmemcached] / tests / test.c
index 020bfc5347939d4a1c866535ea78545cad537b91..215218c5f068e5a3f9bfc26c26a419d03bf9cd72 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 "config.h"
+
 #include <assert.h>
-#include <memcached.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
-void init_test(void)
+#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 "libmemcached/memcached.h"
+#include "test.h"
+
+static void world_stats_print(world_stats_st *stats)
 {
-  memcached_st memc;
-
-  (void)memcached_init(&memc);
-  memcached_deinit(&memc);
+  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);
 }
 
-void allocation_test(void)
+long int timedif(struct timeval a, struct timeval b)
 {
-  memcached_st *memc;
-  memc= memcached_init(NULL);
-  assert(memc);
-  memcached_deinit(memc);
-}
+  long us, s;
 
-void set_test(void)
-{
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  rc= memcached_set(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-  
-  memcached_deinit(memc);
+  us = (int)(a.tv_usec - b.tv_usec);
+  us /= 1000;
+  s = (int)(a.tv_sec - b.tv_sec);
+  s *= 1000;
+  return s + us;
 }
 
-void add_test(void)
+const char *test_strerror(test_return_t code)
 {
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  rc= memcached_add(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_NOTSTORED);
-  
-  memcached_deinit(memc);
+  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 replace_test(void)
+void create_core(void)
 {
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  rc= memcached_replace(memc, key, strlen(key), 
-                    value, strlen(value),
-                    (time_t)0, (uint16_t)0);
-  assert(rc == MEMCACHED_SUCCESS);
-  
-  memcached_deinit(memc);
+  if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
+  {
+    pid_t pid= fork();
+
+    if (pid == 0)
+    {
+      abort();
+    }
+    else
+    {
+      while (waitpid(pid, NULL, 0) != pid)
+      {
+        ;
+      }
+    }
+  }
 }
 
-void delete_test(void)
+
+static test_return_t _runner_default(test_callback_fn func, void *p)
 {
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  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);
-  
-  memcached_deinit(memc);
+  if (func)
+  {
+    return func(p);
+  }
+  else
+  {
+    return TEST_SUCCESS;
+  }
 }
 
-void flush_test(void)
-{
-  memcached_st *memc;
-  memcached_return rc;
+static world_runner_st defualt_runners= {
+  _runner_default,
+  _runner_default,
+  _runner_default
+};
 
-  memc= memcached_init(NULL);
-  assert(memc);
-  rc= memcached_flush(memc, 0);
-  assert(rc == MEMCACHED_SUCCESS);
+static test_return_t _default_callback(void *p)
+{
+  (void)p;
 
-  memcached_deinit(memc);
+  return TEST_SUCCESS;
 }
 
-void get_test(void)
+static inline void set_default_fn(test_callback_fn *fn)
 {
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  
-  string= memcached_get(memc, key, strlen(key),
-                        &string_length, &flags, &rc);
-
-  assert(string_length ==  0);
-  assert(!string);
-
-  memcached_deinit(memc);
+  if (*fn == NULL)
+  {
+    *fn= _default_callback;
+  }
 }
 
-void get_test2(void)
+static collection_st *init_world(world_st *world)
 {
-  memcached_st *memc;
-  memcached_return rc;
-  char *key= "foo";
-  char *value= "when we sanitize";
-  char *string;
-  size_t string_length;
-  uint16_t flags;
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  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_length == strlen(value));
-  assert(!memcmp(string, value, string_length));
-
-  free(string);
-
-  memcached_deinit(memc);
-}
+  if (! world->runner)
+  {
+    world->runner= &defualt_runners;
+  }
 
-void stats_hostname_test(void)
-{
-  memcached_return rc;
-  memcached_stat_st stat;
-  rc= memcached_stat_hostname(&stat, NULL,
-                              "localhost", 
-                              MEMCACHED_DEFAULT_PORT);
-}
+  set_default_fn(&world->collection.startup);
+  set_default_fn(&world->collection.shutdown);
 
-void increment_test(void)
-{
-  memcached_st *memc;
-  unsigned int new_number;
-  memcached_return rc;
-  char *key= "number";
-  char *value= "0";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  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);
-
-  memcached_deinit(memc);
+  return world->collections;
 }
 
-void decrement_test(void)
-{
-  memcached_st *memc;
-  unsigned int new_number;
-  memcached_return rc;
-  char *key= "number";
-  char *value= "3";
-
-  memc= memcached_init(NULL);
-  assert(memc);
-  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);
-
-  memcached_deinit(memc);
-}
 
-int main(void)
+int main(int argc, char *argv[])
 {
-  /* Clean the server before beginning testing */
-  flush_test();
-
-  init_test();
-  allocation_test();
-  set_test();
-  add_test();
-  replace_test();
-  flush_test();
-  delete_test();
-  flush_test();
-  get_test();
-  get_test2();
-  stats_hostname_test();
-
-  increment_test();
-  decrement_test();
-
-  /* Clean up whatever we might have left */
-  flush_test();
-  return 0;
+  test_return_t return_code;
+  unsigned int x;
+  char *collection_to_run= NULL;
+  char *wildcard= NULL;
+  world_st world;
+  collection_st *collection;
+  collection_st *next;
+  void *world_ptr;
+
+  world_stats_st stats;
+
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+  if (sasl_client_init(NULL) != SASL_OK)
+  {
+     fprintf(stderr, "Failed to initialize sasl library!\n");
+     return 1;
+  }
+#endif
+
+  memset(&stats, 0, sizeof(stats));
+  memset(&world, 0, sizeof(world));
+  get_world(&world);
+
+  collection= init_world(&world);
+
+  if (world.create)
+  {
+    test_return_t error;
+    world_ptr= world.create(&error);
+    if (error != TEST_SUCCESS)
+      exit(1);
+  }
+  else
+  {
+    world_ptr= NULL;
+  }
+
+  if (argc > 1)
+    collection_to_run= argv[1];
+
+  if (argc == 3)
+    wildcard= argv[2];
+
+  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 && fnmatch(collection_to_run, next->name, 0))
+      continue;
+
+    stats.collection_total++;
+
+    collection_rc= world.collection.startup(world_ptr);
+
+    if (collection_rc != TEST_SUCCESS)
+      goto skip_pre;
+
+    if (next->pre)
+    {
+      collection_rc= world.runner->pre(next->pre, world_ptr);
+    }
+
+skip_pre:
+    switch (collection_rc)
+    {
+      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.collection_failed++;
+        goto cleanup;
+      case TEST_SKIPPED:
+        fprintf(stderr, "\n%s [ skipping ]\n\n", next->name);
+        stats.collection_skipped++;
+        goto cleanup;
+      case TEST_MEMORY_ALLOCATION_FAILURE:
+      case TEST_MAXIMUM_RETURN:
+      default:
+        assert(0);
+        break;
+    }
+
+
+    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.test.startup)
+      {
+        world.test.startup(world_ptr);
+      }
+
+      if (run->requires_flush && world.test.flush)
+      {
+        world.test.flush(world_ptr);
+      }
+
+      if (world.test.pre_run)
+      {
+        world.test.pre_run(world_ptr);
+      }
+
+
+      // Runner code
+      {
+#if 0
+        if (next->pre && world.runner->pre)
+        {
+          return_code= world.runner->pre(next->pre, world_ptr);
+
+          if (return_code != TEST_SUCCESS)
+          {
+            goto error;
+          }
+        }
+#endif
+
+        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 0
+        if (next->post && world.runner->post)
+        {
+          (void) world.runner->post(next->post, world_ptr);
+        }
+#endif
+      }
+
+      if (world.test.post_run)
+      {
+        world.test.post_run(world_ptr);
+      }
+
+      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 (next->post && world.runner->post)
+    {
+      (void) world.runner->post(next->post, world_ptr);
+    }
+
+    if (! failed && ! skipped)
+    {
+      stats.collection_success++;
+    }
+cleanup:
+
+    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");
+  }
+
+  if (world.destroy)
+  {
+    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
+    }
+  }
+
+  world_stats_print(&stats);
+
+#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
+  sasl_done();
+#endif
+
+  return stats.failed == 0 ? 0 : 1;
 }