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