1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/surface/call_utils.h"
16
17 #include <grpc/grpc.h>
18
19 #include <initializer_list>
20
21 #include "gtest/gtest.h"
22
23 namespace grpc_core {
24
TEST(CallUtils,AreWriteFlagsValid)25 TEST(CallUtils, AreWriteFlagsValid) {
26 EXPECT_TRUE(AreWriteFlagsValid(0));
27 EXPECT_TRUE(AreWriteFlagsValid(GRPC_WRITE_BUFFER_HINT));
28 EXPECT_TRUE(AreWriteFlagsValid(GRPC_WRITE_NO_COMPRESS));
29 EXPECT_FALSE(AreWriteFlagsValid(0xffffffff));
30 }
31
TEST(CallUtils,AreInitialMetadataFlagsValid)32 TEST(CallUtils, AreInitialMetadataFlagsValid) {
33 EXPECT_TRUE(AreInitialMetadataFlagsValid(0));
34 EXPECT_TRUE(
35 AreInitialMetadataFlagsValid(GRPC_INITIAL_METADATA_WAIT_FOR_READY));
36 EXPECT_TRUE(AreInitialMetadataFlagsValid(GRPC_WRITE_THROUGH));
37 EXPECT_FALSE(AreInitialMetadataFlagsValid(0xffffffff));
38 }
39
40 namespace {
do_these_things(std::initializer_list<int>)41 void do_these_things(std::initializer_list<int>) {}
42
43 template <typename... T>
TestOps(T...ops)44 std::vector<grpc_op> TestOps(T... ops) {
45 std::vector<grpc_op> out;
46 auto add_op = [&out](grpc_op_type type) {
47 grpc_op op;
48 op.op = type;
49 out.push_back(op);
50 return 1;
51 };
52 do_these_things({add_op(ops)...});
53 return out;
54 }
55 } // namespace
56
TEST(BatchOpIndex,Basic)57 TEST(BatchOpIndex, Basic) {
58 const auto ops = TestOps(GRPC_OP_SEND_INITIAL_METADATA, GRPC_OP_SEND_MESSAGE,
59 GRPC_OP_SEND_CLOSE_FROM_CLIENT);
60 BatchOpIndex idx(ops.data(), ops.size());
61 EXPECT_EQ(idx.op(GRPC_OP_SEND_INITIAL_METADATA), &ops[0]);
62 EXPECT_EQ(idx.op(GRPC_OP_SEND_MESSAGE), &ops[1]);
63 EXPECT_EQ(idx.op(GRPC_OP_SEND_CLOSE_FROM_CLIENT), &ops[2]);
64 EXPECT_EQ(idx.op(GRPC_OP_SEND_STATUS_FROM_SERVER), nullptr);
65 }
66
67 } // namespace grpc_core
68
main(int argc,char ** argv)69 int main(int argc, char** argv) {
70 ::testing::InitGoogleTest(&argc, argv);
71 return RUN_ALL_TESTS();
72 }
73