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

Side by Side Diff: test/FilterEngine.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 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
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/JsEngine.cpp » ('j') | 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData; 70 typedef FilterEngineTestGeneric<VeryLazyFileSystem, LazyLogSystem> FilterEngin eTestNoData;
71 71
72 class FilterEngineWithFreshFolder : public ::testing::Test 72 class FilterEngineWithFreshFolder : public ::testing::Test
73 { 73 {
74 protected: 74 protected:
75 FileSystemPtr fileSystem; 75 FileSystemPtr fileSystem;
76 std::weak_ptr<JsEngine> weakJsEngine; 76 std::weak_ptr<JsEngine> weakJsEngine;
77 77
78 void SetUp() override 78 void SetUp() override
79 { 79 {
80 fileSystem.reset(new DefaultFileSystem()); 80 fileSystem = CreateDefaultFileSystem();
81 // Since there is neither in memory FS nor functionality to work with 81 // Since there is neither in memory FS nor functionality to work with
82 // directories use the hack: manually clean the directory. 82 // directories use the hack: manually clean the directory.
83 removeFileIfExists("patterns.ini"); 83 removeFileIfExists("patterns.ini");
84 removeFileIfExists("prefs.json"); 84 removeFileIfExists("prefs.json");
85 } 85 }
86 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo()) 86 JsEnginePtr createJsEngine(const AppInfo& appInfo = AppInfo())
87 { 87 {
88 JsEngineCreationParameters jsEngineParams; 88 JsEngineCreationParameters jsEngineParams;
89 jsEngineParams.appInfo = appInfo; 89 jsEngineParams.appInfo = appInfo;
90 jsEngineParams.fileSystem = fileSystem; 90 jsEngineParams.fileSystem = fileSystem;
(...skipping 11 matching lines...) Expand all
102 fileSystem.reset(); 102 fileSystem.reset();
103 } 103 }
104 void removeFileIfExists(const std::string& path) 104 void removeFileIfExists(const std::string& path)
105 { 105 {
106 // Hack: allow IO to finish currently running operations, in particular 106 // Hack: allow IO to finish currently running operations, in particular
107 // writing into files. Otherwise we get "Permission denied". 107 // writing into files. Otherwise we get "Permission denied".
108 auto safeRemove = [this, &path]()->bool 108 auto safeRemove = [this, &path]()->bool
109 { 109 {
110 try 110 try
111 { 111 {
112 if (fileSystem->Stat(path).exists) 112 Sync sync;
113 fileSystem->Remove(path); 113 auto fs = fileSystem;
114 return true; 114 fileSystem->Stat(path,
115 [fs, &path, &sync](const IFileSystem::StatResult& stats, const std:: string& error)
116 {
117 if (error.empty() && stats.exists)
118 {
119 fs->Remove(path, [&sync](const std::string& error)
120 {
121 sync.Set(error);
122 });
123 }
124 else
125 sync.Set(error);
126 });
127 sync.WaitFor();
128 return sync.GetError().empty();
115 } 129 }
116 catch (...) 130 catch (...)
117 { 131 {
118 return false; 132 return false;
119 } 133 }
120 }; 134 };
121 int i = 5; 135 int i = 5;
122 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove()) 136 while ((i-- > 0 && weakJsEngine.lock()) || !safeRemove())
123 std::this_thread::sleep_for(std::chrono::seconds(2)); 137 std::this_thread::sleep_for(std::chrono::seconds(2));
124 } 138 }
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 { 592 {
579 int timesCalled = 0; 593 int timesCalled = 0;
580 std::this_thread::sleep_for(std::chrono::milliseconds(200)); 594 std::this_thread::sleep_for(std::chrono::milliseconds(200));
581 filterEngine->SetFilterChangeCallback([&timesCalled](const std::string&, Adblo ckPlus::JsValue&&) 595 filterEngine->SetFilterChangeCallback([&timesCalled](const std::string&, Adblo ckPlus::JsValue&&)
582 { 596 {
583 timesCalled++; 597 timesCalled++;
584 }); 598 });
585 filterEngine->GetFilter("foo").AddToList(); 599 filterEngine->GetFilter("foo").AddToList();
586 EXPECT_EQ(1, timesCalled); 600 EXPECT_EQ(1, timesCalled);
587 601
602 // we want to actually check the call count didn't change.
588 filterEngine->RemoveFilterChangeCallback(); 603 filterEngine->RemoveFilterChangeCallback();
589 filterEngine->GetFilter("foo").RemoveFromList(); 604 filterEngine->GetFilter("foo").RemoveFromList();
590 EXPECT_EQ(1, timesCalled); 605 EXPECT_EQ(1, timesCalled);
591 } 606 }
592 607
593 TEST_F(FilterEngineTest, DocumentWhitelisting) 608 TEST_F(FilterEngineTest, DocumentWhitelisting)
594 { 609 {
595 filterEngine->GetFilter("@@||example.org^$document").AddToList(); 610 filterEngine->GetFilter("@@||example.org^$document").AddToList();
596 filterEngine->GetFilter("@@||example.com^$document,domain=example.de").AddToLi st(); 611 filterEngine->GetFilter("@@||example.com^$document,domain=example.de").AddToLi st();
597 612
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 std::string testConnection = "test connection"; 1025 std::string testConnection = "test connection";
1011 filterEngine->SetAllowedConnectionType(&testConnection); 1026 filterEngine->SetAllowedConnectionType(&testConnection);
1012 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); 1027 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB");
1013 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); 1028 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing());
1014 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); 1029 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size());
1015 ASSERT_EQ(1u, capturedConnectionTypes.size()); 1030 ASSERT_EQ(1u, capturedConnectionTypes.size());
1016 EXPECT_TRUE(capturedConnectionTypes[0].first); 1031 EXPECT_TRUE(capturedConnectionTypes[0].first);
1017 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); 1032 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second);
1018 } 1033 }
1019 } 1034 }
OLDNEW
« no previous file with comments | « test/FileSystemJsObject.cpp ('k') | test/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld