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

Side by Side Diff: test/FilterEngine.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: Created Jan. 19, 2017, 5:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 #include "JsLatch.h"
19 #include <thread> 20 #include <thread>
20 #include "../src/JsEngineTransition.h" 21 #include "../src/JsEngineTransition.h"
21 22
22 using namespace AdblockPlus; 23 using namespace AdblockPlus;
23 24
24 namespace 25 namespace
25 { 26 {
26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 27 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
27 28
28 class VeryLazyFileSystem : public LazyFileSystem 29 class VeryLazyFileSystem : public LazyFileSystem
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 { 130 {
130 timesCalled++; 131 timesCalled++;
131 } 132 }
132 133
133 private: 134 private:
134 // We currently cannot store timesCalled in the functor, see: 135 // We currently cannot store timesCalled in the functor, see:
135 // https://issues.adblockplus.org/ticket/1378. 136 // https://issues.adblockplus.org/ticket/1378.
136 int& timesCalled; 137 int& timesCalled;
137 }; 138 };
138 139
139 // Workaround for https://issues.adblockplus.org/ticket/1397.
140 void NoOpUpdaterCallback(const std::string&) {}
141
142 class FilterEngineWithFreshFolder : public ::testing::Test 140 class FilterEngineWithFreshFolder : public ::testing::Test
143 { 141 {
144 protected: 142 protected:
145 FileSystemPtr fileSystem; 143 FileSystemPtr fileSystem;
146 std::shared_ptr<LazyWebRequest> webRequest; 144 std::shared_ptr<LazyWebRequest> webRequest;
147 AdblockPlus::JsEnginePtr jsEngine; 145 AdblockPlus::JsEnginePtr jsEngine;
148 146
149 void SetUp() override 147 void SetUp() override
150 { 148 {
151 fileSystem = std::make_shared<DefaultFileSystem>(); 149 fileSystem = std::make_shared<DefaultFileSystem>();
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 \"test\": {\ 547 \"test\": {\
550 \"version\": \"1.0.2\",\ 548 \"version\": \"1.0.2\",\
551 \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\ 549 \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\
552 }\ 550 }\
553 }"; 551 }";
554 552
555 int timesCalled = 0; 553 int timesCalled = 0;
556 MockUpdateAvailableCallback mockUpdateAvailableCallback(timesCalled); 554 MockUpdateAvailableCallback mockUpdateAvailableCallback(timesCalled);
557 555
558 filterEngine->SetUpdateAvailableCallback(mockUpdateAvailableCallback); 556 filterEngine->SetUpdateAvailableCallback(mockUpdateAvailableCallback);
559 filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); 557 Latch latchA(1);
560 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 558 filterEngine->ForceUpdateCheck([&](const std::string&) {latchA.Arrive(); });
559 latchA.Wait();
561 ASSERT_EQ(1, timesCalled); 560 ASSERT_EQ(1, timesCalled);
562 561
563 filterEngine->RemoveUpdateAvailableCallback(); 562 filterEngine->RemoveUpdateAvailableCallback();
564 filterEngine->ForceUpdateCheck(&NoOpUpdaterCallback); 563 Latch latchB(1);
565 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 564 filterEngine->ForceUpdateCheck([&](const std::string&) {latchB.Arrive(); });
565 latchB.Wait();
566 ASSERT_EQ(1, timesCalled); 566 ASSERT_EQ(1, timesCalled);
567 } 567 }
568 568
569 TEST_F(FilterEngineTest, DocumentWhitelisting) 569 TEST_F(FilterEngineTest, DocumentWhitelisting)
570 { 570 {
571 filterEngine->GetFilter("@@||example.org^$document")->AddToList(); 571 filterEngine->GetFilter("@@||example.org^$document")->AddToList();
572 filterEngine->GetFilter("@@||example.com^$document,domain=example.de")->AddToL ist(); 572 filterEngine->GetFilter("@@||example.com^$document,domain=example.de")->AddToL ist();
573 573
574 ASSERT_TRUE(filterEngine->IsDocumentWhitelisted( 574 ASSERT_TRUE(filterEngine->IsDocumentWhitelisted(
575 "http://example.org", 575 "http://example.org",
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 654
655 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun) 655 TEST_F(FilterEngineWithFreshFolder, DisableSubscriptionsAutoSelectOnFirstRun)
656 { 656 {
657 createJsEngine(); 657 createJsEngine();
658 FilterEngine::Prefs preSettings; 658 FilterEngine::Prefs preSettings;
659 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false); 659 preSettings["first_run_subscription_auto_select"] = jsEngine->NewValue(false);
660 auto filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine, preS ettings); 660 auto filterEngine = std::make_shared<AdblockPlus::FilterEngine>(jsEngine, preS ettings);
661 const auto subscriptions = filterEngine->GetListedSubscriptions(); 661 const auto subscriptions = filterEngine->GetListedSubscriptions();
662 EXPECT_EQ(0u, subscriptions.size()); 662 EXPECT_EQ(0u, subscriptions.size());
663 } 663 }
OLDNEW
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/GlobalJsObject.cpp » ('j') | test/GlobalJsObject.cpp » ('J')

Powered by Google App Engine
This is Rietveld