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