testing: retry check for memcp
[m6w6/libmemcached] / test / tests / bin / memcp.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/Shell.hpp"
3 #include "test/lib/Server.hpp"
4 #include "test/lib/Retry.hpp"
5 #include "test/lib/ReturnMatcher.hpp"
6
7 using Catch::Matchers::Contains;
8
9 TEST_CASE("bin/memcp") {
10 Shell sh{string{TESTING_ROOT "/../src/bin"}};
11
12 SECTION("no servers provided") {
13 string output;
14 REQUIRE_FALSE(sh.run("memcp nonexistent", output));
15 REQUIRE(output == "No Servers provided\n");
16 }
17
18 SECTION("--help") {
19 string output;
20 REQUIRE(sh.run("memcp --help", output));
21 REQUIRE_THAT(output, Contains("memcp"));
22 REQUIRE_THAT(output, Contains("v1"));
23 REQUIRE_THAT(output, Contains("help"));
24 REQUIRE_THAT(output, Contains("version"));
25 REQUIRE_THAT(output, Contains("option"));
26 REQUIRE_THAT(output, Contains("--"));
27 REQUIRE_THAT(output, Contains("="));
28 }
29
30 SECTION("with server") {
31 Server server{MEMCACHED_BINARY, {"-U", random_port_string}};
32 MemcachedPtr memc;
33 LoneReturnMatcher test{*memc};
34
35 REQUIRE(server.ensureListening());
36 auto port = get<int>(server.getSocketOrPort());
37 auto comm = "memcp --servers=localhost:" + to_string(port) + " ";
38
39 REQUIRE_SUCCESS(memcached_server_add(*memc, "localhost", port));
40
41 SECTION("okay") {
42 auto udp_buffer = GENERATE(0,1,2);
43 auto binary = GENERATE(0,1);
44 auto set_add_replace = GENERATE(0,1,2);
45 auto expire = GENERATE(0, random_num(10,12345));
46 string set_add_replace_s[3] = {
47 "set", "add", "replace"
48 };
49
50 DYNAMIC_SECTION("udp=" << (udp_buffer==1) <<" buffer=" << (udp_buffer==2) << " binary=" << binary << " mode=" << set_add_replace_s[set_add_replace] << " expire=" << expire) {
51 Tempfile temp;
52 temp.put(S("123"));
53
54 if (udp_buffer == 1) {
55 comm += " --udp ";
56 } else if (udp_buffer == 2) {
57 comm += " --buffer ";
58 }
59 if(binary) {
60 comm += " --binary ";
61 }
62 if (expire) {
63 comm += " --expire " + to_string(expire) + " ";
64 }
65 switch (set_add_replace) {
66 case 2:
67 comm += " --replace ";
68 REQUIRE_SUCCESS(memcached_set(*memc, S(temp.getFn()), S("foo"), 0, 0));
69 break;
70 case 1:
71 comm += " --add ";
72 }
73
74 INFO(comm);
75 string output;
76 auto ok = sh.run(comm + temp.getFn(), output);
77 REQUIRE(output == "");
78 REQUIRE(ok);
79
80 Retry settled{[&memc, &temp]{
81 size_t len;
82 memcached_return_t rc;
83 Malloced val(memcached_get(*memc, S(temp.getFn()), &len, nullptr, &rc));
84
85 return MEMCACHED_SUCCESS == rc && *val && string(*val, len) == "123";
86 }};
87 REQUIRE(settled());
88 }
89 }
90
91 SECTION("connection failure") {
92 server.signal(SIGKILL);
93 server.wait();
94
95 Tempfile temp;
96
97 string output;
98 REQUIRE_FALSE(sh.run(comm + temp.getFn(), output));
99 REQUIRE_THAT(output,
100 Contains("CONNECTION FAILURE")
101 || Contains("SERVER HAS FAILED")
102 || Contains("SYSTEM ERROR")
103 || Contains("TIMEOUT OCCURRED"));
104 }
105
106 SECTION("file not found") {
107 string output;
108 REQUIRE_FALSE(sh.run(comm + "nonexistent", output));
109 REQUIRE_THAT(output, Contains("No such file or directory"));
110 }
111 }
112 }