 Issue 29449592:
  Issue 5183 - Provide async interface for FileSystem  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/
    
  
    Issue 29449592:
  Issue 5183 - Provide async interface for FileSystem  (Closed) 
  Base URL: https://hg.adblockplus.org/libadblockplus/| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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.Wait(); | |
| 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 Loading... | |
| 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([×Called](const std::string&, Adblo ckPlus::JsValue&&) | 595 filterEngine->SetFilterChangeCallback([×Called](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. | |
| 603 int previousTimesCalled = timesCalled; | |
| 
sergei
2017/06/16 15:05:56
But its value is still one. Could you move it into
 
hub
2017/06/16 21:52:56
I'll just keep the comment.
 | |
| 588 filterEngine->RemoveFilterChangeCallback(); | 604 filterEngine->RemoveFilterChangeCallback(); | 
| 589 filterEngine->GetFilter("foo").RemoveFromList(); | 605 filterEngine->GetFilter("foo").RemoveFromList(); | 
| 590 EXPECT_EQ(1, timesCalled); | 606 EXPECT_EQ(previousTimesCalled, timesCalled); | 
| 591 } | 607 } | 
| 592 | 608 | 
| 593 TEST_F(FilterEngineTest, DocumentWhitelisting) | 609 TEST_F(FilterEngineTest, DocumentWhitelisting) | 
| 594 { | 610 { | 
| 595 filterEngine->GetFilter("@@||example.org^$document").AddToList(); | 611 filterEngine->GetFilter("@@||example.org^$document").AddToList(); | 
| 596 filterEngine->GetFilter("@@||example.com^$document,domain=example.de").AddToLi st(); | 612 filterEngine->GetFilter("@@||example.com^$document,domain=example.de").AddToLi st(); | 
| 597 | 613 | 
| 598 ASSERT_TRUE(filterEngine->IsDocumentWhitelisted( | 614 ASSERT_TRUE(filterEngine->IsDocumentWhitelisted( | 
| 599 "http://example.org", | 615 "http://example.org", | 
| 600 std::vector<std::string>())); | 616 std::vector<std::string>())); | 
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1010 std::string testConnection = "test connection"; | 1026 std::string testConnection = "test connection"; | 
| 1011 filterEngine->SetAllowedConnectionType(&testConnection); | 1027 filterEngine->SetAllowedConnectionType(&testConnection); | 
| 1012 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); | 1028 auto subscription = EnsureExampleSubscriptionAndForceUpdate("subB"); | 
| 1013 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); | 1029 EXPECT_EQ("synchronize_ok", subscription.GetProperty("downloadStatus").AsStr ing()); | 
| 1014 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 1030 EXPECT_EQ(1u, subscription.GetProperty("filters").AsList().size()); | 
| 1015 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 1031 ASSERT_EQ(1u, capturedConnectionTypes.size()); | 
| 1016 EXPECT_TRUE(capturedConnectionTypes[0].first); | 1032 EXPECT_TRUE(capturedConnectionTypes[0].first); | 
| 1017 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); | 1033 EXPECT_EQ(testConnection, capturedConnectionTypes[0].second); | 
| 1018 } | 1034 } | 
| 1019 } | 1035 } | 
| OLD | NEW |