Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "src/tracing/core/service_impl.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "gmock/gmock.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | #include "perfetto/tracing/core/data_source_config.h" |
| 24 | #include "perfetto/tracing/core/data_source_descriptor.h" |
| 25 | #include "perfetto/tracing/core/producer.h" |
| 26 | #include "perfetto/tracing/core/shared_memory.h" |
| 27 | #include "src/base/test/test_task_runner.h" |
| 28 | #include "src/tracing/test/test_shared_memory.h" |
| 29 | |
| 30 | namespace perfetto { |
| 31 | namespace { |
| 32 | |
| 33 | using ::testing::_; |
| 34 | using ::testing::InSequence; |
| 35 | using ::testing::Mock; |
| 36 | |
| 37 | class MockProducer : public Producer { |
| 38 | public: |
| 39 | ~MockProducer() override {} |
| 40 | |
| 41 | // Producer implementation. |
| 42 | MOCK_METHOD0(OnConnect, void()); |
| 43 | MOCK_METHOD0(OnDisconnect, void()); |
| 44 | MOCK_METHOD2(CreateDataSourceInstance, |
| 45 | void(DataSourceInstanceID, const DataSourceConfig&)); |
| 46 | MOCK_METHOD1(TearDownDataSourceInstance, void(DataSourceInstanceID)); |
| 47 | }; |
| 48 | |
| 49 | TEST(ServiceImpl, RegisterAndUnregister) { |
| 50 | base::TestTaskRunner task_runner; |
| 51 | auto shm_factory = |
| 52 | std::unique_ptr<SharedMemory::Factory>(new TestSharedMemory::Factory()); |
| 53 | std::unique_ptr<ServiceImpl> svc(static_cast<ServiceImpl*>( |
| 54 | Service::CreateInstance(std::move(shm_factory), &task_runner).release())); |
| 55 | MockProducer mock_producer_1; |
| 56 | MockProducer mock_producer_2; |
| 57 | std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_1 = |
| 58 | svc->ConnectProducer(&mock_producer_1); |
| 59 | std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_2 = |
| 60 | svc->ConnectProducer(&mock_producer_2); |
| 61 | |
| 62 | ASSERT_TRUE(producer_endpoint_1); |
| 63 | ASSERT_TRUE(producer_endpoint_2); |
| 64 | |
| 65 | InSequence seq; |
| 66 | EXPECT_CALL(mock_producer_1, OnConnect()); |
| 67 | EXPECT_CALL(mock_producer_2, OnConnect()); |
| 68 | task_runner.RunUntilIdle(); |
| 69 | |
| 70 | ASSERT_EQ(2u, svc->num_producers()); |
| 71 | ASSERT_EQ(producer_endpoint_1.get(), svc->GetProducer(1)); |
| 72 | ASSERT_EQ(producer_endpoint_2.get(), svc->GetProducer(2)); |
| 73 | |
| 74 | producer_endpoint_1->RegisterDataSource( |
| 75 | {"foo"}, [&task_runner, &producer_endpoint_1](DataSourceID id) { |
| 76 | EXPECT_EQ(1u, id); |
| 77 | task_runner.PostTask( |
| 78 | std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| 79 | producer_endpoint_1.get(), id)); |
| 80 | }); |
| 81 | |
| 82 | producer_endpoint_2->RegisterDataSource( |
| 83 | {"bar"}, [&task_runner, &producer_endpoint_2](DataSourceID id) { |
| 84 | EXPECT_EQ(1u, id); |
| 85 | task_runner.PostTask( |
| 86 | std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| 87 | producer_endpoint_2.get(), id)); |
| 88 | }); |
| 89 | |
| 90 | task_runner.RunUntilIdle(); |
| 91 | |
| 92 | EXPECT_CALL(mock_producer_1, OnDisconnect()); |
| 93 | producer_endpoint_1.reset(); |
| 94 | task_runner.RunUntilIdle(); |
| 95 | Mock::VerifyAndClearExpectations(&mock_producer_1); |
| 96 | |
| 97 | ASSERT_EQ(1u, svc->num_producers()); |
| 98 | ASSERT_EQ(nullptr, svc->GetProducer(1)); |
| 99 | |
| 100 | EXPECT_CALL(mock_producer_2, OnDisconnect()); |
| 101 | producer_endpoint_2.reset(); |
| 102 | task_runner.RunUntilIdle(); |
| 103 | Mock::VerifyAndClearExpectations(&mock_producer_2); |
| 104 | |
| 105 | ASSERT_EQ(0u, svc->num_producers()); |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | } // namespace perfetto |