David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | a43aadd | 2016-01-21 10:01:28 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 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++/alarm.h> |
| 35 | #include <grpc++/completion_queue.h> |
| 36 | #include <gtest/gtest.h> |
| 37 | |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 38 | #include <grpc++/completion_queue.h> |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 39 | #include "test/core/util/test_config.h" |
| 40 | |
| 41 | namespace grpc { |
| 42 | namespace { |
| 43 | |
| 44 | class TestTag : public CompletionQueueTag { |
| 45 | public: |
| 46 | TestTag() : tag_(0) {} |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 47 | TestTag(intptr_t tag) : tag_(tag) {} |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 48 | bool FinalizeResult(void** tag, bool* status) { return true; } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 49 | intptr_t tag() { return tag_; } |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 52 | intptr_t tag_; |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | TEST(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 | |
| 70 | TEST(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 | |
| 89 | int main(int argc, char** argv) { |
| 90 | ::testing::InitGoogleTest(&argc, argv); |
| 91 | return RUN_ALL_TESTS(); |
| 92 | } |