Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FilterEngine.cpp

Issue 29499630: Issue 4938 - fix race conditions and get rid of hacks related to DefaultFileSystem (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 27, 2017, 11:15 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/DefaultFileSystem.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } 65 }
66 }; 66 };
67 67
68 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest; 68 typedef FilterEngineTestGeneric<LazyFileSystem, AdblockPlus::DefaultLogSystem> FilterEngineTest;
69 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData; 69 typedef FilterEngineTestGeneric<NoFilesFileSystem, LazyLogSystem> FilterEngine TestNoData;
70 70
71 class FilterEngineWithFreshFolder : public ::testing::Test 71 class FilterEngineWithFreshFolder : public ::testing::Test
72 { 72 {
73 protected: 73 protected:
74 FileSystemPtr fileSystem; 74 FileSystemPtr fileSystem;
75 std::list<SchedulerTask> fileSystemTasks;
75 std::weak_ptr<JsEngine> weakJsEngine; 76 std::weak_ptr<JsEngine> weakJsEngine;
76 77
77 void SetUp() override 78 void SetUp() override
78 { 79 {
79 fileSystem = CreateDefaultFileSystem(); 80 fileSystem = CreateDefaultFileSystem([this](const SchedulerTask& task)
81 {
82 fileSystemTasks.emplace_back(task);
83 });
80 // Since there is neither in memory FS nor functionality to work with 84 // Since there is neither in memory FS nor functionality to work with
81 // directories use the hack: manually clean the directory. 85 // directories use the hack: manually clean the directory.
82 removeFileIfExists("patterns.ini"); 86 removeFileIfExists("patterns.ini");
83 removeFileIfExists("prefs.json"); 87 removeFileIfExists("prefs.json");
84 } 88 }
85 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 89 JsEnginePtr CreateJsEngine(const AppInfo& appInfo = AppInfo())
86 { 90 {
87 JsEngineCreationParameters jsEngineParams; 91 JsEngineCreationParameters jsEngineParams;
88 jsEngineParams.appInfo = appInfo; 92 jsEngineParams.appInfo = appInfo;
89 jsEngineParams.fileSystem = fileSystem; 93 jsEngineParams.fileSystem = fileSystem;
90 jsEngineParams.logSystem.reset(new LazyLogSystem()); 94 jsEngineParams.logSystem.reset(new LazyLogSystem());
91 jsEngineParams.timer.reset(new NoopTimer()); 95 jsEngineParams.timer.reset(new NoopTimer());
92 jsEngineParams.webRequest.reset(new NoopWebRequest()); 96 jsEngineParams.webRequest.reset(new NoopWebRequest());
93 auto jsEngine = CreateJsEngine(std::move(jsEngineParams)); 97 auto jsEngine = ::CreateJsEngine(std::move(jsEngineParams));
94 weakJsEngine = jsEngine; 98 weakJsEngine = jsEngine;
95 return jsEngine; 99 return jsEngine;
96 } 100 }
101
102 FilterEnginePtr CreateFilterEngine(const JsEnginePtr& jsEngine,
103 const FilterEngine::CreationParameters& creationParams = FilterEngine::Cre ationParameters())
104 {
105 FilterEnginePtr retValue;
106 FilterEngine::CreateAsync(jsEngine, [&retValue](const FilterEnginePtr& fil terEngine)
107 {
108 retValue = filterEngine;
109 }, creationParams);
110 while (!retValue && !fileSystemTasks.empty())
111 {
112 (*fileSystemTasks.begin())();
113 fileSystemTasks.pop_front();
114 }
115 return retValue;
116 }
117
97 void TearDown() override 118 void TearDown() override
98 { 119 {
99 removeFileIfExists("patterns.ini"); 120 removeFileIfExists("patterns.ini");
100 removeFileIfExists("prefs.json"); 121 removeFileIfExists("prefs.json");
101 fileSystem.reset(); 122 fileSystem.reset();
102 } 123 }
103 void removeFileIfExists(const std::string& path) 124 void removeFileIfExists(const std::string& path)
104 { 125 {
105 // Hack: allow IO to finish currently running operations, in particular 126 bool hasStatRun = false;
106 // writing into files. Otherwise we get "Permission denied". 127 bool doesFileExists;
107 auto safeRemove = [this, &path]()->bool 128 fileSystem->Stat(path, [&hasStatRun, &doesFileExists](const IFileSystem::S tatResult& stats, const std::string& error)
108 { 129 {
109 try 130 EXPECT_TRUE(error.empty()) << error;
110 { 131 doesFileExists = stats.exists;
111 Sync sync; 132 hasStatRun = true;
112 auto fs = fileSystem; 133 });
113 fileSystem->Stat(path, 134 while (!hasStatRun && !fileSystemTasks.empty())
114 [fs, &path, &sync](const IFileSystem::StatResult& stats, const std:: string& error) 135 {
115 { 136 (*fileSystemTasks.begin())();
116 if (error.empty() && stats.exists) 137 fileSystemTasks.pop_front();
117 { 138 }
118 fs->Remove(path, [&sync](const std::string& error) 139
119 { 140 if (!doesFileExists)
120 sync.Set(error); 141 return;
121 }); 142
122 } 143 bool hasRemoveRun = false;
123 else 144 fileSystem->Remove(path, [&hasRemoveRun](const std::string& error)
124 sync.Set(error); 145 {
125 }); 146 EXPECT_TRUE(error.empty()) << error;
126 sync.WaitFor(); 147 hasRemoveRun = true;
127 return sync.GetError().empty(); 148 });
128 } 149 while (!hasStatRun && !fileSystemTasks.empty())
129 catch (...) 150 {
130 { 151 (*fileSystemTasks.begin())();
131 return false; 152 fileSystemTasks.pop_front();
132 } 153 }
133 };
134 int i = 5;
135 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove())
136 std::this_thread::sleep_for(std::chrono::seconds(2));
137 } 154 }
138 }; 155 };
139 156
140 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test 157 class FilterEngineIsSubscriptionDownloadAllowedTest : public ::testing::Test
141 { 158 {
142 protected: 159 protected:
143 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes; 160 typedef std::vector<std::pair<bool, std::string>> ConnectionTypes;
144 DelayedWebRequest::SharedTasks webRequestTasks; 161 DelayedWebRequest::SharedTasks webRequestTasks;
145 DelayedTimer::SharedTasks timerTasks; 162 DelayedTimer::SharedTasks timerTasks;
146 FilterEngine::CreationParameters createParams; 163 FilterEngine::CreationParameters createParams;
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted( 677 ASSERT_FALSE(filterEngine->IsElemhideWhitelisted(
661 "http://example.co.uk", 678 "http://example.co.uk",
662 documentUrls1)); 679 documentUrls1));
663 } 680 }
664 681
665 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun) 682 TEST_F(FilterEngineWithFreshFolder, LangAndAASubscriptionsAreChosenOnFirstRun)
666 { 683 {
667 AppInfo appInfo; 684 AppInfo appInfo;
668 appInfo.locale = "zh"; 685 appInfo.locale = "zh";
669 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt"; 686 const std::string langSubscriptionUrl = "https://easylist-downloads.adblockplu s.org/easylistchina+easylist.txt";
670 auto jsEngine = createJsEngine(appInfo); 687 auto jsEngine = CreateJsEngine(appInfo);
671 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 688 auto filterEngine = CreateFilterEngine(jsEngine);
672 const auto subscriptions = filterEngine->GetListedSubscriptions(); 689 const auto subscriptions = filterEngine->GetListedSubscriptions();
673 ASSERT_EQ(2u, subscriptions.size()); 690 ASSERT_EQ(2u, subscriptions.size());
674 std::unique_ptr<Subscription> aaSubscription; 691 std::unique_ptr<Subscription> aaSubscription;
675 std::unique_ptr<Subscription> langSubscription; 692 std::unique_ptr<Subscription> langSubscription;
676 if (subscriptions[0].IsAA()) 693 if (subscriptions[0].IsAA())
677 { 694 {
678 aaSubscription.reset(new Subscription(subscriptions[0])); 695 aaSubscription.reset(new Subscription(subscriptions[0]));
679 langSubscription.reset(new Subscription(subscriptions[1])); 696 langSubscription.reset(new Subscription(subscriptions[1]));
680 } 697 }
681 else if (subscriptions[1].IsAA()) 698 else if (subscriptions[1].IsAA())
682 { 699 {
683 aaSubscription.reset(new Subscription(subscriptions[1])); 700 aaSubscription.reset(new Subscription(subscriptions[1]));
684 langSubscription.reset(new Subscription(subscriptions[0])); 701 langSubscription.reset(new Subscription(subscriptions[0]));
685 } 702 }
686 ASSERT_NE(nullptr, aaSubscription); 703 ASSERT_NE(nullptr, aaSubscription);
687 ASSERT_NE(nullptr, langSubscription); 704 ASSERT_NE(nullptr, langSubscription);
688 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString() ); 705 EXPECT_EQ(langSubscriptionUrl, langSubscription->GetProperty("url").AsString() );
689 EXPECT_TRUE(filterEngine->IsAAEnabled()); 706 EXPECT_TRUE(filterEngine->IsAAEnabled());
690 } 707 }
691 708
692 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 709 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
693 { 710 {
694 auto jsEngine = createJsEngine(); 711 auto jsEngine = CreateJsEngine();
695 FilterEngine::CreationParameters createParams; 712 FilterEngine::CreationParameters createParams;
696 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select", jsEngine->NewValue(false)); 713 createParams.preconfiguredPrefs.emplace("first_run_subscription_auto_select", jsEngine->NewValue(false));
697 auto filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, createParams); 714 auto filterEngine = CreateFilterEngine(jsEngine, createParams);
698 const auto subscriptions = filterEngine->GetListedSubscriptions(); 715 const auto subscriptions = filterEngine->GetListedSubscriptions();
699 EXPECT_EQ(0u, subscriptions.size()); 716 EXPECT_EQ(0u, subscriptions.size());
700 EXPECT_FALSE(filterEngine->IsAAEnabled()); 717 EXPECT_FALSE(filterEngine->IsAAEnabled());
701 } 718 }
702 719
703 namespace AA_ApiTest 720 namespace AA_ApiTest
704 { 721 {
705 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt"; 722 const std::string kOtherSubscriptionUrl = "https://non-existing-subscription.t xt";
706 enum class AAStatus 723 enum class AAStatus
707 { 724 {
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 std::string testConnection = "test connection"; 1041 std::string testConnection = "test connection";
1025 filterEngine->SetAllowedConnectionType(&testConnection); 1042 filterEngine->SetAllowedConnectionType(&testConnection);
1026 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1043 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1027 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1044 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1028 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1045 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1029 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1046 ASSERT_EQ(1u, capturedConnectionTypes.size());
1030 EXPECT_TRUE(capturedConnectionTypes[0].first); 1047 EXPECT_TRUE(capturedConnectionTypes[0].first);
1031 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1048 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1032 } 1049 }
1033 } 1050 }
OLDNEW
« no previous file with comments | « test/DefaultFileSystem.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld