1 /* 2 * Copyright (C) 2018 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/traced/service/lazy_producer.h" 18 19 #include "src/base/test/test_task_runner.h" 20 21 #include "perfetto/tracing/core/data_source_config.h" 22 23 #include "gmock/gmock.h" 24 #include "gtest/gtest.h" 25 26 namespace perfetto { 27 namespace { 28 29 constexpr const char* kDataSourceName = "android.heapprofd"; 30 constexpr const char* kPropertyName = "persist.heapprofd.enable"; 31 32 using ::testing::_; 33 using ::testing::InSequence; 34 using ::testing::Return; 35 using ::testing::InvokeWithoutArgs; 36 37 class MockLazyProducer : public LazyProducer { 38 public: 39 MockLazyProducer(base::TaskRunner* task_runner) 40 : LazyProducer(task_runner, 0, kDataSourceName, kPropertyName) {} 41 42 MOCK_METHOD2(SetAndroidProperty, 43 bool(const std::string&, const std::string&)); 44 }; 45 46 TEST(LazyProducersTest, Simple) { 47 DataSourceConfig cfg; 48 cfg.set_name(kDataSourceName); 49 base::TestTaskRunner task_runner; 50 auto done = task_runner.CreateCheckpoint("done"); 51 MockLazyProducer p(&task_runner); 52 InSequence s; 53 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "1")).WillOnce(Return(true)); 54 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "0")) 55 .WillOnce(InvokeWithoutArgs([&done]() { 56 done(); 57 return true; 58 })); 59 p.SetupDataSource(1, cfg); 60 p.StopDataSource(1); 61 task_runner.RunUntilCheckpoint("done"); 62 } 63 64 TEST(LazyProducersTest, RefCount) { 65 DataSourceConfig cfg; 66 cfg.set_name(kDataSourceName); 67 base::TestTaskRunner task_runner; 68 auto done = task_runner.CreateCheckpoint("done"); 69 MockLazyProducer p(&task_runner); 70 InSequence s; 71 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "1")) 72 .WillRepeatedly(Return(true)); 73 p.SetupDataSource(1, cfg); 74 p.SetupDataSource(2, cfg); 75 p.StopDataSource(2); 76 task_runner.RunUntilIdle(); 77 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "0")) 78 .WillOnce(InvokeWithoutArgs([&done]() { 79 done(); 80 return true; 81 })); 82 p.StopDataSource(1); 83 task_runner.RunUntilCheckpoint("done"); 84 } 85 86 TEST(LazyProducersTest, NoFlap) { 87 DataSourceConfig cfg; 88 cfg.set_name(kDataSourceName); 89 base::TestTaskRunner task_runner; 90 auto done = task_runner.CreateCheckpoint("done"); 91 MockLazyProducer p(&task_runner); 92 InSequence s; 93 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "1")) 94 .WillRepeatedly(Return(true)); 95 p.SetupDataSource(1, cfg); 96 p.StopDataSource(1); 97 p.SetupDataSource(2, cfg); 98 task_runner.RunUntilIdle(); 99 p.StopDataSource(2); 100 EXPECT_CALL(p, SetAndroidProperty(kPropertyName, "0")) 101 .WillOnce(InvokeWithoutArgs([&done]() { 102 done(); 103 return true; 104 })); 105 task_runner.RunUntilCheckpoint("done"); 106 } 107 108 } // namespace 109 } // namespace perfetto 110