+ uint32_t number_of_hosts= memcached_server_count(memc);
+ memc->number_of_hosts= 1;
+
+ char k[250];
+ size_t len;
+
+ for (uint32_t x= 0; x < 250; ++x)
+ {
+ len= (size_t)snprintf(k, sizeof(k), "%0250u", x);
+ rc= memcached_delete(memc, k, len, 0);
+ test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ (void)snprintf(k, sizeof(k), "%037u", 251U);
+ len= strlen(k);
+
+ rc= memcached_delete(memc, k, len, 0);
+ test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0);
+ test_true(rc == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, k, len, 0);
+ test_true(rc == MEMCACHED_NOTFOUND);
+
+ memc->number_of_hosts= number_of_hosts;
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_447342(memcached_st *memc)
+{
+ memcached_server_instance_st instance_one;
+ memcached_server_instance_st instance_two;
+
+ if (memcached_server_count(memc) < 3 || pre_replication(memc) != TEST_SUCCESS)
+ return TEST_SKIPPED;
+
+ memcached_return_t rc;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 2);
+ test_true(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 (size_t x= 0; x < max_keys; ++x)
+ {
+ char k[251];
+
+ key_length[x]= (size_t)snprintf(k, sizeof(k), "0200%lu", (unsigned long)x);
+ keys[x]= strdup(k);
+ test_true(keys[x] != NULL);
+ rc= memcached_set(memc, k, key_length[x], k, key_length[x], 0, 0);
+ test_true(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_true(rc == MEMCACHED_SUCCESS);
+
+ 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_true(counter == 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..
+ */
+ instance_one= memcached_server_instance_by_position(memc, 0);
+ instance_two= memcached_server_instance_by_position(memc, 2);
+ in_port_t port0= instance_one->port;
+ in_port_t port2= instance_two->port;
+
+ ((memcached_server_write_instance_st)instance_one)->port= 0;
+ ((memcached_server_write_instance_st)instance_two)->port= 0;
+
+ rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+ test_true(counter == (unsigned int)max_keys);
+
+ /* restore the memc handle */
+ ((memcached_server_write_instance_st)instance_one)->port= port0;
+ ((memcached_server_write_instance_st)instance_two)->port= port2;
+
+ memcached_quit(memc);
+
+ /* Remove half of the objects */
+ for (size_t x= 0; x < max_keys; ++x)
+ {
+ if (x & 1)
+ {
+ rc= memcached_delete(memc, keys[x], key_length[x], 0);
+ test_true(rc == MEMCACHED_SUCCESS);
+ }
+ }
+
+ memcached_quit(memc);
+ ((memcached_server_write_instance_st)instance_one)->port= 0;
+ ((memcached_server_write_instance_st)instance_two)->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_true(rc == MEMCACHED_SUCCESS);
+
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+ test_true(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 */
+ ((memcached_server_write_instance_st)instance_one)->port= port0;
+ ((memcached_server_write_instance_st)instance_two)->port= port2;
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_463297(memcached_st *memc)
+{
+ memcached_st *memc_clone= memcached_clone(NULL, memc);
+ test_true(memc_clone != NULL);
+ test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS);
+
+ memcached_server_instance_st instance=
+ memcached_server_instance_by_position(memc_clone, 0);
+
+ if (instance->major_version > 1 ||
+ (instance->major_version == 1 &&
+ instance->minor_version > 2))
+ {
+ /* Binary protocol doesn't support deferred delete */
+ memcached_st *bin_clone= memcached_clone(NULL, memc);
+ test_true(bin_clone != NULL);
+ test_true(memcached_behavior_set(bin_clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) == MEMCACHED_SUCCESS);
+ test_true(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_true(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_true_got(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR || rc == MEMCACHED_INVALID_ARGUMENTS, memcached_strerror(NULL, rc));
+
+ /* And buffered mode should be disabled and we should get protocol error */
+ test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1) == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, "foo", 3, 1);
+ test_true_got(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR || rc == MEMCACHED_INVALID_ARGUMENTS, memcached_strerror(NULL, rc));
+
+ /* Same goes for noreply... */
+ test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1) == MEMCACHED_SUCCESS);
+ rc= memcached_delete(memc, "foo", 3, 1);
+ test_true_got(rc == MEMCACHED_PROTOCOL_ERROR || rc == MEMCACHED_NOTFOUND || rc == MEMCACHED_CLIENT_ERROR || rc == MEMCACHED_INVALID_ARGUMENTS, memcached_strerror(NULL, rc));
+
+ /* but a normal request should go through (and be buffered) */
+ test_true((rc= memcached_delete(memc, "foo", 3, 0)) == MEMCACHED_BUFFERED);
+ test_true(memcached_flush_buffers(memc) == MEMCACHED_SUCCESS);
+
+ test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0) == MEMCACHED_SUCCESS);
+ /* unbuffered noreply should be success */
+ test_true(memcached_delete(memc, "foo", 3, 0) == MEMCACHED_SUCCESS);
+ /* unbuffered with reply should be not found... */
+ test_true(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 0) == MEMCACHED_SUCCESS);
+ test_true(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_instance_st disconnected_server;
+
+ /* With the working set of server */
+ const char *key= "marmotte";
+ const char *value= "milka";
+
+ memcached_reset_last_disconnected_server(memc);
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ disconnected_server = memcached_server_get_last_disconnect(memc);
+ test_true(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_true(servers);
+ mine= memcached_create(NULL);
+ rc= memcached_server_push(mine, servers);
+ test_true(rc == MEMCACHED_SUCCESS);
+ memcached_server_list_free(servers);
+ test_true(mine);
+
+ rc= memcached_set(mine, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_true(rc != MEMCACHED_SUCCESS);
+
+ disconnected_server= memcached_server_get_last_disconnect(mine);
+ if (disconnected_server == NULL)
+ {
+ fprintf(stderr, "RC %s\n", memcached_strerror(mine, rc));
+ abort();
+ }
+ test_true(disconnected_server != NULL);
+ test_true(memcached_server_port(disconnected_server)== 9);
+ test_true(strncmp(memcached_server_name(disconnected_server),"localhost",9) == 0);
+
+ memcached_quit(mine);
+ memcached_free(mine);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t test_verbosity(memcached_st *memc)
+{
+ memcached_verbosity(memc, 3);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t test_server_failure(memcached_st *memc)
+{
+ memcached_st *local_memc;
+ memcached_server_instance_st instance= memcached_server_instance_by_position(memc, 0);
+
+ local_memc= memcached_create(NULL);
+
+ memcached_server_add(local_memc, memcached_server_name(instance), memcached_server_port(instance));
+ memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 2);
+
+ uint32_t server_count= memcached_server_count(local_memc);
+
+ test_true(server_count == 1);
+
+ // Disable the server
+ instance= memcached_server_instance_by_position(local_memc, 0);
+ ((memcached_server_write_instance_st)instance)->server_failure_counter= 2;
+
+ memcached_return_t rc;
+ rc= memcached_set(local_memc, "foo", strlen("foo"),
+ NULL, 0,
+ (time_t)0, (uint32_t)0);
+ test_true(rc == MEMCACHED_SERVER_MARKED_DEAD);
+
+ ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
+ rc= memcached_set(local_memc, "foo", strlen("foo"),
+ NULL, 0,
+ (time_t)0, (uint32_t)0);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+
+ memcached_free(local_memc);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t test_cull_servers(memcached_st *memc)
+{
+ uint32_t count = memcached_server_count(memc);
+
+ // Do not do this in your code, it is not supported.
+ memc->servers[1].options.is_dead= true;
+ memc->state.is_time_for_rebuild= true;
+
+ uint32_t new_count= memcached_server_count(memc);
+ test_true(count == new_count);
+
+#if 0
+ test_true(count == new_count + 1 );
+#endif
+
+ return TEST_SUCCESS;
+}
+
+
+static memcached_return_t stat_printer(memcached_server_instance_st server,
+ const char *key, size_t key_length,
+ const char *value, size_t value_length,
+ void *context)
+{
+ (void)server;
+ (void)context;
+ (void)key;
+ (void)key_length;
+ (void)value;
+ (void)value_length;
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return_t memcached_stat_execute_test(memcached_st *memc)
+{
+ memcached_return_t rc= memcached_stat_execute(memc, NULL, stat_printer, NULL);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_stat_execute(memc, "slabs", stat_printer, NULL);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_stat_execute(memc, "items", stat_printer, NULL);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_stat_execute(memc, "sizes", stat_printer, NULL);
+ test_true(rc == MEMCACHED_SUCCESS);
+
+ 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;
+ memcached_server_instance_st instance;
+
+ /* 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= memcached_server_count(memc);
+ 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_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+
+ instance= memcached_server_instance_by_position(memc, 0);
+ /* 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
+ */
+ ((memcached_server_write_instance_st)instance)->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_true(instance->server_failure_counter == 0);
+
+ /* restore the instance */
+ memc->number_of_hosts= number_of_hosts;
+
+ return TEST_SUCCESS;
+}
+
+/*
+ * This tests ensures expected disconnections (for some behavior changes
+ * for instance) do not wrongly increase failure counter
+ */
+static test_return_t wrong_failure_counter_two_test(memcached_st *memc)
+{
+ memcached_return rc;
+
+ memcached_st *memc_clone;
+ memc_clone= memcached_clone(NULL, memc);
+ test_true(memc_clone);
+
+ /* Set value to force connection to the server */
+ const char *key= "marmotte";
+ const char *value= "milka";
+ char *string = NULL;
+ size_t string_length;
+ uint32_t flags;
+
+ rc= memcached_set(memc_clone, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ test_true_got(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED, memcached_strerror(NULL, rc));