Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

CCBC-107 Port test from http://review.couchbase.org/13667

Reference: http://review.couchbase.org/#/c/13667/12/tests/smoke-test.c
Change-Id: Ia2fb0c99dd3759ed56bd9a508e3810bc45388457
Reviewed-on: http://review.couchbase.org/22585
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Mordechai Nunberg <mnunberg@haskalah.org>
  • Loading branch information...
commit d5cbb27f84440d46595a1553312ca45c636f071a 1 parent f598e54
Sergey Avseyev avsej authored
5 Makefile.am
@@ -307,6 +307,11 @@ tests_unit_tests_SOURCES += \
307 307 tests/testutil.cc \
308 308 tests/testutil.h
309 309
  310 +if !HAVE_GETHRTIME
  311 +tests_unit_tests_SOURCES += \
  312 + src/gethrtime.c
  313 +endif
  314 +
310 315 check_PROGRAMS += \
311 316 tests/smoke-test
312 317 endif
9 tests/mock-environment.cc
@@ -20,6 +20,7 @@
20 20
21 21 #include "server.h"
22 22 #include "mock-environment.h"
  23 +#include <sstream>
23 24
24 25 MockEnvironment *MockEnvironment::instance;
25 26
@@ -65,6 +66,14 @@ void MockEnvironment::respawnNode(int index, std::string bucket)
65 66 assert(nw == cmd.size());
66 67 }
67 68
  69 +void MockEnvironment::hiccupNodes(int msecs, int offset)
  70 +{
  71 + std::stringstream cmdbuf;
  72 + cmdbuf << "hiccup," << msecs << "," << offset << "\n";
  73 + std::string cmd = cmdbuf.str();
  74 + ssize_t nw = send(mock->client, cmd.c_str(), cmd.size(), 0);
  75 + assert(nw == cmd.size());
  76 +}
