blob: 054c32838978361932e05806ab225e8aacceb225 [file] [log] [blame]
David Garcia Quintasf747bbc2015-10-04 23:09:47 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/grpc.h>
35#include <grpc/support/alloc.h>
36#include <grpc/support/log.h>
37#include <grpc/support/time.h>
38#include <grpc/support/useful.h>
39#include "test/core/util/test_config.h"
40
41#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
42
43static void *create_test_tag(void) {
Craig Tiller7536af02015-12-22 13:49:30 -080044 static intptr_t i = 0;
David Garcia Quintasf747bbc2015-10-04 23:09:47 -070045 return (void *)(++i);
46}
47
48/* helper for tests to shutdown correctly and tersely */
49static void shutdown_and_destroy(grpc_completion_queue *cc) {
50 grpc_event ev;
51 grpc_completion_queue_shutdown(cc);
52 ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL);
53 GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
54 grpc_completion_queue_destroy(cc);
55}
56
57static void test_alarm(void) {
58 grpc_completion_queue *cc;
59
60 LOG_TEST("test_alarm");
61 cc = grpc_completion_queue_create(NULL);
62 {
63 /* regular expiry */
64 grpc_event ev;
65 void *tag = create_test_tag();
66 grpc_alarm *alarm =
67 grpc_alarm_create(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), tag);
68
69 ev = grpc_completion_queue_next(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2),
70 NULL);
71 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
72 GPR_ASSERT(ev.tag == tag);
73 GPR_ASSERT(ev.success);
74 grpc_alarm_destroy(alarm);
75 }
76 {
77 /* cancellation */
78 grpc_event ev;
79 void *tag = create_test_tag();
80 grpc_alarm *alarm =
81 grpc_alarm_create(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), tag);
82
83 grpc_alarm_cancel(alarm);
84 ev = grpc_completion_queue_next(cc, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1),
85 NULL);
86 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
87 GPR_ASSERT(ev.tag == tag);
88 GPR_ASSERT(ev.success == 0);
89 grpc_alarm_destroy(alarm);
90 }
91 shutdown_and_destroy(cc);
92}
93
94int main(int argc, char **argv) {
95 grpc_test_init(argc, argv);
96 grpc_init();
97 test_alarm();
98 grpc_shutdown();
99 return 0;
100}