aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/surface/alarm_test.c
blob: 4fd7cb93c65fc8b728d7f07bd7b4c3d629d2ee7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#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 intptr_t 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_for_next(NULL);
  {
    /* regular expiry */
    grpc_event ev;
    void *tag = create_test_tag();
    grpc_alarm *alarm = grpc_alarm_create(NULL);
    grpc_alarm_set(alarm, cc, grpc_timeout_seconds_to_deadline(1), tag, NULL);

    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, NULL);
  }
  {
    /* cancellation */
    grpc_event ev;
    void *tag = create_test_tag();
    grpc_alarm *alarm = grpc_alarm_create(NULL);
    grpc_alarm_set(alarm, cc, grpc_timeout_seconds_to_deadline(2), tag, NULL);

    grpc_alarm_cancel(alarm, NULL);
    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, NULL);
  }
  {
    /* alarm_destroy before cq_next */
    grpc_event ev;
    void *tag = create_test_tag();
    grpc_alarm *alarm = grpc_alarm_create(NULL);
    grpc_alarm_set(alarm, cc, grpc_timeout_seconds_to_deadline(2), tag, NULL);

    grpc_alarm_destroy(alarm, NULL);
    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);
  }
  {
    /* alarm_destroy before set */
    grpc_alarm *alarm = grpc_alarm_create(NULL);
    grpc_alarm_destroy(alarm, NULL);
  }

  shutdown_and_destroy(cc);
}

int main(int argc, char **argv) {
  grpc_test_init(argc, argv);
  grpc_init();
  test_alarm();
  grpc_shutdown();
  return 0;
}