32/64 bit cleanup issue in tests.
[m6w6/libmemcached] / tests / mem_functions.c
index 1c2a80fd3b37e6b95f313e01471d0b6b153089bb..1d4b94a9d4c5c9683fcc01d8a363fd2a6683cb77 100644 (file)
@@ -80,22 +80,22 @@ static test_return_t  server_list_null_test(memcached_st *ptr __attribute__((unu
 }
 
 #define TEST_PORT_COUNT 7
-uint32_t test_ports[TEST_PORT_COUNT];
+in_port_t test_ports[TEST_PORT_COUNT];
 
 static memcached_return_t  server_display_function(memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
 {
   /* Do Nothing */
-  uint32_t bigger= *((uint32_t *)(context));
+  size_t bigger= *((size_t *)(context));
   assert(bigger <= server->port);
-  *((uint32_t *)(context))= server->port;
+  *((size_t *)(context))= server->port;
 
   return MEMCACHED_SUCCESS;
 }
 
 static test_return_t  server_sort_test(memcached_st *ptr __attribute__((unused)))
 {
-  uint32_t x;
-  uint32_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
+  size_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
+
   memcached_return_t rc;
   memcached_server_fn callbacks[1];
   memcached_st *local_memc;
@@ -104,9 +104,9 @@ static test_return_t  server_sort_test(memcached_st *ptr __attribute__((unused))
   test_truth(local_memc);
   memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1);
 
-  for (x= 0; x < TEST_PORT_COUNT; x++)
+  for (size_t x= 0; x < TEST_PORT_COUNT; x++)
   {
-    test_ports[x]= (uint32_t)random() % 64000;
+    test_ports[x]= (in_port_t)random() % 64000;
     rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
     test_truth(memcached_server_count(local_memc) == x + 1);
     test_truth(memcached_servers_count(memcached_server_list(local_memc)) == x+1);
@@ -124,7 +124,7 @@ static test_return_t  server_sort_test(memcached_st *ptr __attribute__((unused))
 
 static test_return_t  server_sort2_test(memcached_st *ptr __attribute__((unused)))
 {
-  uint32_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
+  size_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
   memcached_return_t rc;
   memcached_server_fn callbacks[1];
   memcached_st *local_memc;
@@ -171,9 +171,8 @@ static memcached_return_t server_display_unsort_function(memcached_st *ptr __att
 
 static test_return_t  server_unsort_test(memcached_st *ptr __attribute__((unused)))
 {
-  uint32_t x;
-  uint32_t counter= 0; /* Prime the value for the test_truth in server_display_function */
-  uint32_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
+  size_t counter= 0; /* Prime the value for the test_truth in server_display_function */
+  size_t bigger= 0; /* Prime the value for the test_truth in server_display_function */
   memcached_return_t rc;
   memcached_server_fn callbacks[1];
   memcached_st *local_memc;
@@ -181,9 +180,9 @@ static test_return_t  server_unsort_test(memcached_st *ptr __attribute__((unused
   local_memc= memcached_create(NULL);
   test_truth(local_memc);
 
-  for (x= 0; x < TEST_PORT_COUNT; x++)
+  for (size_t x= 0; x < TEST_PORT_COUNT; x++)
   {
-    test_ports[x]= (uint32_t)(random() % 64000);
+    test_ports[x]= (in_port_t)(random() % 64000);
     rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
     test_truth(memcached_server_count(local_memc) == x+1);
     test_truth(memcached_servers_count(memcached_server_list(local_memc)) == x+1);
@@ -1505,7 +1504,7 @@ static memcached_return_t callback_counter(memcached_st *ptr __attribute__((unus
                                            memcached_result_st *result __attribute__((unused)),
                                            void *context)
 {
-  unsigned int *counter= (unsigned int *)context;
+  size_t *counter= (size_t *)context;
 
   *counter= *counter + 1;
 
@@ -1518,7 +1517,7 @@ static test_return_t  mget_result_function(memcached_st *memc)
   const char *keys[]= {"fudge", "son", "food"};
   size_t key_length[]= {5, 3, 4};
   unsigned int x;
-  unsigned int counter;
+  size_t counter;
   memcached_execute_fn callbacks[1];
 
   /* We need to empty the server before continueing test */
@@ -1601,6 +1600,7 @@ static test_return_t  mget_test(memcached_st *memc)
 static test_return_t mget_execute(memcached_st *memc)
 {
   bool binary= false;
+
   if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
     binary= true;
 
@@ -1611,19 +1611,20 @@ static test_return_t mget_execute(memcached_st *memc)
   uint32_t number_of_hosts= memc->number_of_hosts;
   memc->number_of_hosts= 1;
 
-  int max_keys= binary ? 20480 : 1;
+  size_t max_keys= binary ? 20480 : 1;
 
 
-  char **keys= calloc((size_t)max_keys, sizeof(char*));
-  size_t *key_length=calloc((size_t)max_keys, sizeof(size_t));
+  char **keys= calloc(max_keys, sizeof(char*));
+  size_t *key_length=calloc(max_keys, sizeof(size_t));
 
   /* First add all of the items.. */
   char blob[1024] = {0};
   memcached_return_t rc;
-  for (int x= 0; x < max_keys; ++x)
+  for (size_t x= 0; x < max_keys; ++x)
   {
     char k[251];
-    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%u", x);
+
+    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
     keys[x]= strdup(k);
     test_truth(keys[x] != NULL);
     rc= memcached_add(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
@@ -1631,10 +1632,10 @@ static test_return_t mget_execute(memcached_st *memc)
   }
 
   /* Try to get all of them with a large multiget */
-  unsigned int counter= 0;
+  size_t counter= 0;
   memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
   rc= memcached_mget_execute(memc, (const char**)keys, key_length,
-                             (size_t)max_keys, callbacks, &counter, 1);
+                             max_keys, callbacks, &counter, 1);
 
   if (binary)
   {
@@ -1644,7 +1645,7 @@ static test_return_t mget_execute(memcached_st *memc)
     test_truth(rc == MEMCACHED_END);
 
     /* Verify that we got all of the items */
-    test_truth(counter == (unsigned int)max_keys);
+    test_truth(counter == max_keys);
   }
   else
   {
@@ -1653,8 +1654,10 @@ static test_return_t mget_execute(memcached_st *memc)
   }
 
   /* Release all allocated resources */
-  for (int x= 0; x < max_keys; ++x)
+  for (size_t x= 0; x < max_keys; ++x)
+  {
     free(keys[x]);
+  }
   free(keys);
   free(key_length);
 
@@ -3297,7 +3300,7 @@ static test_return_t  mget_read_result(memcached_st *memc)
 static test_return_t  mget_read_function(memcached_st *memc)
 {
   memcached_return_t rc;
-  unsigned int counter;
+  size_t counter;
   memcached_execute_fn callbacks[1];
 
   rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
@@ -3324,13 +3327,12 @@ static test_return_t  delete_generate(memcached_st *memc)
 
 static test_return_t  delete_buffer_generate(memcached_st *memc)
 {
-  size_t latch= 0;
-  unsigned int x;
+  uint64_t latch= 0;
 
   latch= 1;
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
 
-  for (x= 0; x < global_count; x++)
+  for (size_t x= 0; x < global_count; x++)
   {
     (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
   }
@@ -3517,8 +3519,7 @@ static test_return_t pre_binary(memcached_st *memc)
 {
   memcached_return_t rc= MEMCACHED_FAILURE;
   memcached_st *memc_clone;
-  memcached_server_instance_st *instance=
-    memcached_server_instance_fetch(memc, 0);
+  memcached_server_instance_st *instance;
 
   memc_clone= memcached_clone(NULL, memc);
   test_truth(memc_clone);
@@ -3526,6 +3527,8 @@ static test_return_t pre_binary(memcached_st *memc)
   // will not toggle protocol on an connection.
   memcached_version(memc_clone);
 
+  instance= memcached_server_instance_fetch(memc_clone, 0);
+
   if (instance->major_version >= 1 && instance->minor_version > 2)
   {
     rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
@@ -3956,7 +3959,11 @@ static test_return_t noreply_test(memcached_st *memc)
     */
     int no_msg=0;
     for (uint32_t x=0; x < memcached_server_count(memc); ++x)
-      no_msg+=(int)(memc->hosts[x].cursor_active);
+    {
+      memcached_server_instance_st *instance=
+        memcached_server_instance_fetch(memc, x);
+      no_msg+=(int)(instance->cursor_active);
+    }
 
     test_truth(no_msg == 0);
     test_truth(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
@@ -4058,7 +4065,7 @@ static memcached_return_t callback_dump_counter(memcached_st *ptr __attribute__(
                                               size_t key_length __attribute__((unused)),
                                               void *context)
 {
-  uint32_t *counter= (uint32_t *)context;
+  size_t *counter= (size_t *)context;
 
   *counter= *counter + 1;
 
@@ -4068,7 +4075,7 @@ static memcached_return_t callback_dump_counter(memcached_st *ptr __attribute__(
 static test_return_t dump_test(memcached_st *memc)
 {
   memcached_return_t rc;
-  uint32_t counter= 0;
+  size_t counter= 0;
   memcached_dump_fn callbacks[1];
   test_return_t main_rc;
 
@@ -4235,7 +4242,10 @@ static test_return_t replication_get_test(memcached_st *memc)
   for (uint32_t host= 0; host < memcached_server_count(memc); ++host)
   {
     memcached_st *memc_clone= memcached_clone(NULL, memc);
-    memc_clone->hosts[host].port= 0;
+    memcached_server_instance_st *instance=
+      memcached_server_instance_fetch(memc_clone, host);
+
+    instance->port= 0;
 
     for (int x= 'a'; x <= 'z'; ++x)
     {
@@ -4294,7 +4304,9 @@ static test_return_t replication_mget_test(memcached_st *memc)
   for (uint32_t host= 0; host < memc_clone->number_of_hosts; host++)
   {
     memcached_st *new_clone= memcached_clone(NULL, memc);
-    new_clone->hosts[host].port= 0;
+    memcached_server_instance_st *instance=
+      memcached_server_instance_fetch(new_clone, host);
+    instance->port= 0;
 
     for (int x= 'a'; x <= 'z'; ++x)
     {
@@ -4389,7 +4401,10 @@ static test_return_t replication_delete_test(memcached_st *memc)
   uint32_t hash= memcached_generate_hash(memc, keys[0], len[0]);
   for (uint32_t x= 0; x < (repl + 1); ++x)
   {
-    memc_clone->hosts[hash].port= 0;
+    memcached_server_instance_st *instance=
+      memcached_server_instance_fetch(memc_clone, x);
+
+    instance->port= 0;
     if (++hash == memc_clone->number_of_hosts)
       hash= 0;
   }
@@ -4435,7 +4450,12 @@ static uint16_t *get_udp_request_ids(memcached_st *memc)
   unsigned int x;
 
   for (x= 0; x < memcached_server_count(memc); x++)
-    ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer);
+  {
+    memcached_server_instance_st *instance=
+      memcached_server_instance_fetch(memc, x);
+
+    ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) instance->write_buffer);
+  }
 
   return ids;
 }
@@ -4443,7 +4463,7 @@ static uint16_t *get_udp_request_ids(memcached_st *memc)
 static test_return_t post_udp_op_check(memcached_st *memc, uint16_t *expected_req_ids)
 {
   unsigned int x;
-  memcached_server_st *cur_server = memc->hosts;
+  memcached_server_st *cur_server = memcached_server_list(memc);
   uint16_t *cur_req_ids = get_udp_request_ids(memc);
 
   for (x= 0; x < memcached_server_count(memc); x++)
@@ -4475,16 +4495,24 @@ static test_return_t init_udp(memcached_st *memc)
   uint32_t num_hosts= memcached_server_count(memc);
   unsigned int x= 0;
   memcached_server_st servers[num_hosts];
-  memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
+  memcpy(servers, memcached_server_list(memc), sizeof(memcached_server_st) * num_hosts);
   for (x= 0; x < num_hosts; x++)
-    memcached_server_free(&memc->hosts[x]);
+  {
+    memcached_server_instance_st *set_instance=
+      memcached_server_instance_fetch(memc, x);
+
+    memcached_server_free(set_instance);
+  }
 
   memc->number_of_hosts= 0;
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
   for (x= 0; x < num_hosts; x++)
   {
+    memcached_server_instance_st *set_instance=
+      memcached_server_instance_fetch(memc, x);
+
     test_truth(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
-    test_truth(memc->hosts[x].write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
+    test_truth(set_instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
   }
 
   return TEST_SUCCESS;
@@ -5129,7 +5157,7 @@ static test_return_t regression_bug_434843(memcached_st *memc)
     return test_rc;
 
   memcached_return_t rc;
-  unsigned int counter= 0;
+  size_t counter= 0;
   memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
 
   /*
@@ -5144,10 +5172,11 @@ static test_return_t regression_bug_434843(memcached_st *memc)
   char **keys= calloc(max_keys, sizeof(char*));
   size_t *key_length=calloc(max_keys, sizeof(size_t));
 
-  for (int x= 0; x < (int)max_keys; ++x)
+  for (size_t x= 0; x < max_keys; ++x)
   {
      char k[251];
-     key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%u", x);
+
+     key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
      keys[x]= strdup(k);
      test_truth(keys[x] != NULL);
   }
@@ -5156,17 +5185,20 @@ static test_return_t regression_bug_434843(memcached_st *memc)
    * Run two times.. the first time we should have 100% cache miss,
    * and the second time we should have 100% cache hits
    */
-  for (int y= 0; y < 2; ++y)
+  for (size_t y= 0; y < 2; y++)
   {
     rc= memcached_mget(memc, (const char**)keys, key_length, max_keys);
     test_truth(rc == MEMCACHED_SUCCESS);
     rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+
     if (y == 0)
     {
       /* The first iteration should give me a 100% cache miss. verify that*/
-      test_truth(counter == 0);
       char blob[1024]= { 0 };
-      for (int x= 0; x < (int)max_keys; ++x)
+
+      test_truth(counter == 0);
+
+      for (size_t x= 0; x < max_keys; ++x)
       {
         rc= memcached_add(memc, keys[x], key_length[x],
                           blob, sizeof(blob), 0, 0);
@@ -5176,13 +5208,15 @@ static test_return_t regression_bug_434843(memcached_st *memc)
     else
     {
       /* Verify that we received all of the key/value pairs */
-       test_truth(counter == (unsigned int)max_keys);
+       test_truth(counter == max_keys);
     }
   }
 
   /* Release allocated resources */
   for (size_t x= 0; x < max_keys; ++x)
+  {
     free(keys[x]);
+  }
   free(keys);
   free(key_length);
 
@@ -5293,10 +5327,11 @@ static test_return_t regression_bug_447342(memcached_st *memc)
   char **keys= calloc(max_keys, sizeof(char*));
   size_t *key_length= calloc(max_keys, sizeof(size_t));
 
-  for (uint64_t x= 0; x < max_keys; ++x)
+  for (size_t x= 0; x < max_keys; ++x)
   {
     char k[251];
-    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%"PRIu64, x);
+
+    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
     keys[x]= strdup(k);
     test_truth(keys[x] != NULL);
     rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0);
@@ -5324,11 +5359,11 @@ static test_return_t regression_bug_447342(memcached_st *memc)
   rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
   test_truth(rc == MEMCACHED_SUCCESS);
 
-  unsigned int counter= 0;
+  size_t counter= 0;
   memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
   rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
   /* Verify that we received all of the key/value pairs */
-  test_truth(counter == (unsigned int)max_keys);
+  test_truth(counter == max_keys);
 
   memcached_quit(memc);
   /*
@@ -5556,6 +5591,91 @@ static test_return_t wrong_failure_counter_test(memcached_st *memc)
   return TEST_SUCCESS;
 }
 
+
+
+
+/*
+ * Test that ensures mget_execute does not end into recursive calls that finally fails
+ */
+static test_return_t regression_bug_490486(memcached_st *memc)
+{
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 1000);
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1);
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 3600);
+
+  /*
+   * I only want to hit _one_ server so I know the number of requests I'm
+   * sending in the pipeline.
+   */
+  uint32_t number_of_hosts= memc->number_of_hosts;
+  memc->number_of_hosts= 1;
+  size_t max_keys= 20480;
+
+
+  char **keys= calloc(max_keys, sizeof(char*));
+  size_t *key_length=calloc(max_keys, sizeof(size_t));
+
+  /* First add all of the items.. */
+  char blob[1024]= { 0 };
+  memcached_return rc;
+  for (size_t x= 0; x < max_keys; ++x)
+  {
+    char k[251];
+    key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%zu", x);
+    keys[x]= strdup(k);
+    assert(keys[x] != NULL);
+    rc= memcached_set(memc, keys[x], key_length[x], blob, sizeof(blob), 0, 0);
+    assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+  }
+
+  /* Try to get all of them with a large multiget */
+  size_t counter= 0;
+  memcached_execute_function callbacks[1]= { [0]= &callback_counter };
+  rc= memcached_mget_execute(memc, (const char**)keys, key_length,
+                             (size_t)max_keys, callbacks, &counter, 1);
+
+  assert(rc == MEMCACHED_SUCCESS);
+  char* the_value= NULL;
+  char the_key[MEMCACHED_MAX_KEY];
+  size_t the_key_length;
+  size_t the_value_length;
+  uint32_t the_flags;
+
+  do {
+    the_value= memcached_fetch(memc, the_key, &the_key_length, &the_value_length, &the_flags, &rc);
+
+    if ((the_value!= NULL) && (rc == MEMCACHED_SUCCESS))
+    {
+      ++counter;
+      free(the_value);
+    }
+
+  } while ( (the_value!= NULL) && (rc == MEMCACHED_SUCCESS));
+
+
+  assert(rc == MEMCACHED_END);
+
+  /* Verify that we got all of the items */
+  assert(counter == max_keys);
+
+  /* Release all allocated resources */
+  for (size_t x= 0; x < max_keys; ++x)
+  {
+    free(keys[x]);
+  }
+  free(keys);
+  free(key_length);
+
+  memc->number_of_hosts= number_of_hosts;
+
+  return TEST_SUCCESS;
+}
+
+
+
+
 test_st udp_setup_server_tests[] ={
   {"set_udp_behavior_test", 0, (test_callback_fn)set_udp_behavior_test},
   {"add_tcp_server_udp_client_test", 0, (test_callback_fn)add_tcp_server_udp_client_test},
@@ -5734,6 +5854,7 @@ test_st regression_tests[]= {
   {"lp:442914", 1, (test_callback_fn)regression_bug_442914 },
   {"lp:447342", 1, (test_callback_fn)regression_bug_447342 },
   {"lp:463297", 1, (test_callback_fn)regression_bug_463297 },
+  {"lp:490486", 1, (test_callback_fn)regression_bug_490486 },
   {0, 0, (test_callback_fn)0}
 };