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 { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 31 | using ::testing::_; |
| 32 | using ::testing::InSequence; |
| 33 | using ::testing::Mock; |
| 34 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame^] | 35 | namespace { |
| 36 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame^] | 49 | } // namespace |
| 50 | |
| 51 | TEST(ServiceImplTest, RegisterAndUnregister) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 52 | base::TestTaskRunner task_runner; |
| 53 | auto shm_factory = |
| 54 | std::unique_ptr<SharedMemory::Factory>(new TestSharedMemory::Factory()); |
| 55 | std::unique_ptr<ServiceImpl> svc(static_cast<ServiceImpl*>( |
| 56 | Service::CreateInstance(std::move(shm_factory), &task_runner).release())); |
| 57 | MockProducer mock_producer_1; |
| 58 | MockProducer mock_producer_2; |
| 59 | std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_1 = |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame^] | 60 | svc->ConnectProducer(&mock_producer_1, 123u /* uid */); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 61 | std::unique_ptr<Service::ProducerEndpoint> producer_endpoint_2 = |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame^] | 62 | svc->ConnectProducer(&mock_producer_2, 456u /* uid */); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 63 | |
| 64 | ASSERT_TRUE(producer_endpoint_1); |
| 65 | ASSERT_TRUE(producer_endpoint_2); |
| 66 | |
| 67 | InSequence seq; |
| 68 | EXPECT_CALL(mock_producer_1, OnConnect()); |
| 69 | EXPECT_CALL(mock_producer_2, OnConnect()); |
| 70 | task_runner.RunUntilIdle(); |
| 71 | |
| 72 | ASSERT_EQ(2u, svc->num_producers()); |
| 73 | ASSERT_EQ(producer_endpoint_1.get(), svc->GetProducer(1)); |
| 74 | ASSERT_EQ(producer_endpoint_2.get(), svc->GetProducer(2)); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame^] | 75 | ASSERT_EQ(123u, svc->GetProducer(1)->uid_); |
| 76 | ASSERT_EQ(456u, svc->GetProducer(2)->uid_); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 77 | |
Primiano Tucci | 114b649 | 2017-12-11 23:09:45 +0000 | [diff] [blame] | 78 | DataSourceDescriptor ds_desc1; |
| 79 | ds_desc1.set_name("foo"); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 80 | producer_endpoint_1->RegisterDataSource( |
Primiano Tucci | 114b649 | 2017-12-11 23:09:45 +0000 | [diff] [blame] | 81 | ds_desc1, [&task_runner, &producer_endpoint_1](DataSourceID id) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 82 | EXPECT_EQ(1u, id); |
| 83 | task_runner.PostTask( |
| 84 | std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| 85 | producer_endpoint_1.get(), id)); |
| 86 | }); |
| 87 | |
Primiano Tucci | 114b649 | 2017-12-11 23:09:45 +0000 | [diff] [blame] | 88 | DataSourceDescriptor ds_desc2; |
| 89 | ds_desc2.set_name("bar"); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 90 | producer_endpoint_2->RegisterDataSource( |
Primiano Tucci | 114b649 | 2017-12-11 23:09:45 +0000 | [diff] [blame] | 91 | ds_desc2, [&task_runner, &producer_endpoint_2](DataSourceID id) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 92 | EXPECT_EQ(1u, id); |
| 93 | task_runner.PostTask( |
| 94 | std::bind(&Service::ProducerEndpoint::UnregisterDataSource, |
| 95 | producer_endpoint_2.get(), id)); |
| 96 | }); |
| 97 | |
| 98 | task_runner.RunUntilIdle(); |
| 99 | |
| 100 | EXPECT_CALL(mock_producer_1, OnDisconnect()); |
| 101 | producer_endpoint_1.reset(); |
| 102 | task_runner.RunUntilIdle(); |
| 103 | Mock::VerifyAndClearExpectations(&mock_producer_1); |
| 104 | |
| 105 | ASSERT_EQ(1u, svc->num_producers()); |
| 106 | ASSERT_EQ(nullptr, svc->GetProducer(1)); |
| 107 | |
| 108 | EXPECT_CALL(mock_producer_2, OnDisconnect()); |
| 109 | producer_endpoint_2.reset(); |
| 110 | task_runner.RunUntilIdle(); |
| 111 | Mock::VerifyAndClearExpectations(&mock_producer_2); |
| 112 | |
| 113 | ASSERT_EQ(0u, svc->num_producers()); |
| 114 | } |
| 115 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 116 | } // namespace perfetto |