| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "src/tracing/core/service_impl.h" |
| |
| #include <string.h> |
| |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "perfetto/tracing/core/data_source_config.h" |
| #include "perfetto/tracing/core/data_source_descriptor.h" |
| #include "perfetto/tracing/core/producer.h" |
| #include "perfetto/tracing/core/shared_memory.h" |
| #include "src/base/test/test_task_runner.h" |
| #include "src/tracing/test/test_shared_memory.h" |
| |
| namespace perfetto { |
| namespace { |
| |
| using ::testing::_; |
| using ::testing::InSequence; |
| using ::testing::Mock; |
| |
| class MockProducer : public Producer { |
| public: |
| ~MockProducer() override {} |
| |
| // Producer implementation. |
| MOCK_METHOD0(OnConnect, void()); |
| MOCK_METHOD0(OnDisconnect, void()); |
| MOCK_METHOD2(CreateDataSourceInstance, |
| void(DataSourceInstanceID, const DataSourceConfig&)); |
| MOCK_METHOD1(TearDownDataSourceInstance, void(DataSourceInstanceID)); |
| }; |
| |
| TEST(ServiceImpl, RegisterAndUnregister) { |
| base::TestTaskRunner task_runner; |
| auto shm_factory = |
| std::unique_ptr<SharedMemory::Factory>(new TestSharedMemory::Factory()); |
| std::unique_ptr<ServiceImpl> svc(static_cast<ServiceImpl*>( |
| Service::CreateInstance(std::move(shm_factory), &task_runner).release())); |
| MockProducer mock_producer_1; |
| MockProducer mock_producer_2; |
| std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_1 = |
| svc->ConnectProducer(&mock_producer_1); |
| std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_2 = |
| svc->ConnectProducer(&mock_producer_2); |
| |
| ASSERT_TRUE(producer_endpoint_1); |
| ASSERT_TRUE(producer_endpoint_2); |
| |
| InSequence seq; |
| EXPECT_CALL(mock_producer_1, OnConnect()); |
| EXPECT_CALL(mock_producer_2, OnConnect()); |
| task_runner.RunUntilIdle(); |
| |
| ASSERT_EQ(2u, svc->num_producers()); |
| ASSERT_EQ(producer_endpoint_1.get(), svc->GetProducer(1)); |
| ASSERT_EQ(producer_endpoint_2.get(), svc->GetProducer(2)); |
| |
| DataSourceDescriptor ds_desc1; |
| ds_desc1.set_name("foo"); |
| producer_endpoint_1->RegisterDataSource( |
| ds_desc1, [&task_runner, &producer_endpoint_1](DataSourceID id) { |
| EXPECT_EQ(1u, id); |
| task_runner.PostTask( |
| std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| producer_endpoint_1.get(), id)); |
| }); |
| |
| DataSourceDescriptor ds_desc2; |
| ds_desc2.set_name("bar"); |
| producer_endpoint_2->RegisterDataSource( |
| ds_desc2, [&task_runner, &producer_endpoint_2](DataSourceID id) { |
| EXPECT_EQ(1u, id); |
| task_runner.PostTask( |
| std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| producer_endpoint_2.get(), id)); |
| }); |
| |
| task_runner.RunUntilIdle(); |
| |
| EXPECT_CALL(mock_producer_1, OnDisconnect()); |
| producer_endpoint_1.reset(); |
| task_runner.RunUntilIdle(); |
| Mock::VerifyAndClearExpectations(&mock_producer_1); |
| |
| ASSERT_EQ(1u, svc->num_producers()); |
| ASSERT_EQ(nullptr, svc->GetProducer(1)); |
| |
| EXPECT_CALL(mock_producer_2, OnDisconnect()); |
| producer_endpoint_2.reset(); |
| task_runner.RunUntilIdle(); |
| Mock::VerifyAndClearExpectations(&mock_producer_2); |
| |
| ASSERT_EQ(0u, svc->num_producers()); |
| } |
| |
| } // namespace |
| } // namespace perfetto |