+ len= (size_t)snprintf(k, sizeof(k), "%037u", 251);
+ rc= memcached_delete(memc, k, len, 0);
+ test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, k, len, 0);
+ test_truth(rc == MEMCACHED_NOTFOUND);
+
+ memc->number_of_hosts= number_of_hosts;
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_447342(memcached_st *memc)
+{
+ if (memc->number_of_hosts < 3 || pre_replication(memc) != MEMCACHED_SUCCESS)
+ return TEST_SKIPPED;
+
+ memcached_return_t rc;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 2);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ const size_t max_keys= 100;
+ 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)
+ {
+ char k[251];
+ key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%u", x);
+ keys[x]= strdup(k);
+ test_truth(keys[x] != NULL);
+ rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ }
+
+ /*
+ ** We are using the quiet commands to store the replicas, so we need
+ ** to ensure that all of them are processed before we can continue.
+ ** In the test we go directly from storing the object to trying to
+ ** receive the object from all of the different servers, so we
+ ** could end up in a race condition (the memcached server hasn't yet
+ ** processed the quiet command from the replication set when it process
+ ** the request from the other client (created by the clone)). As a
+ ** workaround for that we call memcached_quit to send the quit command
+ ** to the server and wait for the response ;-) If you use the test code
+ ** as an example for your own code, please note that you shouldn't need
+ ** to do this ;-)
+ */
+ memcached_quit(memc);
+
+ /* Verify that all messages are stored, and we didn't stuff too much
+ * into the servers
+ */
+ rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ unsigned int 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);
+
+ memcached_quit(memc);
+ /*
+ * Don't do the following in your code. I am abusing the internal details
+ * within the library, and this is not a supported interface.
+ * This is to verify correct behavior in the library. Fake that two servers
+ * are dead..
+ */
+ in_port_t port0= memc->hosts[0].port;
+ in_port_t port2= memc->hosts[2].port;
+
+ memc->hosts[0].port= 0;
+ memc->hosts[2].port= 0;
+
+ rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+ test_truth(counter == (unsigned int)max_keys);
+
+ /* restore the memc handle */
+ memc->hosts[0].port= port0;
+ memc->hosts[2].port= port2;
+
+ memcached_quit(memc);
+
+ /* Remove half of the objects */
+ for (int x= 0; x < (int)max_keys; ++x)
+ {
+ if (x & 1)
+ {
+ rc= memcached_delete(memc, keys[x], key_length[x], 0);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ }
+ }
+
+ memcached_quit(memc);
+ memc->hosts[0].port= 0;
+ memc->hosts[2].port= 0;
+
+ /* now retry the command, this time we should have cache misses */
+ rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+ test_truth(counter == (unsigned int)(max_keys >> 1));
+
+ /* Release allocated resources */
+ for (size_t x= 0; x < max_keys; ++x)
+ free(keys[x]);
+ free(keys);
+ free(key_length);
+
+ /* restore the memc handle */
+ memc->hosts[0].port= port0;
+ memc->hosts[2].port= port2;
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_463297(memcached_st *memc)
+{
+ memcached_st *memc_clone= memcached_clone(NULL, memc);
+ test_truth(memc_clone != NULL);
+ test_truth(memcached_version(memc_clone) == MEMCACHED_SUCCESS);
+
+ if (memc_clone->hosts[0].major_version > 1 ||
+ (memc_clone->hosts[0].major_version == 1 &&
+ memc_clone->hosts[0].minor_version > 2))
+ {
+ /* Binary protocol doesn't support deferred delete */
+ memcached_st *bin_clone= memcached_clone(NULL, memc);
+ test_truth(bin_clone != NULL);
+ test_truth(memcached_behavior_set(bin_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) == MEMCACHED_SUCCESS);
+ test_truth(memcached_delete(bin_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
+ memcached_free(bin_clone);
+
+ memcached_quit(memc_clone);
+
+ /* If we know the server version, deferred delete should fail
+ * with invalid arguments */
+ test_truth(memcached_delete(memc_clone, "foo", 3, 1) == MEMCACHED_INVALID_ARGUMENTS);
+
+ /* If we don't know the server version, we should get a protocol error */
+ memcached_return_t rc= memcached_delete(memc, "foo", 3, 1);
+
+ /* but there is a bug in some of the memcached servers (1.4) that treats
+ * the counter as noreply so it doesn't send the proper error message
+ */
+ test_truth(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
+
+ /* And buffered mode should be disabled and we should get protocol error */
+ test_truth(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1) == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, "foo", 3, 1);
+ test_truth(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
+
+ /* Same goes for noreply... */
+ test_truth(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1) == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, "foo", 3, 1);
+ test_truth(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR);
+
+ /* but a normal request should go through (and be buffered) */
+ test_truth((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_BUFFERED);
+ test_truth(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
+
+ test_truth(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0) == MEMCACHED_SUCCESS);
+ /* unbuffered noreply should be success */
+ test_truth(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_SUCCESS);
+ /* unbuffered with reply should be not found... */
+ test_truth(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0) == MEMCACHED_SUCCESS);
+ test_truth(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_NOTFOUND);
+ }
+
+ memcached_free(memc_clone);
+ return TEST_SUCCESS;
+}
+
+
+/* Test memcached_server_get_last_disconnect
+ * For a working server set, shall be NULL
+ * For a set of non existing server, shall not be NULL
+ */
+static test_return_t test_get_last_disconnect(memcached_st *memc)
+{
+ memcached_return_t rc;
+ memcached_server_st *disconnected_server;
+
+ /* With the working set of server */
+ const char *key= "marmotte";
+ const char *value= "milka";
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ disconnected_server = memcached_server_get_last_disconnect(memc);
+ test_truth(disconnected_server == NULL);
+
+ /* With a non existing server */
+ memcached_st *mine;
+ memcached_server_st *servers;
+
+ const char *server_list= "localhost:9";
+
+ servers= memcached_servers_parse(server_list);
+ test_truth(servers);
+ mine= memcached_create(NULL);
+ rc= memcached_server_push(mine, servers);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ memcached_server_list_free(servers);
+ test_truth(mine);
+
+ rc= memcached_set(mine, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_truth(rc != MEMCACHED_SUCCESS);
+
+ disconnected_server = memcached_server_get_last_disconnect(mine);
+ test_truth(disconnected_server != NULL);
+ test_truth(disconnected_server->port == 9);
+ test_truth(strncmp(disconnected_server->hostname,"localhost",9) == 0);
+
+ memcached_quit(mine);
+ memcached_free(mine);
+
+ return TEST_SUCCESS;
+}
+
+/*
+ * This test ensures that the failure counter isn't incremented during
+ * normal termination of the memcached instance.
+ */
+static test_return_t wrong_failure_counter_test(memcached_st *memc)
+{
+ memcached_return_t rc;
+
+ /* Set value to force connection to the server */
+ const char *key= "marmotte";
+ const char *value= "milka";
+
+ /*
+ * Please note that I'm abusing the internal structures in libmemcached
+ * in a non-portable way and you shouldn't be doing this. I'm only
+ * doing this in order to verify that the library works the way it should
+ */
+ uint32_t number_of_hosts= memc->number_of_hosts;
+ memc->number_of_hosts= 1;
+
+ /* Ensure that we are connected to the server by setting a value */
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+
+ /* The test is to see that the memcached_quit doesn't increase the
+ * the server failure conter, so let's ensure that it is zero
+ * before sending quit
+ */
+ memc->hosts[0].server_failure_counter= 0;
+
+ memcached_quit(memc);
+
+ /* Verify that it memcached_quit didn't increment the failure counter
+ * Please note that this isn't bullet proof, because an error could
+ * occur...
+ */
+ test_truth(memc->hosts[0].server_failure_counter == 0);
+
+ /* restore the instance */
+ memc->number_of_hosts= number_of_hosts;
+