Debian Codegraph @

test/core/iomgr/buffer_list_test.cc

raw
1 /*
2 *
3 * Copyright 2018 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 "src/core/lib/iomgr/buffer_list.h"
20
21 #include <gtest/gtest.h>
22
23 #include <grpc/grpc.h>
24
25 #include "src/core/lib/iomgr/port.h"
26 #include "test/core/util/test_config.h"
27
28 #ifdef GRPC_LINUX_ERRQUEUE
29
30 static void TestShutdownFlushesListVerifier(void* arg,
31 grpc_core::Timestamps* /*ts*/,
32 grpc_error_handle error) {
33 ASSERT_TRUE(error.ok());
34 ASSERT_NE(arg, nullptr);
35 gpr_atm* done = reinterpret_cast<gpr_atm*>(arg);
36 gpr_atm_rel_store(done, static_cast<gpr_atm>(1));
37 }
38
39 /** Tests that all TracedBuffer elements in the list are flushed out on
40 * shutdown.
41 * Also tests that arg is passed correctly.
42 */
43 TEST(BufferListTest, Testshutdownflusheslist) {
44 grpc_core::grpc_tcp_set_write_timestamps_callback(
45 TestShutdownFlushesListVerifier);
46 grpc_core::TracedBufferList tb_list;
47 #define NUM_ELEM 5
48 gpr_atm verifier_called[NUM_ELEM];
49 for (auto i = 0; i < NUM_ELEM; i++) {
50 gpr_atm_rel_store(&verifier_called[i], static_cast<gpr_atm>(0));
51 tb_list.AddNewEntry(i, 0, static_cast<void*>(&verifier_called[i]));
52 }
53 tb_list.Shutdown(nullptr, absl::OkStatus());
54 for (auto i = 0; i < NUM_ELEM; i++) {
55 ASSERT_EQ(gpr_atm_acq_load(&verifier_called[i]), static_cast<gpr_atm>(1));
56 }
57 }
58
59 static void TestVerifierCalledOnAckVerifier(void* arg,
60 grpc_core::Timestamps* ts,
61 grpc_error_handle error) {
62 ASSERT_TRUE(error.ok());
63 ASSERT_NE(arg, nullptr);
64 ASSERT_EQ(ts->acked_time.time.clock_type, GPR_CLOCK_REALTIME);
65 ASSERT_EQ(ts->acked_time.time.tv_sec, 123);
66 ASSERT_EQ(ts->acked_time.time.tv_nsec, 456);
67 ASSERT_GT(ts->info.length, 0);
68 gpr_atm* done = reinterpret_cast<gpr_atm*>(arg);
69 gpr_atm_rel_store(done, static_cast<gpr_atm>(1));
70 }
71
72 /** Tests that the timestamp verifier is called on an ACK timestamp.
73 */
74 TEST(BufferListTest, Testverifiercalledonack) {
75 struct sock_extended_err serr;
76 serr.ee_data = 213;
77 serr.ee_info = grpc_core::SCM_TSTAMP_ACK;
78 struct grpc_core::scm_timestamping tss;
79 tss.ts[0].tv_sec = 123;
80 tss.ts[0].tv_nsec = 456;
81 grpc_core::grpc_tcp_set_write_timestamps_callback(
82 TestVerifierCalledOnAckVerifier);
83 grpc_core::TracedBufferList tb_list;
84 gpr_atm verifier_called;
85 gpr_atm_rel_store(&verifier_called, static_cast<gpr_atm>(0));
86 tb_list.AddNewEntry(213, 0, &verifier_called);
87 tb_list.ProcessTimestamp(&serr, nullptr, &tss);
88 ASSERT_EQ(gpr_atm_acq_load(&verifier_called), static_cast<gpr_atm>(1));
89 tb_list.Shutdown(nullptr, absl::OkStatus());
90 }
91
92 /** Tests that shutdown can be called repeatedly.
93 */
94 TEST(BufferListTest, Testrepeatedshutdown) {
95 struct sock_extended_err serr;
96 serr.ee_data = 213;
97 serr.ee_info = grpc_core::SCM_TSTAMP_ACK;
98 struct grpc_core::scm_timestamping tss;
99 tss.ts[0].tv_sec = 123;
100 tss.ts[0].tv_nsec = 456;
101 grpc_core::grpc_tcp_set_write_timestamps_callback(
102 TestVerifierCalledOnAckVerifier);
103 grpc_core::TracedBufferList tb_list;
104 gpr_atm verifier_called;
105 gpr_atm_rel_store(&verifier_called, static_cast<gpr_atm>(0));
106 tb_list.AddNewEntry(213, 0, &verifier_called);
107 tb_list.ProcessTimestamp(&serr, nullptr, &tss);
108 ASSERT_EQ(gpr_atm_acq_load(&verifier_called), static_cast<gpr_atm>(1));
109 tb_list.Shutdown(nullptr, absl::OkStatus());
110 tb_list.Shutdown(nullptr, absl::OkStatus());
111 tb_list.Shutdown(nullptr, absl::OkStatus());
112 }
113
114 int main(int argc, char** argv) {
115 grpc::testing::TestEnvironment env(&argc, argv);
116 ::testing::InitGoogleTest(&argc, argv);
117 grpc::testing::TestGrpcScope grpc_scope;
118 return RUN_ALL_TESTS();
119 }
120
121 #else /* GRPC_LINUX_ERRQUEUE */
122
123 int main(int /*argc*/, char** /*argv*/) { return 0; }
124
125 #endif /* GRPC_LINUX_ERRQUEUE */