David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc++/alarm.h> |
| 20 | #include <grpc++/completion_queue.h> |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "test/core/util/test_config.h" |
| 26 | |
| 27 | namespace grpc { |
| 28 | namespace { |
| 29 | |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 30 | TEST(AlarmTest, RegularExpiry) { |
| 31 | CompletionQueue cq; |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 32 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 33 | Alarm alarm; |
| 34 | alarm.Set(&cq, grpc_timeout_seconds_to_deadline(1), junk); |
| 35 | |
| 36 | void* output_tag; |
| 37 | bool ok; |
| 38 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
| 39 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(2)); |
| 40 | |
| 41 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 42 | EXPECT_TRUE(ok); |
| 43 | EXPECT_EQ(junk, output_tag); |
| 44 | } |
| 45 | |
| 46 | TEST(AlarmTest, MultithreadedRegularExpiry) { |
| 47 | CompletionQueue cq; |
| 48 | void* junk = reinterpret_cast<void*>(1618033); |
| 49 | void* output_tag; |
| 50 | bool ok; |
| 51 | CompletionQueue::NextStatus status; |
| 52 | Alarm alarm; |
| 53 | |
| 54 | std::thread t1([&alarm, &cq, &junk] { |
| 55 | alarm.Set(&cq, grpc_timeout_seconds_to_deadline(1), junk); |
| 56 | }); |
| 57 | |
| 58 | std::thread t2([&cq, &ok, &output_tag, &status] { |
| 59 | status = cq.AsyncNext((void**)&output_tag, &ok, |
| 60 | grpc_timeout_seconds_to_deadline(2)); |
| 61 | }); |
| 62 | |
| 63 | t1.join(); |
| 64 | t2.join(); |
| 65 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 66 | EXPECT_TRUE(ok); |
| 67 | EXPECT_EQ(junk, output_tag); |
| 68 | } |
| 69 | |
| 70 | TEST(AlarmTest, DeprecatedRegularExpiry) { |
| 71 | CompletionQueue cq; |
| 72 | void* junk = reinterpret_cast<void*>(1618033); |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 73 | Alarm alarm(&cq, grpc_timeout_seconds_to_deadline(1), junk); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 74 | |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 75 | void* output_tag; |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 76 | bool ok; |
| 77 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 78 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(2)); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 79 | |
| 80 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 81 | EXPECT_TRUE(ok); |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 82 | EXPECT_EQ(junk, output_tag); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
David Garcia Quintas | ab1ff6b | 2017-06-28 14:14:24 -0700 | [diff] [blame] | 85 | TEST(AlarmTest, MoveConstructor) { |
David Garcia Quintas | e48949f | 2017-06-19 13:57:32 -0700 | [diff] [blame] | 86 | CompletionQueue cq; |
| 87 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 88 | Alarm first; |
| 89 | first.Set(&cq, grpc_timeout_seconds_to_deadline(1), junk); |
David Garcia Quintas | e48949f | 2017-06-19 13:57:32 -0700 | [diff] [blame] | 90 | Alarm second(std::move(first)); |
David Garcia Quintas | ab1ff6b | 2017-06-28 14:14:24 -0700 | [diff] [blame] | 91 | void* output_tag; |
| 92 | bool ok; |
| 93 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
| 94 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(2)); |
| 95 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 96 | EXPECT_TRUE(ok); |
| 97 | EXPECT_EQ(junk, output_tag); |
| 98 | } |
| 99 | |
| 100 | TEST(AlarmTest, MoveAssignment) { |
| 101 | CompletionQueue cq; |
| 102 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 103 | Alarm first; |
| 104 | first.Set(&cq, grpc_timeout_seconds_to_deadline(1), junk); |
David Garcia Quintas | ab1ff6b | 2017-06-28 14:14:24 -0700 | [diff] [blame] | 105 | Alarm second(std::move(first)); |
David Garcia Quintas | e48949f | 2017-06-19 13:57:32 -0700 | [diff] [blame] | 106 | first = std::move(second); |
| 107 | |
| 108 | void* output_tag; |
| 109 | bool ok; |
| 110 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
| 111 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(2)); |
| 112 | |
| 113 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 114 | EXPECT_TRUE(ok); |
| 115 | EXPECT_EQ(junk, output_tag); |
| 116 | } |
| 117 | |
David Garcia Quintas | 0160873 | 2016-02-22 17:49:45 -0800 | [diff] [blame] | 118 | TEST(AlarmTest, RegularExpiryChrono) { |
| 119 | CompletionQueue cq; |
| 120 | void* junk = reinterpret_cast<void*>(1618033); |
| 121 | std::chrono::system_clock::time_point one_sec_deadline = |
| 122 | std::chrono::system_clock::now() + std::chrono::seconds(1); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 123 | Alarm alarm; |
| 124 | alarm.Set(&cq, one_sec_deadline, junk); |
David Garcia Quintas | 0160873 | 2016-02-22 17:49:45 -0800 | [diff] [blame] | 125 | |
| 126 | void* output_tag; |
| 127 | bool ok; |
| 128 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 129 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(2)); |
David Garcia Quintas | 0160873 | 2016-02-22 17:49:45 -0800 | [diff] [blame] | 130 | |
| 131 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 132 | EXPECT_TRUE(ok); |
| 133 | EXPECT_EQ(junk, output_tag); |
| 134 | } |
| 135 | |
Vijay Pai | 64e7875 | 2016-02-18 13:38:32 -0800 | [diff] [blame] | 136 | TEST(AlarmTest, ZeroExpiry) { |
| 137 | CompletionQueue cq; |
| 138 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 139 | Alarm alarm; |
| 140 | alarm.Set(&cq, grpc_timeout_seconds_to_deadline(0), junk); |
Vijay Pai | 64e7875 | 2016-02-18 13:38:32 -0800 | [diff] [blame] | 141 | |
| 142 | void* output_tag; |
| 143 | bool ok; |
| 144 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
Craig Tiller | 1a8f1e8 | 2017-09-21 10:07:36 -0700 | [diff] [blame] | 145 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(1)); |
Vijay Pai | 64e7875 | 2016-02-18 13:38:32 -0800 | [diff] [blame] | 146 | |
| 147 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 148 | EXPECT_TRUE(ok); |
| 149 | EXPECT_EQ(junk, output_tag); |
| 150 | } |
| 151 | |
| 152 | TEST(AlarmTest, NegativeExpiry) { |
| 153 | CompletionQueue cq; |
| 154 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 155 | Alarm alarm; |
| 156 | alarm.Set(&cq, grpc_timeout_seconds_to_deadline(-1), junk); |
Vijay Pai | 64e7875 | 2016-02-18 13:38:32 -0800 | [diff] [blame] | 157 | |
| 158 | void* output_tag; |
| 159 | bool ok; |
| 160 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
Craig Tiller | 1a8f1e8 | 2017-09-21 10:07:36 -0700 | [diff] [blame] | 161 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(1)); |
Vijay Pai | 64e7875 | 2016-02-18 13:38:32 -0800 | [diff] [blame] | 162 | |
| 163 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 164 | EXPECT_TRUE(ok); |
| 165 | EXPECT_EQ(junk, output_tag); |
| 166 | } |
| 167 | |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 168 | TEST(AlarmTest, Cancellation) { |
| 169 | CompletionQueue cq; |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 170 | void* junk = reinterpret_cast<void*>(1618033); |
Vijay Pai | 58c33ba | 2017-09-01 14:08:42 -0700 | [diff] [blame] | 171 | Alarm alarm; |
| 172 | alarm.Set(&cq, grpc_timeout_seconds_to_deadline(2), junk); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 173 | alarm.Cancel(); |
| 174 | |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 175 | void* output_tag; |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 176 | bool ok; |
| 177 | const CompletionQueue::NextStatus status = cq.AsyncNext( |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 178 | (void**)&output_tag, &ok, grpc_timeout_seconds_to_deadline(1)); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 179 | |
| 180 | EXPECT_EQ(status, CompletionQueue::GOT_EVENT); |
| 181 | EXPECT_FALSE(ok); |
vjpai | 8fdafed | 2016-02-10 23:16:55 -0800 | [diff] [blame] | 182 | EXPECT_EQ(junk, output_tag); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace |
| 186 | } // namespace grpc |
| 187 | |
| 188 | int main(int argc, char** argv) { |
vjpai | dbca72f | 2016-02-11 00:18:16 -0800 | [diff] [blame] | 189 | grpc_test_init(argc, argv); |
David Garcia Quintas | 7fd0fd5 | 2015-10-07 17:41:08 -0700 | [diff] [blame] | 190 | ::testing::InitGoogleTest(&argc, argv); |
| 191 | return RUN_ALL_TESTS(); |
| 192 | } |