Debian Codegraph @

test/cpp/end2end/time_change_test.cc

raw
1 /*
2 *
3 * Copyright 2019 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <sys/time.h>
20
21 #include <thread>
22
23 #include <gtest/gtest.h>
24
25 #include "absl/memory/memory.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30 #include <grpcpp/channel.h>
31 #include <grpcpp/client_context.h>
32 #include <grpcpp/create_channel.h>
33 #include <grpcpp/server.h>
34 #include <grpcpp/server_builder.h>
35 #include <grpcpp/server_context.h>
36
37 #include "src/core/lib/iomgr/timer.h"
38 #include "src/proto/grpc/testing/echo.grpc.pb.h"
39 #include "test/core/util/port.h"
40 #include "test/core/util/test_config.h"
41 #include "test/cpp/end2end/test_service_impl.h"
42 #include "test/cpp/util/subprocess.h"
43
44 static std::string g_root;
45
46 static gpr_mu g_mu;
47 extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);
48 gpr_timespec (*gpr_now_impl_orig)(gpr_clock_type clock_type) = gpr_now_impl;
49 static int g_time_shift_sec = 0;
50 static int g_time_shift_nsec = 0;
51 static gpr_timespec now_impl(gpr_clock_type clock) {
52 auto ts = gpr_now_impl_orig(clock);
53 // We only manipulate the realtime clock to simulate changes in wall-clock
54 // time
55 if (clock != GPR_CLOCK_REALTIME) {
56 return ts;
57 }
58 GPR_ASSERT(ts.tv_nsec >= 0);
59 GPR_ASSERT(ts.tv_nsec < GPR_NS_PER_SEC);
60 gpr_mu_lock(&g_mu);
61 ts.tv_sec += g_time_shift_sec;
62 ts.tv_nsec += g_time_shift_nsec;
63 gpr_mu_unlock(&g_mu);
64 if (ts.tv_nsec >= GPR_NS_PER_SEC) {
65 ts.tv_nsec -= GPR_NS_PER_SEC;
66 ++ts.tv_sec;
67 } else if (ts.tv_nsec < 0) {
68 --ts.tv_sec;
69 ts.tv_nsec = GPR_NS_PER_SEC + ts.tv_nsec;
70 }
71 return ts;
72 }
73
74 // offset the value returned by gpr_now(GPR_CLOCK_REALTIME) by msecs
75 // milliseconds
76 static void set_now_offset(int msecs) {
77 gpr_mu_lock(&g_mu);
78 g_time_shift_sec = msecs / 1000;
79 g_time_shift_nsec = (msecs % 1000) * 1e6;
80 gpr_mu_unlock(&g_mu);
81 }
82
83 // restore the original implementation of gpr_now()
84 static void reset_now_offset() {
85 gpr_mu_lock(&g_mu);
86 g_time_shift_sec = 0;
87 g_time_shift_nsec = 0;
88 gpr_mu_unlock(&g_mu);
89 }
90
91 namespace grpc {
92 namespace testing {
93
94 namespace {
95
96 // gpr_now() is called with invalid clock_type
97 TEST(TimespecTest, GprNowInvalidClockType) {
98 // initialize to some junk value
99 gpr_clock_type invalid_clock_type = static_cast<gpr_clock_type>(32641);
100 EXPECT_DEATH(gpr_now(invalid_clock_type), ".*");
101 }
102
103 // Add timespan with negative nanoseconds
104 TEST(TimespecTest, GprTimeAddNegativeNs) {
105 gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
106 gpr_timespec bad_ts = {1, -1000, GPR_TIMESPAN};
107 EXPECT_DEATH(gpr_time_add(now, bad_ts), ".*");
108 }
109
110 // Subtract timespan with negative nanoseconds
111 TEST(TimespecTest, GprTimeSubNegativeNs) {
112 // Nanoseconds must always be positive. Negative timestamps are represented by
113 // (negative seconds, positive nanoseconds)
114 gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
115 gpr_timespec bad_ts = {1, -1000, GPR_TIMESPAN};
116 EXPECT_DEATH(gpr_time_sub(now, bad_ts), ".*");
117 }
118
119 // Add negative milliseconds to gpr_timespec
120 TEST(TimespecTest, GrpcNegativeMillisToTimespec) {
121 // -1500 milliseconds converts to timespec (-2 secs, 5 * 10^8 nsec)
122 gpr_timespec ts =
123 grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(-1500)
124 .as_timespec(GPR_CLOCK_MONOTONIC);
125 GPR_ASSERT(ts.tv_sec = -2);
126 GPR_ASSERT(ts.tv_nsec = 5e8);
127 GPR_ASSERT(ts.clock_type == GPR_CLOCK_MONOTONIC);
128 }
129
130 class TimeChangeTest : public ::testing::Test {
131 protected:
132 TimeChangeTest() {}
133
134 static void SetUpTestCase() {
135 auto port = grpc_pick_unused_port_or_die();
136 std::ostringstream addr_stream;
137 addr_stream << "localhost:" << port;
138 server_address_ = addr_stream.str();
139 server_ = std::make_unique<SubProcess>(std::vector<std::string>({
140 g_root + "/client_crash_test_server",
141 "--address=" + server_address_,
142 }));
143 GPR_ASSERT(server_);
144 // connect to server and make sure it's reachable.
145 auto channel =
146 grpc::CreateChannel(server_address_, InsecureChannelCredentials());
147 GPR_ASSERT(channel);
148 EXPECT_TRUE(channel->WaitForConnected(
149 grpc_timeout_milliseconds_to_deadline(30000)));
150 }
151
152 static void TearDownTestCase() { server_.reset(); }
153
154 void SetUp() override {
155 channel_ =
156 grpc::CreateChannel(server_address_, InsecureChannelCredentials());
157 GPR_ASSERT(channel_);
158 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
159 }
160
161 void TearDown() override { reset_now_offset(); }
162
163 std::unique_ptr<grpc::testing::EchoTestService::Stub> CreateStub() {
164 return grpc::testing::EchoTestService::NewStub(channel_);
165 }
166
167 std::shared_ptr<Channel> GetChannel() { return channel_; }
168 // time jump offsets in milliseconds
169 const int TIME_OFFSET1 = 20123;
170 const int TIME_OFFSET2 = 5678;
171
172 private:
173 static std::string server_address_;
174 static std::unique_ptr<SubProcess> server_;
175 std::shared_ptr<Channel> channel_;
176 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
177 };
178 std::string TimeChangeTest::server_address_;
179 std::unique_ptr<SubProcess> TimeChangeTest::server_;
180
181 // Wall-clock time jumps forward on client before bidi stream is created
182 TEST_F(TimeChangeTest, TimeJumpForwardBeforeStreamCreated) {
183 EchoRequest request;
184 EchoResponse response;
185 ClientContext context;
186 context.set_deadline(grpc_timeout_milliseconds_to_deadline(5000));
187 context.AddMetadata(kServerResponseStreamsToSend, "1");
188
189 auto channel = GetChannel();
190 GPR_ASSERT(channel);
191 EXPECT_TRUE(
192 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(5000)));
193 auto stub = CreateStub();
194
195 // time jumps forward by TIME_OFFSET1 milliseconds
196 set_now_offset(TIME_OFFSET1);
197 auto stream = stub->BidiStream(&context);
198 request.set_message("Hello");
199 EXPECT_TRUE(stream->Write(request));
200
201 EXPECT_TRUE(stream->WritesDone());
202 EXPECT_TRUE(stream->Read(&response));
203
204 auto status = stream->Finish();
205 EXPECT_TRUE(status.ok());
206 }
207
208 // Wall-clock time jumps back on client before bidi stream is created
209 TEST_F(TimeChangeTest, TimeJumpBackBeforeStreamCreated) {
210 EchoRequest request;
211 EchoResponse response;
212 ClientContext context;
213 context.set_deadline(grpc_timeout_milliseconds_to_deadline(5000));
214 context.AddMetadata(kServerResponseStreamsToSend, "1");
215
216 auto channel = GetChannel();
217 GPR_ASSERT(channel);
218 EXPECT_TRUE(
219 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(5000)));
220 auto stub = CreateStub();
221
222 // time jumps back by TIME_OFFSET1 milliseconds
223 set_now_offset(-TIME_OFFSET1);
224 auto stream = stub->BidiStream(&context);
225 request.set_message("Hello");
226 EXPECT_TRUE(stream->Write(request));
227
228 EXPECT_TRUE(stream->WritesDone());
229 EXPECT_TRUE(stream->Read(&response));
230 EXPECT_EQ(request.message(), response.message());
231
232 auto status = stream->Finish();
233 EXPECT_TRUE(status.ok());
234 }
235
236 // Wall-clock time jumps forward on client while call is in progress
237 TEST_F(TimeChangeTest, TimeJumpForwardAfterStreamCreated) {
238 EchoRequest request;
239 EchoResponse response;
240 ClientContext context;
241 context.set_deadline(grpc_timeout_milliseconds_to_deadline(5000));
242 context.AddMetadata(kServerResponseStreamsToSend, "2");
243
244 auto channel = GetChannel();
245 GPR_ASSERT(channel);
246 EXPECT_TRUE(
247 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(5000)));
248 auto stub = CreateStub();
249
250 auto stream = stub->BidiStream(&context);
251
252 request.set_message("Hello");
253 EXPECT_TRUE(stream->Write(request));
254 EXPECT_TRUE(stream->Read(&response));
255
256 // time jumps forward by TIME_OFFSET1 milliseconds.
257 set_now_offset(TIME_OFFSET1);
258
259 request.set_message("World");
260 EXPECT_TRUE(stream->Write(request));
261 EXPECT_TRUE(stream->WritesDone());
262 EXPECT_TRUE(stream->Read(&response));
263
264 auto status = stream->Finish();
265 EXPECT_TRUE(status.ok());
266 }
267
268 // Wall-clock time jumps back on client while call is in progress
269 TEST_F(TimeChangeTest, TimeJumpBackAfterStreamCreated) {
270 EchoRequest request;
271 EchoResponse response;
272 ClientContext context;
273 context.set_deadline(grpc_timeout_milliseconds_to_deadline(5000));
274 context.AddMetadata(kServerResponseStreamsToSend, "2");
275
276 auto channel = GetChannel();
277 GPR_ASSERT(channel);
278 EXPECT_TRUE(
279 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(5000)));
280 auto stub = CreateStub();
281
282 auto stream = stub->BidiStream(&context);
283
284 request.set_message("Hello");
285 EXPECT_TRUE(stream->Write(request));
286 EXPECT_TRUE(stream->Read(&response));
287
288 // time jumps back TIME_OFFSET1 milliseconds.
289 set_now_offset(-TIME_OFFSET1);
290
291 request.set_message("World");
292 EXPECT_TRUE(stream->Write(request));
293 EXPECT_TRUE(stream->WritesDone());
294 EXPECT_TRUE(stream->Read(&response));
295
296 auto status = stream->Finish();
297 EXPECT_TRUE(status.ok());
298 }
299
300 // Wall-clock time jumps forward and backwards during call
301 TEST_F(TimeChangeTest, TimeJumpForwardAndBackDuringCall) {
302 EchoRequest request;
303 EchoResponse response;
304 ClientContext context;
305 context.set_deadline(grpc_timeout_milliseconds_to_deadline(5000));
306 context.AddMetadata(kServerResponseStreamsToSend, "2");
307
308 auto channel = GetChannel();
309 GPR_ASSERT(channel);
310
311 EXPECT_TRUE(
312 channel->WaitForConnected(grpc_timeout_milliseconds_to_deadline(5000)));
313 auto stub = CreateStub();
314 auto stream = stub->BidiStream(&context);
315
316 request.set_message("Hello");
317 EXPECT_TRUE(stream->Write(request));
318
319 // time jumps back by TIME_OFFSET2 milliseconds
320 set_now_offset(-TIME_OFFSET2);
321
322 EXPECT_TRUE(stream->Read(&response));
323 request.set_message("World");
324
325 // time jumps forward by TIME_OFFSET milliseconds
326 set_now_offset(TIME_OFFSET1);
327
328 EXPECT_TRUE(stream->Write(request));
329
330 // time jumps back by TIME_OFFSET2 milliseconds
331 set_now_offset(-TIME_OFFSET2);
332
333 EXPECT_TRUE(stream->WritesDone());
334
335 // time jumps back by TIME_OFFSET2 milliseconds
336 set_now_offset(-TIME_OFFSET2);
337
338 EXPECT_TRUE(stream->Read(&response));
339
340 // time jumps back by TIME_OFFSET2 milliseconds
341 set_now_offset(-TIME_OFFSET2);
342
343 auto status = stream->Finish();
344 EXPECT_TRUE(status.ok());
345 }
346
347 } // namespace
348
349 } // namespace testing
350 } // namespace grpc
351
352 int main(int argc, char** argv) {
353 std::string me = argv[0];
354 // get index of last slash in path to test binary
355 auto lslash = me.rfind('/');
356 // set g_root = path to directory containing test binary
357 if (lslash != std::string::npos) {
358 g_root = me.substr(0, lslash);
359 } else {
360 g_root = ".";
361 }
362
363 gpr_mu_init(&g_mu);
364 gpr_now_impl = now_impl;
365
366 grpc::testing::TestEnvironment env(&argc, argv);
367 ::testing::InitGoogleTest(&argc, argv);
368 auto ret = RUN_ALL_TESTS();
369 return ret;
370 }