-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug when scanner could hang if it was destroyed immediately after construction. Fix possible CPU consume in paused state. Fix incorrect counting of inotify watches.
- Loading branch information
1 parent
be5ea1c
commit 4e28440
Showing
4 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
|
||
#include <catch.hpp> | ||
|
||
#include "spacescanner.h" | ||
#include "filepath.h" | ||
#include "filedb.h" | ||
#include "utils.h" | ||
#include "DirHelper.h" | ||
|
||
|
||
TEST_CASE( "Scanner tests", "[scanner]" ) | ||
{ | ||
auto scanner = Utils::make_unique<SpaceScanner>(); | ||
|
||
REQUIRE_FALSE( scanner->is_loaded() ); | ||
|
||
std::unique_ptr<FilePath> curScanPath; | ||
|
||
scanner->getCurrentScanPath(curScanPath); | ||
|
||
REQUIRE( curScanPath == nullptr ); | ||
|
||
SECTION( "Scan custom path" ) | ||
{ | ||
DirHelper dh("TestDir"); | ||
dh.createDir("test"); | ||
dh.createFile("test/test.txt"); | ||
dh.createFile("test/test2.txt"); | ||
dh.createDir("test3"); | ||
dh.createFile("test3/test.txt"); | ||
dh.createFile("test3/test2.txt"); | ||
dh.createFile("test3/test3.txt"); | ||
dh.createFile("test3/test4.txt"); | ||
dh.createDir("test3/test5"); | ||
dh.createFile("test3/test5/test1.txt"); | ||
dh.createFile("test3/test5/test2.txt"); | ||
dh.createFile("test3/test5/test3.txt"); | ||
|
||
REQUIRE( scanner->scan_dir("TestDir2") == ScannerError::CANT_OPEN_DIR ); | ||
REQUIRE( scanner->scan_dir("TestDir") == ScannerError::NONE ); | ||
REQUIRE( scanner->scan_dir("TestDir") == ScannerError::SCAN_RUNNING ); | ||
|
||
REQUIRE( scanner->is_loaded() ); | ||
REQUIRE( scanner->getRootPath() != nullptr ); | ||
REQUIRE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE_FALSE( scanner->isProgressKnown() ); | ||
REQUIRE( scanner->has_changes() ); | ||
REQUIRE( scanner->can_refresh() ); | ||
|
||
//wait for scanner to complete | ||
while ( scanner->get_scan_progress()<100 ) | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
|
||
REQUIRE_FALSE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE( scanner->has_changes() ); | ||
REQUIRE( scanner->can_refresh() ); | ||
|
||
REQUIRE( scanner->getDirCount() == 4 ); | ||
REQUIRE( scanner->getFileCount() == 9 ); | ||
scanner->rescan_dir(FilePath("TestDir")); | ||
REQUIRE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
|
||
|
||
//wait for scanner to complete | ||
while ( scanner->get_scan_progress()<100 ) | ||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
|
||
REQUIRE( scanner->getDirCount() == 4 ); | ||
REQUIRE( scanner->getFileCount() == 9 ); | ||
|
||
dh.createDir("test3/test2"); | ||
dh.createFile("test3/test2/test1.txt"); | ||
dh.createFile("test3/test2/test2.txt"); | ||
dh.createFile("test3/test2/test3.txt"); | ||
dh.createDir("test3/test1"); | ||
dh.createFile("test3/test1/test2.txt"); | ||
dh.createFile("test3/test1/test3.txt"); | ||
//wait for scanner to detect changes | ||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
|
||
REQUIRE( scanner->getDirCount() == 6 ); | ||
REQUIRE( scanner->getFileCount() == 14 ); | ||
|
||
dh.createDir("test3/test10"); | ||
dh.createFile("test3/test10/test2.txt"); | ||
dh.createFile("test3/test10/test3.txt"); | ||
dh.createDir("test3/test10/test2"); | ||
dh.createFile("test3/test10/test2/test1.txt"); | ||
dh.createFile("test3/test10/test2/test2.txt"); | ||
FilePath path("TestDir"); | ||
path.addDir("test3"); | ||
|
||
//wait for scanner to finish | ||
std::this_thread::sleep_for(std::chrono::milliseconds(60)); | ||
REQUIRE_FALSE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE( scanner->get_scan_progress() == 100 ); | ||
|
||
REQUIRE( scanner->getDirCount() == 8 ); | ||
REQUIRE( scanner->getFileCount() == 18 ); | ||
|
||
int64_t watchedNow, limit; | ||
bool exceeded = scanner->getWatcherLimits(watchedNow, limit); | ||
REQUIRE_FALSE( exceeded ); | ||
REQUIRE( watchedNow == scanner->getDirCount() ); | ||
|
||
dh.deleteDir("test3/test10"); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(60)); | ||
scanner->getWatcherLimits(watchedNow, limit); | ||
REQUIRE( watchedNow == scanner->getDirCount() ); | ||
} | ||
SECTION( "Scan root" ) | ||
{ | ||
auto roots = scanner->get_available_roots(); | ||
REQUIRE_FALSE( roots.empty() ); | ||
REQUIRE( scanner->scan_dir(roots.front()) == ScannerError::NONE ); | ||
REQUIRE( scanner->is_loaded() ); | ||
REQUIRE( scanner->getRootPath() != nullptr ); | ||
REQUIRE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE( scanner->isProgressKnown() ); | ||
REQUIRE( scanner->has_changes() ); | ||
REQUIRE( scanner->can_refresh() ); | ||
|
||
//wait for scanner to start scanning | ||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
|
||
SECTION("Pause and stop running scan") | ||
{ | ||
REQUIRE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE( scanner->pauseScan() ); | ||
REQUIRE_FALSE( scanner->canPause() ); | ||
REQUIRE( scanner->canResume() ); | ||
|
||
//wait for scanner to actually pause | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
REQUIRE_FALSE( scanner->canPause() ); | ||
REQUIRE( scanner->canResume() ); | ||
scanner->getCurrentScanPath(curScanPath); | ||
REQUIRE( curScanPath != nullptr ); | ||
scanner->getCurrentScanPath(curScanPath); | ||
REQUIRE( curScanPath != nullptr ); | ||
|
||
REQUIRE( scanner->has_changes() ); | ||
auto db = scanner->getFileDB(); | ||
REQUIRE( db != nullptr ); | ||
auto root = scanner->getRootPath(); | ||
REQUIRE( root != nullptr ); | ||
auto processed = db->processEntry(*root,[](const FileEntry& entry){}); | ||
REQUIRE( processed ); | ||
REQUIRE_FALSE( scanner->has_changes() ); | ||
|
||
REQUIRE( scanner->getDirCount() > 0 ); | ||
REQUIRE( scanner->getFileCount() > 0 ); | ||
|
||
REQUIRE( scanner->resumeScan() ); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
|
||
REQUIRE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
scanner->stop_scan(); | ||
REQUIRE_FALSE( scanner->canPause() ); | ||
REQUIRE_FALSE( scanner->canResume() ); | ||
REQUIRE( scanner->get_scan_progress() == 100 ); | ||
|
||
uint64_t used, available, total; | ||
scanner->getSpace(used, available, total); | ||
REQUIRE( used > 0 ); | ||
REQUIRE( available > 0 ); | ||
REQUIRE( total > 0 ); | ||
REQUIRE( (total-available) >= used ); | ||
} | ||
} | ||
} |