Add support for incr/decr with initial value
[awesomized/libmemcached] / tests / function.c
index 91c12f7a0d76dea85a43e3abe7d5a0887945f850..d4239a80c09c43bb54997eef074e2ff0287995a2 100644 (file)
@@ -204,6 +204,30 @@ static test_return  clone_test(memcached_st *memc)
     memcached_st *clone;
     clone= memcached_clone(NULL, memc);
     assert(clone);
+
+    assert(clone->call_free == memc->call_free);
+    assert(clone->call_malloc == memc->call_malloc);
+    assert(clone->call_realloc == memc->call_realloc);
+    assert(clone->connect_timeout == memc->connect_timeout);
+    assert(clone->delete_trigger == memc->delete_trigger);
+    assert(clone->distribution == memc->distribution);
+    assert(clone->flags == memc->flags);
+    assert(clone->get_key_failure == memc->get_key_failure);
+    assert(clone->hash == memc->hash);
+    assert(clone->hash_continuum == memc->hash_continuum);
+    assert(clone->io_bytes_watermark == memc->io_bytes_watermark);
+    assert(clone->io_msg_watermark == memc->io_msg_watermark);
+    assert(clone->on_cleanup == memc->on_cleanup);
+    assert(clone->on_clone == memc->on_clone);
+    assert(clone->poll_timeout == memc->poll_timeout);
+    assert(clone->rcv_timeout == memc->rcv_timeout);
+    assert(clone->recv_size == memc->recv_size);
+    assert(clone->retry_timeout == memc->retry_timeout);
+    assert(clone->send_size == memc->send_size);
+    assert(clone->server_failure_limit == memc->server_failure_limit);
+    assert(clone->snd_timeout == memc->snd_timeout);
+    assert(clone->user_data == memc->user_data);
+
     memcached_free(clone);
   }
 
@@ -519,11 +543,22 @@ static test_return  add_test(memcached_st *memc)
   return 0;
 }
 
+/*
+** There was a problem of leaking filedescriptors in the initial release
+** of MacOSX 10.5. This test case triggers the problem. On some Solaris
+** systems it seems that the kernel is slow on reclaiming the resources
+** because the connects starts to time out (the test doesn't do much
+** anyway, so just loop 10 iterations)
+*/
 static test_return  add_wrapper(memcached_st *memc)
 {
   unsigned int x;
+  unsigned int max= 10000;
+#ifdef __sun
+  max= 10;
+#endif
 
-  for (x= 0; x < 10000; x++)
+  for (x= 0; x < max; x++)
     add_test(memc);
 
   return 0;
@@ -926,6 +961,51 @@ static test_return  get_test4(memcached_st *memc)
   return 0;
 }
 
+/*
+ * This test verifies that memcached_read_one_response doesn't try to
+ * dereference a NIL-pointer if you issue a multi-get and don't read out all
+ * responses before you execute a storage command.
+ */
+static test_return get_test5(memcached_st *memc)
+{
+  /*
+  ** Request the same key twice, to ensure that we hash to the same server
+  ** (so that we have multiple response values queued up) ;-)
+  */
+  char *keys[]= { "key", "key" };
+  size_t lengths[]= { 3, 3 };
+  uint32_t flags;
+  size_t rlen;
+
+  memcached_return rc= memcached_set(memc, keys[0], lengths[0], 
+                                     keys[0], lengths[0], 0, 0);
+  assert(rc == MEMCACHED_SUCCESS);
+  rc= memcached_mget(memc, keys, lengths, 2);
+
+  memcached_result_st results_obj;
+  memcached_result_st *results;
+  results=memcached_result_create(memc, &results_obj);
+  assert(results);
+  results=memcached_fetch_result(memc, &results_obj, &rc);
+  assert(results);
+  memcached_result_free(&results_obj);
+
+  /* Don't read out the second result, but issue a set instead.. */
+  rc= memcached_set(memc, keys[0], lengths[0], keys[0], lengths[0], 0, 0);
+  assert(rc == MEMCACHED_SUCCESS);
+
+  char *val= memcached_get_by_key(memc, keys[0], lengths[0], "yek", 3,
+                                  &rlen, &flags, &rc);
+  assert(val == NULL);
+  assert(rc == MEMCACHED_NOTFOUND);
+  val= memcached_get(memc, keys[0], lengths[0], &rlen, &flags, &rc);
+  assert(val != NULL);
+  assert(rc == MEMCACHED_SUCCESS);
+  free(val);
+
+  return TEST_SUCCESS;
+}
+
 /* Do not copy the style of this code, I just access hosts to testthis function */
 static test_return  stats_servername_test(memcached_st *memc)
 {
@@ -963,6 +1043,28 @@ static test_return  increment_test(memcached_st *memc)
   return 0;
 }
 
+static test_return  increment_with_initial_test(memcached_st *memc)
+{
+  if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
+  {
+    uint64_t new_number;
+    memcached_return rc;
+    char *key= "number";
+    uint64_t initial= 0;
+
+    rc= memcached_increment_with_initial(memc, key, strlen(key),
+                                         1, initial, 0, &new_number);
+    assert(rc == MEMCACHED_SUCCESS);
+    assert(new_number == initial);
+
+    rc= memcached_increment_with_initial(memc, key, strlen(key),
+                                         1, initial, 0, &new_number);
+    assert(rc == MEMCACHED_SUCCESS);
+    assert(new_number == (initial + 1));
+  }
+  return 0;
+}
+
 static test_return  decrement_test(memcached_st *memc)
 {
   uint64_t new_number;
@@ -988,6 +1090,28 @@ static test_return  decrement_test(memcached_st *memc)
   return 0;
 }
 
+static test_return  decrement_with_initial_test(memcached_st *memc)
+{
+  if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) != 0)
+  {
+    uint64_t new_number;
+    memcached_return rc;
+    char *key= "number";
+    uint64_t initial= 3;
+
+    rc= memcached_decrement_with_initial(memc, key, strlen(key),
+                                         1, initial, 0, &new_number);
+    assert(rc == MEMCACHED_SUCCESS);
+    assert(new_number == initial);
+
+    rc= memcached_decrement_with_initial(memc, key, strlen(key),
+                                         1, initial, 0, &new_number);
+    assert(rc == MEMCACHED_SUCCESS);
+    assert(new_number == (initial - 1));
+  }
+  return 0;
+}
+
 static test_return  quit_test(memcached_st *memc)
 {
   memcached_return rc;
@@ -3222,6 +3346,8 @@ static memcached_return init_udp(memcached_st *memc)
   unsigned int x= 0;
   memcached_server_st servers[num_hosts];
   memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts);
+  for (x= 0; x < num_hosts; x++)
+    memcached_server_free(&memc->hosts[x]);
   memc->number_of_hosts= 0;
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
   for (x= 0; x < num_hosts; x++)
@@ -3544,9 +3670,12 @@ test_st tests[] ={
   {"get2", 0, get_test2 },
   {"get3", 0, get_test3 },
   {"get4", 0, get_test4 },
+  {"partial mget", 0, get_test5 },
   {"stats_servername", 0, stats_servername_test },
   {"increment", 0, increment_test },
+  {"increment_with_initial", 1, increment_with_initial_test },
   {"decrement", 0, decrement_test },
+  {"decrement_with_initial", 1, decrement_with_initial_test },
   {"quit", 0, quit_test },
   {"mget", 1, mget_test },
   {"mget_result", 1, mget_result_test },