blob: 09df6852a512954f923146c656bc36a90fe85d87 [file] [log] [blame]
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -07001/*
2 *
David Garcia Quintasa43aadd2016-01-21 10:01:28 -08003 * Copyright 2015-2016, Google Inc.
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -07004 * 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++/alarm.h>
35#include <grpc++/completion_queue.h>
36#include <gtest/gtest.h>
37
David Garcia Quintas08a0a332016-01-21 01:04:36 -080038#include <grpc++/completion_queue.h>
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -070039#include "test/core/util/test_config.h"
40
41namespace grpc {
42namespace {
43
44class TestTag : public CompletionQueueTag {
45 public:
46 TestTag() : tag_(0) {}
Craig Tiller7536af02015-12-22 13:49:30 -080047 TestTag(intptr_t tag) : tag_(tag) {}
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -070048 bool FinalizeResult(void** tag, bool* status) { return true; }
Craig Tiller7536af02015-12-22 13:49:30 -080049 intptr_t tag() { return tag_; }
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -070050
51 private:
Craig Tiller7536af02015-12-22 13:49:30 -080052 intptr_t tag_;
David Garcia Quintas7fd0fd52015-10-07 17:41:08 -070053};
54
55TEST(AlarmTest, RegularExpiry) {
56 CompletionQueue cq;
57 TestTag input_tag(1618033);
58 Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), &input_tag);
59
60 TestTag* output_tag;
61 bool ok;
62 const CompletionQueue::NextStatus status = cq.AsyncNext(
63 (void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2));
64
65 EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
66 EXPECT_TRUE(ok);
67 EXPECT_EQ(output_tag->tag(), input_tag.tag());
68}
69
70TEST(AlarmTest, Cancellation) {
71 CompletionQueue cq;
72 TestTag input_tag(1618033);
73 Alarm alarm(&cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2), &input_tag);
74 alarm.Cancel();
75
76 TestTag* output_tag;
77 bool ok;
78 const CompletionQueue::NextStatus status = cq.AsyncNext(
79 (void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
80
81 EXPECT_EQ(status, CompletionQueue::GOT_EVENT);
82 EXPECT_FALSE(ok);
83 EXPECT_EQ(output_tag->tag(), input_tag.tag());
84}
85
86} // namespace
87} // namespace grpc
88
89int main(int argc, char** argv) {
90 ::testing::InitGoogleTest(&argc, argv);
91 return RUN_ALL_TESTS();
92}