+ test_truth(jenkins_values[x] == hash_val);
+ }
+
+ return TEST_SUCCESS;
+}
+
+
+static test_return_t ketama_compatibility_libmemcached(memcached_st *trash)
+{
+ memcached_return_t rc;
+ uint64_t value;
+ int x;
+ memcached_server_st *server_pool;
+ memcached_st *memc;
+
+ (void)trash;
+
+ memc= memcached_create(NULL);
+ test_truth(memc);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
+ test_truth(value == 1);
+
+ test_truth(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) == MEMCACHED_SUCCESS);
+ test_truth(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA);
+
+
+ server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
+ memcached_server_push(memc, server_pool);
+
+ /* verify that the server list was parsed okay. */
+ test_truth(memc->number_of_hosts == 8);
+ test_strcmp(server_pool[0].hostname, "10.0.1.1");
+ test_truth(server_pool[0].port == 11211);
+ test_truth(server_pool[0].weight == 600);
+ test_strcmp(server_pool[2].hostname, "10.0.1.3");
+ test_truth(server_pool[2].port == 11211);
+ test_truth(server_pool[2].weight == 200);
+ test_strcmp(server_pool[7].hostname, "10.0.1.8");
+ test_truth(server_pool[7].port == 11211);
+ test_truth(server_pool[7].weight == 100);
+
+ /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
+ * us test the boundary wraparound.
+ */
+ test_truth(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
+
+ /* verify the standard ketama set. */
+ for (x= 0; x < 99; x++)
+ {
+ uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
+ char *hostname = memc->hosts[server_idx].hostname;
+
+ test_strcmp(hostname, ketama_test_cases[x].server);
+ }
+
+ memcached_server_list_free(server_pool);
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t ketama_compatibility_spymemcached(memcached_st *trash)
+{
+ memcached_return_t rc;
+ uint64_t value;
+ int x;
+ memcached_server_st *server_pool;
+ memcached_st *memc;
+
+ (void)trash;
+
+ memc= memcached_create(NULL);
+ test_truth(memc);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
+ test_truth(value == 1);
+
+ test_truth(memcached_behavior_set_distribution(memc, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY) == MEMCACHED_SUCCESS);
+ test_truth(memcached_behavior_get_distribution(memc) == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY);
+
+ server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
+ memcached_server_push(memc, server_pool);
+
+ /* verify that the server list was parsed okay. */
+ test_truth(memc->number_of_hosts == 8);
+ test_strcmp(server_pool[0].hostname, "10.0.1.1");
+ test_truth(server_pool[0].port == 11211);
+ test_truth(server_pool[0].weight == 600);
+ test_strcmp(server_pool[2].hostname, "10.0.1.3");
+ test_truth(server_pool[2].port == 11211);
+ test_truth(server_pool[2].weight == 200);
+ test_strcmp(server_pool[7].hostname, "10.0.1.8");
+ test_truth(server_pool[7].port == 11211);
+ test_truth(server_pool[7].weight == 100);
+
+ /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
+ * us test the boundary wraparound.
+ */
+ test_truth(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
+
+ /* verify the standard ketama set. */
+ for (x= 0; x < 99; x++)
+ {
+ uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key));
+ char *hostname = memc->hosts[server_idx].hostname;
+ test_strcmp(hostname, ketama_test_cases_spy[x].server);
+ }
+
+ memcached_server_list_free(server_pool);
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_434484(memcached_st *memc)
+{
+ test_return_t test_rc;
+ test_rc= pre_binary(memc);
+
+ if (test_rc != TEST_SUCCESS)
+ return test_rc;
+
+ memcached_return_t ret;
+ const char *key= "regression_bug_434484";
+ size_t keylen= strlen(key);
+
+ ret= memcached_append(memc, key, keylen, key, keylen, 0, 0);
+ test_truth(ret == MEMCACHED_NOTSTORED);
+
+ size_t size= 2048 * 1024;
+ void *data= calloc(1, size);
+ test_truth(data != NULL);
+ ret= memcached_set(memc, key, keylen, data, size, 0, 0);
+ test_truth(ret == MEMCACHED_E2BIG);
+ free(data);
+
+ return TEST_SUCCESS;
+}
+
+static test_return_t regression_bug_434843(memcached_st *memc)
+{
+ test_return_t test_rc;
+ test_rc= pre_binary(memc);
+
+ if (test_rc != TEST_SUCCESS)
+ return test_rc;
+
+ memcached_return_t rc;
+ unsigned int counter= 0;
+ memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
+
+ /*
+ * I only want to hit only _one_ server so I know the number of requests I'm
+ * sending in the pipleine to the server. Let's try to do a multiget of
+ * 1024 (that should satisfy most users don't you think?). Future versions
+ * will include a mget_execute function call if you need a higher number.
+ */
+ uint32_t number_of_hosts= memc->number_of_hosts;
+ memc->number_of_hosts= 1;
+ const size_t max_keys= 1024;
+ 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);
+ }
+
+ /*
+ * 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)
+ {
+ 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)
+ {
+ rc= memcached_add(memc, keys[x], key_length[x],
+ blob, sizeof(blob), 0, 0);
+ test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+ }
+ else
+ {
+ /* Verify that we received all of the key/value pairs */
+ test_truth(counter == (unsigned int)max_keys);
+ }
+ }
+
+ /* Release 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;
+}
+
+static test_return_t regression_bug_434843_buffered(memcached_st *memc)
+{
+ memcached_return_t rc;
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ return regression_bug_434843(memc);
+}
+
+static test_return_t regression_bug_421108(memcached_st *memc)
+{
+ memcached_return_t rc;
+ memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc);
+ test_truth(rc == MEMCACHED_SUCCESS);
+
+ char *bytes= memcached_stat_get_value(memc, memc_stat, "bytes", &rc);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ test_truth(bytes != NULL);
+ char *bytes_read= memcached_stat_get_value(memc, memc_stat,
+ "bytes_read", &rc);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ test_truth(bytes_read != NULL);
+
+ char *bytes_written= memcached_stat_get_value(memc, memc_stat,
+ "bytes_written", &rc);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ test_truth(bytes_written != NULL);
+
+ test_truth(strcmp(bytes, bytes_read) != 0);
+ test_truth(strcmp(bytes, bytes_written) != 0);
+
+ /* Release allocated resources */
+ free(bytes);
+ free(bytes_read);
+ free(bytes_written);
+ memcached_stat_free(NULL, memc_stat);
+
+ return TEST_SUCCESS;
+}
+
+/*
+ * The test case isn't obvious so I should probably document why
+ * it works the way it does. Bug 442914 was caused by a bug
+ * in the logic in memcached_purge (it did not handle the case
+ * where the number of bytes sent was equal to the watermark).
+ * In this test case, create messages so that we hit that case
+ * and then disable noreply mode and issue a new command to
+ * verify that it isn't stuck. If we change the format for the
+ * delete command or the watermarks, we need to update this
+ * test....
+ */
+static test_return_t regression_bug_442914(memcached_st *memc)
+{
+ memcached_return_t rc;
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1);
+ test_truth(rc == MEMCACHED_SUCCESS);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1);
+
+ uint32_t number_of_hosts= memc->number_of_hosts;
+ memc->number_of_hosts= 1;
+
+ char k[250];
+ size_t len;
+
+ for (int x= 0; x < 250; ++x)
+ {
+ len= (size_t)snprintf(k, sizeof(k), "%0250u", x);
+ rc= memcached_delete(memc, k, len, 0);
+ test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ 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);