Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc++/support/channel_arguments.h>
35 :
36 : #include <grpc/grpc.h>
37 : #include <gtest/gtest.h>
38 :
39 : namespace grpc {
40 : namespace testing {
41 :
42 4 : class ChannelArgumentsTest : public ::testing::Test {
43 : protected:
44 6 : void SetChannelArgs(const ChannelArguments& channel_args,
45 : grpc_channel_args* args) {
46 6 : channel_args.SetChannelArgs(args);
47 6 : }
48 : };
49 :
50 5 : TEST_F(ChannelArgumentsTest, SetInt) {
51 : grpc_channel_args args;
52 1 : ChannelArguments channel_args;
53 : // Empty arguments.
54 1 : SetChannelArgs(channel_args, &args);
55 1 : EXPECT_EQ(static_cast<size_t>(0), args.num_args);
56 :
57 1 : grpc::string key("key0");
58 1 : channel_args.SetInt(key, 0);
59 : // Clear key early to make sure channel_args takes a copy
60 1 : key = "";
61 1 : SetChannelArgs(channel_args, &args);
62 1 : EXPECT_EQ(static_cast<size_t>(1), args.num_args);
63 1 : EXPECT_EQ(GRPC_ARG_INTEGER, args.args[0].type);
64 1 : EXPECT_STREQ("key0", args.args[0].key);
65 1 : EXPECT_EQ(0, args.args[0].value.integer);
66 :
67 1 : key = "key1";
68 1 : channel_args.SetInt(key, 1);
69 1 : key = "";
70 1 : SetChannelArgs(channel_args, &args);
71 1 : EXPECT_EQ(static_cast<size_t>(2), args.num_args);
72 : // We do not enforce order on the arguments.
73 3 : for (size_t i = 0; i < args.num_args; i++) {
74 2 : EXPECT_EQ(GRPC_ARG_INTEGER, args.args[i].type);
75 2 : if (grpc::string(args.args[i].key) == "key0") {
76 1 : EXPECT_EQ(0, args.args[i].value.integer);
77 1 : } else if (grpc::string(args.args[i].key) == "key1") {
78 1 : EXPECT_EQ(1, args.args[i].value.integer);
79 : }
80 1 : }
81 1 : }
82 :
83 5 : TEST_F(ChannelArgumentsTest, SetString) {
84 : grpc_channel_args args;
85 1 : ChannelArguments channel_args;
86 : // Empty arguments.
87 1 : SetChannelArgs(channel_args, &args);
88 1 : EXPECT_EQ(static_cast<size_t>(0), args.num_args);
89 :
90 1 : grpc::string key("key0");
91 2 : grpc::string val("val0");
92 1 : channel_args.SetString(key, val);
93 : // Clear key/val early to make sure channel_args takes a copy
94 1 : key = "";
95 1 : val = "";
96 1 : SetChannelArgs(channel_args, &args);
97 1 : EXPECT_EQ(static_cast<size_t>(1), args.num_args);
98 1 : EXPECT_EQ(GRPC_ARG_STRING, args.args[0].type);
99 1 : EXPECT_STREQ("key0", args.args[0].key);
100 1 : EXPECT_STREQ("val0", args.args[0].value.string);
101 :
102 1 : key = "key1";
103 1 : val = "val1";
104 1 : channel_args.SetString(key, val);
105 1 : SetChannelArgs(channel_args, &args);
106 1 : EXPECT_EQ(static_cast<size_t>(2), args.num_args);
107 : // We do not enforce order on the arguments.
108 3 : for (size_t i = 0; i < args.num_args; i++) {
109 2 : EXPECT_EQ(GRPC_ARG_STRING, args.args[i].type);
110 2 : if (grpc::string(args.args[i].key) == "key0") {
111 1 : EXPECT_STREQ("val0", args.args[i].value.string);
112 1 : } else if (grpc::string(args.args[i].key) == "key1") {
113 1 : EXPECT_STREQ("val1", args.args[i].value.string);
114 : }
115 1 : }
116 1 : }
117 :
118 : } // namespace testing
119 : } // namespace grpc
120 :
121 1 : int main(int argc, char** argv) {
122 1 : ::testing::InitGoogleTest(&argc, argv);
123 1 : return RUN_ALL_TESTS();
124 3 : }
|