xref: /aosp_15_r20/external/grpc-grpc/test/core/transport/binder/endpoint_binder_pool_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2021 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/ext/transport/binder/client/endpoint_binder_pool.h"
16 
17 #include <cassert>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 
25 #include "absl/memory/memory.h"
26 
27 #include "test/core/transport/binder/mock_objects.h"
28 #include "test/core/util/test_config.h"
29 
30 namespace grpc_binder {
31 
32 class CallbackChecker {
33  public:
34   MOCK_METHOD(void, Cb, (std::unique_ptr<grpc_binder::Binder>), ());
35 };
36 
TEST(EndpointBinderPoolTest,AddBeforeGet)37 TEST(EndpointBinderPoolTest, AddBeforeGet) {
38   EndpointBinderPool pool;
39   auto b = std::make_unique<grpc_binder::MockBinder>();
40   CallbackChecker cc;
41   pool.AddEndpointBinder("test", std::move(b));
42   // TODO(mingcl): Use pointer matcher to verify it is `b` being passed back
43   // here. It is only available in newer gtest version
44   EXPECT_CALL(cc, Cb(testing::_));
45   pool.GetEndpointBinder(
46       "test", std::bind(&CallbackChecker::Cb, &cc, std::placeholders::_1));
47 }
48 
TEST(EndpointBinderPoolTest,GetBeforeAdd)49 TEST(EndpointBinderPoolTest, GetBeforeAdd) {
50   EndpointBinderPool pool;
51   auto b = std::make_unique<grpc_binder::MockBinder>();
52   CallbackChecker cc;
53   EXPECT_CALL(cc, Cb(testing::_)).Times(0);
54   pool.GetEndpointBinder(
55       "test", std::bind(&CallbackChecker::Cb, &cc, std::placeholders::_1));
56   EXPECT_CALL(cc, Cb(testing::_)).Times(1);
57   pool.AddEndpointBinder("test", std::move(b));
58 }
59 
TEST(EndpointBinderPoolTest,ExpectNotCalled)60 TEST(EndpointBinderPoolTest, ExpectNotCalled) {
61   EndpointBinderPool pool;
62   auto b = std::make_unique<grpc_binder::MockBinder>();
63   CallbackChecker cc;
64   EXPECT_CALL(cc, Cb(testing::_)).Times(0);
65   pool.GetEndpointBinder(
66       "test", std::bind(&CallbackChecker::Cb, &cc, std::placeholders::_1));
67 }
68 
69 }  // namespace grpc_binder
70 
main(int argc,char ** argv)71 int main(int argc, char** argv) {
72   ::testing::InitGoogleTest(&argc, argv);
73   grpc::testing::TestEnvironment env(&argc, argv);
74   return RUN_ALL_TESTS();
75 }
76