68 77 void MockEnvironment::createConnection(lcb_t &instance)
69 78 {
70 79 struct lcb_io_opt_st *io;
10 tests/mock-environment.h
@@ -102,6 +102,16 @@ class MockEnvironment : public ::testing::Environment
102 102 */
103 103 void createConnection(lcb_t &instance);
104 104
  105 + /**
  106 + * Setup mock to split response in two parts: send first "offset" bytes
  107 + * immediately and send the rest after "msecs" milliseconds.
  108 + *
  109 + * @param msecs the number of milliseconds to wait before sending the
  110 + * rest of the packet.
  111 + * @param offset the number of the bytes to send in first before delay
  112 + */
  113 + void hiccupNodes(int msecs, int offset);
  114 +
105 115 ServerVersion getServerVersion(void) const {
106 116 return serverVersion;
107 117 }
95 tests/mock-unit-test.cc
@@ -348,6 +348,7 @@ extern "C" {
348 348 lcb_cas_t cas2;
349 349 char *bytes;
350 350 lcb_size_t nbytes;
  351 + lcb_int32_t counter;
351 352 };
352 353
353 354 static void df_store_callback1(lcb_t instance,
@@ -467,6 +468,13 @@ extern "C" {
467 468
468 469 int store_cnt;
469 470
  471 + /* Needed for "testPurgedBody", to ensure preservation of connection */
  472 + static void io_close_wrap(lcb_io_opt_t, lcb_socket_t)
  473 + {
  474 + fprintf(stderr, "We requested to close, but we were't expecting it\n");
  475 + abort();
  476 + }
  477 +
470 478 static void store_callback(lcb_t instance, const void *cookie,
471 479 lcb_storage_t, lcb_error_t error,
472 480 const lcb_store_resp_t *)
@@ -477,7 +485,6 @@ extern "C" {
477 485 instance->io->v.v0.stop_event_loop(instance->io);
478 486 }
479 487
480   -
481 488 static void get_callback(lcb_t instance, const void *cookie,
482 489 lcb_error_t error, const lcb_get_resp_t *resp)
483 490 {
@@ -488,6 +495,92 @@ extern "C" {
488 495 rv->nbytes = resp->v.v0.nbytes;
489 496 instance->io->v.v0.stop_event_loop(instance->io);
490 497 }
  498 +
  499 + static void tpb_get_callback(lcb_t instance, const void *cookie,
  500 + lcb_error_t error, const lcb_get_resp_t *resp)
  501 + {
  502 + struct rvbuf *rv = (struct rvbuf *)cookie;
  503 + rv->error = error;
  504 + rv->counter--;
  505 + if (rv->counter <= 0) {
  506 + lcb_io_opt_t io = (lcb_io_opt_t)lcb_get_cookie(instance);
  507 + assert(io);
  508 + io->v.v0.stop_event_loop(io);
  509 + }
  510 + }
  511 +}
  512 +
  513 +TEST_F(MockUnitTest, testPurgedBody)
  514 +{
  515 + lcb_error_t err;
  516 + struct rvbuf rv;
  517 + const char key[] = "testPurgedBody";
  518 + lcb_size_t nkey = sizeof(key);
  519 + int nvalue = 100000;
  520 + char *value = (char *)malloc(nvalue);
  521 + lcb_t instance;
  522 + lcb_io_opt_t io;
  523 +
  524 + createConnection(instance);
  525 +
  526 + lcb_uint32_t old_timeo = lcb_get_timeout(instance);
  527 + io = (lcb_io_opt_t)lcb_get_cookie(instance);
  528 +
  529 + /* --enable-warnings --enable-werror won't let me use a simple void* */
  530 + void (*io_close_old)(lcb_io_opt_t, lcb_socket_t) = io->v.v0.close;
  531 +
  532 + lcb_set_timeout(instance, 3100000); /* 3.1 seconds */
  533 + hrtime_t now = gethrtime(), begin_time = 0;
  534 +
  535 + io->v.v0.close = io_close_wrap;
  536 +
  537 + memset(value, 0xff, nvalue);
  538 + lcb_set_store_callback(instance, store_callback);
  539 + lcb_set_get_callback(instance, tpb_get_callback);
  540 +
  541 + /*
  542 + * Store large 100000 bytes key in the bucket
  543 + */
  544 + lcb_store_cmd_t store_cmd(LCB_SET, key, nkey, value, nvalue);
  545 + lcb_store_cmd_t *store_cmds[] = { &store_cmd };
  546 + err = lcb_store(instance, &rv, 1, store_cmds);
  547 + ASSERT_EQ(LCB_SUCCESS, err);
  548 + rv.counter = 1;
  549 + io->v.v0.run_event_loop(io);
  550 + ASSERT_EQ(LCB_SUCCESS, rv.error);
  551 +
  552 + /*
  553 + * Schedule GET operation for the key
  554 + */
  555 + lcb_get_cmd_t get_cmd(key, nkey);
  556 + lcb_get_cmd_t *get_cmds[] = { &get_cmd };
  557 + err = lcb_get(instance, &rv, 1, get_cmds);
  558 + ASSERT_EQ(LCB_SUCCESS, err);
  559 +
  560 + /*
  561 + * Setup mock to split response in two parts: send first 40 bytes
  562 + * immediately and send the rest after 3.5 seconds.
  563 + *
  564 + * And sleep a bit (for 0.25 seconds)
  565 + */
  566 + MockEnvironment::getInstance()->hiccupNodes(3500, 40); /* 3.5 seconds */
  567 + usleep(250000L); /* 0.25 seconds */
  568 +
  569 + /*
  570 + * Run the IO loop and measure how long does it take to transfer the
  571 + * value back.
  572 + */
  573 + begin_time = gethrtime();
  574 + io->v.v0.run_event_loop(io);
  575 + now = gethrtime();
  576 +
  577 + /*
  578 + * The command must be timed out and it should take more than 2.8
  579 + * seconds
  580 + */
  581 + ASSERT_EQ(LCB_ETIMEDOUT, rv.error);
  582 + ASSERT_GE(now - begin_time, 2800000000L); /* 2.8 seconds */
  583 + free(value);
491 584 }
492 585
493 586 TEST_F(MockUnitTest, testReconfigurationOnNodeFailover)

0 comments on commit d5cbb27

Please sign in to comment.
Something went wrong with that request. Please try again.