aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/core/iomgr/timer_heap_test.c (renamed from test/core/iomgr/alarm_heap_test.c)118
-rw-r--r--test/core/iomgr/timer_list_test.c (renamed from test/core/iomgr/alarm_list_test.c)56
-rw-r--r--test/core/surface/alarm_test.c100
-rw-r--r--test/cpp/common/alarm_test.cc91
4 files changed, 278 insertions, 87 deletions
diff --git a/test/core/iomgr/alarm_heap_test.c b/test/core/iomgr/timer_heap_test.c
index 13bf7e43f6..941f61c5c7 100644
--- a/test/core/iomgr/alarm_heap_test.c
+++ b/test/core/iomgr/timer_heap_test.c
@@ -31,7 +31,7 @@
*
*/
-#include "src/core/iomgr/alarm_heap.h"
+#include "src/core/iomgr/timer_heap.h"
#include <stdlib.h>
#include <string.h>
@@ -48,8 +48,8 @@ static gpr_timespec random_deadline(void) {
return ts;
}
-static grpc_alarm *create_test_elements(size_t num_elements) {
- grpc_alarm *elems = gpr_malloc(num_elements * sizeof(grpc_alarm));
+static grpc_timer *create_test_elements(size_t num_elements) {
+ grpc_timer *elems = gpr_malloc(num_elements * sizeof(grpc_timer));
size_t i;
for (i = 0; i < num_elements; i++) {
elems[i].deadline = random_deadline();
@@ -63,17 +63,17 @@ static int cmp_elem(const void *a, const void *b) {
return i - j;
}
-static size_t *all_top(grpc_alarm_heap *pq, size_t *n) {
+static size_t *all_top(grpc_timer_heap *pq, size_t *n) {
size_t *vec = NULL;
size_t *need_to_check_children;
size_t num_need_to_check_children = 0;
*n = 0;
- if (pq->alarm_count == 0) return vec;
+ if (pq->timer_count == 0) return vec;
need_to_check_children =
- gpr_malloc(pq->alarm_count * sizeof(*need_to_check_children));
+ gpr_malloc(pq->timer_count * sizeof(*need_to_check_children));
need_to_check_children[num_need_to_check_children++] = 0;
- vec = gpr_malloc(pq->alarm_count * sizeof(*vec));
+ vec = gpr_malloc(pq->timer_count * sizeof(*vec));
while (num_need_to_check_children > 0) {
size_t ind = need_to_check_children[0];
size_t leftchild, rightchild;
@@ -82,15 +82,15 @@ static size_t *all_top(grpc_alarm_heap *pq, size_t *n) {
num_need_to_check_children * sizeof(*need_to_check_children));
vec[(*n)++] = ind;
leftchild = 1u + 2u * ind;
- if (leftchild < pq->alarm_count) {
- if (gpr_time_cmp(pq->alarms[leftchild]->deadline,
- pq->alarms[ind]->deadline) >= 0) {
+ if (leftchild < pq->timer_count) {
+ if (gpr_time_cmp(pq->timers[leftchild]->deadline,
+ pq->timers[ind]->deadline) >= 0) {
need_to_check_children[num_need_to_check_children++] = leftchild;
}
rightchild = leftchild + 1;
- if (rightchild < pq->alarm_count &&
- gpr_time_cmp(pq->alarms[rightchild]->deadline,
- pq->alarms[ind]->deadline) >= 0) {
+ if (rightchild < pq->timer_count &&
+ gpr_time_cmp(pq->timers[rightchild]->deadline,
+ pq->timers[ind]->deadline) >= 0) {
need_to_check_children[num_need_to_check_children++] = rightchild;
}
}
@@ -101,7 +101,7 @@ static size_t *all_top(grpc_alarm_heap *pq, size_t *n) {
return vec;
}
-static void check_pq_top(grpc_alarm *elements, grpc_alarm_heap *pq,
+static void check_pq_top(grpc_timer *elements, grpc_timer_heap *pq,
gpr_uint8 *inpq, size_t num_elements) {
gpr_timespec max_deadline = gpr_inf_past(GPR_CLOCK_REALTIME);
size_t *max_deadline_indices =
@@ -130,45 +130,45 @@ static void check_pq_top(grpc_alarm *elements, grpc_alarm_heap *pq,
gpr_free(top_elements);
}
-static int contains(grpc_alarm_heap *pq, grpc_alarm *el) {
+static int contains(grpc_timer_heap *pq, grpc_timer *el) {
size_t i;
- for (i = 0; i < pq->alarm_count; i++) {
- if (pq->alarms[i] == el) return 1;
+ for (i = 0; i < pq->timer_count; i++) {
+ if (pq->timers[i] == el) return 1;
}
return 0;
}
-static void check_valid(grpc_alarm_heap *pq) {
+static void check_valid(grpc_timer_heap *pq) {
size_t i;
- for (i = 0; i < pq->alarm_count; ++i) {
+ for (i = 0; i < pq->timer_count; ++i) {
size_t left_child = 1u + 2u * i;
size_t right_child = left_child + 1u;
- if (left_child < pq->alarm_count) {
- GPR_ASSERT(gpr_time_cmp(pq->alarms[i]->deadline,
- pq->alarms[left_child]->deadline) >= 0);
+ if (left_child < pq->timer_count) {
+ GPR_ASSERT(gpr_time_cmp(pq->timers[i]->deadline,
+ pq->timers[left_child]->deadline) >= 0);
}
- if (right_child < pq->alarm_count) {
- GPR_ASSERT(gpr_time_cmp(pq->alarms[i]->deadline,
- pq->alarms[right_child]->deadline) >= 0);
+ if (right_child < pq->timer_count) {
+ GPR_ASSERT(gpr_time_cmp(pq->timers[i]->deadline,
+ pq->timers[right_child]->deadline) >= 0);
}
}
}
static void test1(void) {
- grpc_alarm_heap pq;
+ grpc_timer_heap pq;
const size_t num_test_elements = 200;
const size_t num_test_operations = 10000;
size_t i;
- grpc_alarm *test_elements = create_test_elements(num_test_elements);
+ grpc_timer *test_elements = create_test_elements(num_test_elements);
gpr_uint8 *inpq = gpr_malloc(num_test_elements);
- grpc_alarm_heap_init(&pq);
+ grpc_timer_heap_init(&pq);
memset(inpq, 0, num_test_elements);
- GPR_ASSERT(grpc_alarm_heap_is_empty(&pq));
+ GPR_ASSERT(grpc_timer_heap_is_empty(&pq));
check_valid(&pq);
for (i = 0; i < num_test_elements; ++i) {
GPR_ASSERT(!contains(&pq, &test_elements[i]));
- grpc_alarm_heap_add(&pq, &test_elements[i]);
+ grpc_timer_heap_add(&pq, &test_elements[i]);
check_valid(&pq);
GPR_ASSERT(contains(&pq, &test_elements[i]));
inpq[i] = 1;
@@ -180,24 +180,24 @@ static void test1(void) {
GPR_ASSERT(contains(&pq, &test_elements[i]));
}
- GPR_ASSERT(pq.alarm_count == num_test_elements);
+ GPR_ASSERT(pq.timer_count == num_test_elements);
check_pq_top(test_elements, &pq, inpq, num_test_elements);
for (i = 0; i < num_test_operations; ++i) {
size_t elem_num = (size_t)rand() % num_test_elements;
- grpc_alarm *el = &test_elements[elem_num];
+ grpc_timer *el = &test_elements[elem_num];
if (!inpq[elem_num]) { /* not in pq */
GPR_ASSERT(!contains(&pq, el));
el->deadline = random_deadline();
- grpc_alarm_heap_add(&pq, el);
+ grpc_timer_heap_add(&pq, el);
GPR_ASSERT(contains(&pq, el));
inpq[elem_num] = 1;
check_pq_top(test_elements, &pq, inpq, num_test_elements);
check_valid(&pq);
} else {
GPR_ASSERT(contains(&pq, el));
- grpc_alarm_heap_remove(&pq, el);
+ grpc_timer_heap_remove(&pq, el);
GPR_ASSERT(!contains(&pq, el));
inpq[elem_num] = 0;
check_pq_top(test_elements, &pq, inpq, num_test_elements);
@@ -205,66 +205,66 @@ static void test1(void) {
}
}
- grpc_alarm_heap_destroy(&pq);
+ grpc_timer_heap_destroy(&pq);
gpr_free(test_elements);
gpr_free(inpq);
}
static void shrink_test(void) {
- grpc_alarm_heap pq;
+ grpc_timer_heap pq;
size_t i;
size_t expected_size;
/* A large random number to allow for multiple shrinkages, at least 512. */
const size_t num_elements = (size_t)rand() % 2000 + 512;
- grpc_alarm_heap_init(&pq);
+ grpc_timer_heap_init(&pq);
/* Create a priority queue with many elements. Make sure the Size() is
correct. */
for (i = 0; i < num_elements; ++i) {
- GPR_ASSERT(i == pq.alarm_count);
- grpc_alarm_heap_add(&pq, create_test_elements(1));
+ GPR_ASSERT(i == pq.timer_count);
+ grpc_timer_heap_add(&pq, create_test_elements(1));
}
- GPR_ASSERT(num_elements == pq.alarm_count);
+ GPR_ASSERT(num_elements == pq.timer_count);
/* Remove elements until the Size is 1/4 the original size. */
- while (pq.alarm_count > num_elements / 4) {
- grpc_alarm *const te = pq.alarms[pq.alarm_count - 1];
- grpc_alarm_heap_remove(&pq, te);
+ while (pq.timer_count > num_elements / 4) {
+ grpc_timer *const te = pq.timers[pq.timer_count - 1];
+ grpc_timer_heap_remove(&pq, te);
gpr_free(te);
}
- GPR_ASSERT(num_elements / 4 == pq.alarm_count);
+ GPR_ASSERT(num_elements / 4 == pq.timer_count);
/* Expect that Capacity is in the right range:
Size * 2 <= Capacity <= Size * 4 */
- GPR_ASSERT(pq.alarm_count * 2 <= pq.alarm_capacity);
- GPR_ASSERT(pq.alarm_capacity <= pq.alarm_count * 4);
+ GPR_ASSERT(pq.timer_count * 2 <= pq.timer_capacity);
+ GPR_ASSERT(pq.timer_capacity <= pq.timer_count * 4);
check_valid(&pq);
/* Remove the rest of the elements. Check that the Capacity is not more than
4 times the Size and not less than 2 times, but never goes below 16. */
- expected_size = pq.alarm_count;
- while (pq.alarm_count > 0) {
- const size_t which = (size_t)rand() % pq.alarm_count;
- grpc_alarm *te = pq.alarms[which];
- grpc_alarm_heap_remove(&pq, te);
+ expected_size = pq.timer_count;
+ while (pq.timer_count > 0) {
+ const size_t which = (size_t)rand() % pq.timer_count;
+ grpc_timer *te = pq.timers[which];
+ grpc_timer_heap_remove(&pq, te);
gpr_free(te);
expected_size--;
- GPR_ASSERT(expected_size == pq.alarm_count);
- GPR_ASSERT(pq.alarm_count * 2 <= pq.alarm_capacity);
- if (pq.alarm_count >= 8) {
- GPR_ASSERT(pq.alarm_capacity <= pq.alarm_count * 4);
+ GPR_ASSERT(expected_size == pq.timer_count);
+ GPR_ASSERT(pq.timer_count * 2 <= pq.timer_capacity);
+ if (pq.timer_count >= 8) {
+ GPR_ASSERT(pq.timer_capacity <= pq.timer_count * 4);
} else {
- GPR_ASSERT(16 <= pq.alarm_capacity);
+ GPR_ASSERT(16 <= pq.timer_capacity);
}
check_valid(&pq);
}
- GPR_ASSERT(0 == pq.alarm_count);
- GPR_ASSERT(pq.alarm_capacity >= 16 && pq.alarm_capacity < 32);
+ GPR_ASSERT(0 == pq.timer_count);
+ GPR_ASSERT(pq.timer_capacity >= 16 && pq.timer_capacity < 32);
- grpc_alarm_heap_destroy(&pq);
+ grpc_timer_heap_destroy(&pq);
}
int main(int argc, char **argv) {
diff --git a/test/core/iomgr/alarm_list_test.c b/test/core/iomgr/timer_list_test.c
index 6656a8fa3b..5a2d5b5a17 100644
--- a/test/core/iomgr/alarm_list_test.c
+++ b/test/core/iomgr/timer_list_test.c
@@ -31,11 +31,11 @@
*
*/
-#include "src/core/iomgr/alarm.h"
+#include "src/core/iomgr/timer.h"
#include <string.h>
-#include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/timer_internal.h"
#include <grpc/support/log.h>
#include "test/core/util/test_config.h"
@@ -50,29 +50,29 @@ static void cb(grpc_exec_ctx *exec_ctx, void *arg, int success) {
static void add_test(void) {
gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME);
int i;
- grpc_alarm alarms[20];
+ grpc_timer timers[20];
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_alarm_list_init(start);
+ grpc_timer_list_init(start);
memset(cb_called, 0, sizeof(cb_called));
- /* 10 ms alarms. will expire in the current epoch */
+ /* 10 ms timers. will expire in the current epoch */
for (i = 0; i < 10; i++) {
- grpc_alarm_init(&exec_ctx, &alarms[i],
+ grpc_timer_init(&exec_ctx, &timers[i],
gpr_time_add(start, gpr_time_from_millis(10, GPR_TIMESPAN)),
cb, (void *)(gpr_intptr)i, start);
}
- /* 1010 ms alarms. will expire in the next epoch */
+ /* 1010 ms timers. will expire in the next epoch */
for (i = 10; i < 20; i++) {
- grpc_alarm_init(
- &exec_ctx, &alarms[i],
+ grpc_timer_init(
+ &exec_ctx, &timers[i],
gpr_time_add(start, gpr_time_from_millis(1010, GPR_TIMESPAN)), cb,
(void *)(gpr_intptr)i, start);
}
- /* collect alarms. Only the first batch should be ready. */
- GPR_ASSERT(10 == grpc_alarm_check(&exec_ctx,
+ /* collect timers. Only the first batch should be ready. */
+ GPR_ASSERT(10 == grpc_timer_check(&exec_ctx,
gpr_time_add(start, gpr_time_from_millis(
500, GPR_TIMESPAN)),
NULL));
@@ -82,7 +82,7 @@ static void add_test(void) {
GPR_ASSERT(cb_called[i][0] == 0);
}
- GPR_ASSERT(0 == grpc_alarm_check(&exec_ctx,
+ GPR_ASSERT(0 == grpc_timer_check(&exec_ctx,
gpr_time_add(start, gpr_time_from_millis(
600, GPR_TIMESPAN)),
NULL));
@@ -92,8 +92,8 @@ static void add_test(void) {
GPR_ASSERT(cb_called[i][0] == 0);
}
- /* collect the rest of the alarms */
- GPR_ASSERT(10 == grpc_alarm_check(
+ /* collect the rest of the timers */
+ GPR_ASSERT(10 == grpc_timer_check(
&exec_ctx, gpr_time_add(start, gpr_time_from_millis(
1500, GPR_TIMESPAN)),
NULL));
@@ -103,7 +103,7 @@ static void add_test(void) {
GPR_ASSERT(cb_called[i][0] == 0);
}
- GPR_ASSERT(0 == grpc_alarm_check(&exec_ctx,
+ GPR_ASSERT(0 == grpc_timer_check(&exec_ctx,
gpr_time_add(start, gpr_time_from_millis(
1600, GPR_TIMESPAN)),
NULL));
@@ -112,7 +112,7 @@ static void add_test(void) {
GPR_ASSERT(cb_called[i][0] == 0);
}
- grpc_alarm_list_shutdown(&exec_ctx);
+ grpc_timer_list_shutdown(&exec_ctx);
grpc_exec_ctx_finish(&exec_ctx);
}
@@ -122,34 +122,34 @@ static gpr_timespec tfm(int m) {
return t;
}
-/* Cleaning up a list with pending alarms. */
+/* Cleaning up a list with pending timers. */
void destruction_test(void) {
- grpc_alarm alarms[5];
+ grpc_timer timers[5];
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_alarm_list_init(gpr_time_0(GPR_CLOCK_REALTIME));
+ grpc_timer_list_init(gpr_time_0(GPR_CLOCK_REALTIME));
memset(cb_called, 0, sizeof(cb_called));
- grpc_alarm_init(&exec_ctx, &alarms[0], tfm(100), cb, (void *)(gpr_intptr)0,
+ grpc_timer_init(&exec_ctx, &timers[0], tfm(100), cb, (void *)(gpr_intptr)0,
gpr_time_0(GPR_CLOCK_REALTIME));
- grpc_alarm_init(&exec_ctx, &alarms[1], tfm(3), cb, (void *)(gpr_intptr)1,
+ grpc_timer_init(&exec_ctx, &timers[1], tfm(3), cb, (void *)(gpr_intptr)1,
gpr_time_0(GPR_CLOCK_REALTIME));
- grpc_alarm_init(&exec_ctx, &alarms[2], tfm(100), cb, (void *)(gpr_intptr)2,
+ grpc_timer_init(&exec_ctx, &timers[2], tfm(100), cb, (void *)(gpr_intptr)2,
gpr_time_0(GPR_CLOCK_REALTIME));
- grpc_alarm_init(&exec_ctx, &alarms[3], tfm(3), cb, (void *)(gpr_intptr)3,
+ grpc_timer_init(&exec_ctx, &timers[3], tfm(3), cb, (void *)(gpr_intptr)3,
gpr_time_0(GPR_CLOCK_REALTIME));
- grpc_alarm_init(&exec_ctx, &alarms[4], tfm(1), cb, (void *)(gpr_intptr)4,
+ grpc_timer_init(&exec_ctx, &timers[4], tfm(1), cb, (void *)(gpr_intptr)4,
gpr_time_0(GPR_CLOCK_REALTIME));
- GPR_ASSERT(1 == grpc_alarm_check(&exec_ctx, tfm(2), NULL));
+ GPR_ASSERT(1 == grpc_timer_check(&exec_ctx, tfm(2), NULL));
grpc_exec_ctx_finish(&exec_ctx);
GPR_ASSERT(1 == cb_called[4][1]);
- grpc_alarm_cancel(&exec_ctx, &alarms[0]);
- grpc_alarm_cancel(&exec_ctx, &alarms[3]);
+ grpc_timer_cancel(&exec_ctx, &timers[0]);
+ grpc_timer_cancel(&exec_ctx, &timers[3]);
grpc_exec_ctx_finish(&exec_ctx);
GPR_ASSERT(1 == cb_called[0][0]);
GPR_ASSERT(1 == cb_called[3][0]);
- grpc_alarm_list_shutdown(&exec_ctx);
+ grpc_timer_list_shutdown(&exec_ctx);
grpc_exec_ctx_finish(&exec_ctx);
GPR_ASSERT(1 == cb_called[1][0]);
GPR_ASSERT(1 == cb_called[2][0]);
diff --git a/test/core/surface/alarm_test.c b/test/core/surface/alarm_test.c
new file mode 100644
index 0000000000..52fe4ea084
--- /dev/null
+++ b/test/core/surface/alarm_test.c
@@ -0,0 +1,100 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/time.h>
+#include <grpc/support/useful.h>
+#include "test/core/util/test_config.h"
+
+#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
+
+static void *create_test_tag(void) {
+ static gpr_intptr i = 0;
+ return (void *)(++i);
+}
+
+/* helper for tests to shutdown correctly and tersely */
+static void shutdown_and_destroy(grpc_completion_queue *cc) {
+ grpc_event ev;
+ grpc_completion_queue_shutdown(cc);
+ ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL);
+ GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
+ grpc_completion_queue_destroy(cc);
+}
+
+static void test_alarm(void) {
+ grpc_completion_queue *cc;
+
+ LOG_TEST("test_alarm");
+ cc = grpc_completion_queue_create(NULL);
+ {
+ /* regular expiry */
+ grpc_event ev;
+ void *tag = create_test_tag();
+ grpc_alarm *alarm =
+ grpc_alarm_create(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), tag);
+
+ ev = grpc_completion_queue_next(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2),
+ NULL);
+ GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
+ GPR_ASSERT(ev.tag == tag);
+ GPR_ASSERT(ev.success);
+ grpc_alarm_destroy(alarm);
+ }
+ {
+ /* cancellation */
+ grpc_event ev;
+ void *tag = create_test_tag();
+ grpc_alarm *alarm =
+ grpc_alarm_create(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), tag);
+
+ grpc_alarm_cancel(alarm);
+ ev = grpc_completion_queue_next(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1),
+ NULL);
+ GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
+ GPR_ASSERT(ev.tag == tag);
+ GPR_ASSERT(ev.success == 0);
+ grpc_alarm_destroy(alarm);
+ }
+ shutdown_and_destroy(cc);
+}
+
+int main(int argc, char **argv) {
+ grpc_test_init(argc, argv);
+ grpc_init();
+ test_alarm();
+ grpc_shutdown();
+ return 0;
+}
diff --git a/test/cpp/common/alarm_test.cc b/test/cpp/common/alarm_test.cc
new file mode 100644
index 0000000000..18083cd8bd
--- /dev/null
+++ b/test/cpp/common/alarm_test.cc
@@ -0,0 +1,91 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <grpc++/alarm.h>
+#include <grpc++/completion_queue.h>
+#include <gtest/gtest.h>
+
+#include "test/core/util/test_config.h"
+
+namespace grpc {
+namespace {
+
+class TestTag : public CompletionQueueTag {
+ public:
+ TestTag() : tag_(0) {}
+ TestTag(gpr_intptr tag) : tag_(tag) {}
+ bool FinalizeResult(void** tag, bool* status) { return true; }
+ gpr_intptr tag() { return tag_; }
+
+ private:
+ gpr_intptr tag_;
+};
+
+TEST(AlarmTest, RegularExpiry) {
+ CompletionQueue cq;
+ TestTag input_tag(1618033);
+ Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), &input_tag);
+
+ TestTag* output_tag;
+ bool ok;
+ const CompletionQueue::NextStatus status = cq.AsyncNext(
+ (void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2));
+
+ EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
+ EXPECT_TRUE(ok);
+ EXPECT_EQ(output_tag->tag(), input_tag.tag());
+}
+
+TEST(AlarmTest, Cancellation) {
+ CompletionQueue cq;
+ TestTag input_tag(1618033);
+ Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), &input_tag);
+ alarm.Cancel();
+
+ TestTag* output_tag;
+ bool ok;
+ const CompletionQueue::NextStatus status = cq.AsyncNext(
+ (void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
+
+ EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
+ EXPECT_FALSE(ok);
+ EXPECT_EQ(output_tag->tag(), input_tag.tag());
+}
+
+} // namespace
+} // namespace grpc
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}