162819106SNicolas Bonnefon #include <QSignalSpy> 262819106SNicolas Bonnefon #include <QMutexLocker> 362819106SNicolas Bonnefon #include <QFile> 462819106SNicolas Bonnefon 562819106SNicolas Bonnefon #include "testlogdata.h" 662819106SNicolas Bonnefon #include "logdata.h" 762819106SNicolas Bonnefon 862819106SNicolas Bonnefon #if QT_VERSION < 0x040500 962819106SNicolas Bonnefon #define QBENCHMARK 1062819106SNicolas Bonnefon #endif 1162819106SNicolas Bonnefon 1262819106SNicolas Bonnefon #if !defined( TMPDIR ) 1362819106SNicolas Bonnefon #define TMPDIR "/tmp" 1462819106SNicolas Bonnefon #endif 1562819106SNicolas Bonnefon 165f0a91faSNicolas Bonnefon static const qint64 VBL_NB_LINES = 4999999LL; 1762819106SNicolas Bonnefon static const int VBL_LINE_PER_PAGE = 70; 18e6d3ab74SNicolas Bonnefon static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %07d\n"; 19e6d3ab74SNicolas Bonnefon static const int VBL_LINE_LENGTH = 84; // Without the final '\n' ! 2062819106SNicolas Bonnefon 215f0a91faSNicolas Bonnefon static const qint64 SL_NB_LINES = 5000LL; 2262819106SNicolas Bonnefon static const int SL_LINE_PER_PAGE = 70; 2362819106SNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 2462819106SNicolas Bonnefon static const int SL_LINE_LENGTH = 83; // Without the final '\n' ! 2562819106SNicolas Bonnefon 2662819106SNicolas Bonnefon void TestLogData::initTestCase() 2762819106SNicolas Bonnefon { 2862819106SNicolas Bonnefon QVERIFY( generateDataFiles() ); 2962819106SNicolas Bonnefon } 3062819106SNicolas Bonnefon 3162819106SNicolas Bonnefon void TestLogData::simpleLoad() 3262819106SNicolas Bonnefon { 3362819106SNicolas Bonnefon LogData logData; 3462819106SNicolas Bonnefon QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) ); 3562819106SNicolas Bonnefon 3662819106SNicolas Bonnefon // Register for notification file is loaded 37*cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 3862819106SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 3962819106SNicolas Bonnefon 4062819106SNicolas Bonnefon QBENCHMARK { 415f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 4262819106SNicolas Bonnefon // Wait for the loading to be done 4362819106SNicolas Bonnefon { 4462819106SNicolas Bonnefon QApplication::exec(); 4562819106SNicolas Bonnefon } 4662819106SNicolas Bonnefon } 4762819106SNicolas Bonnefon QCOMPARE( (qint64) progressSpy.count(), logData.getFileSize() / (5LL*1024*1024) + 1 ); 4862819106SNicolas Bonnefon QCOMPARE( logData.getNbLine(), VBL_NB_LINES ); 4962819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), VBL_LINE_LENGTH ); 5062819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), VBL_NB_LINES * (VBL_LINE_LENGTH+1LL) ); 5162819106SNicolas Bonnefon 5262819106SNicolas Bonnefon // Disconnect all signals 5362819106SNicolas Bonnefon disconnect( &logData, 0 ); 5462819106SNicolas Bonnefon } 5562819106SNicolas Bonnefon 5662819106SNicolas Bonnefon void TestLogData::multipleLoad() 5762819106SNicolas Bonnefon { 5862819106SNicolas Bonnefon LogData logData; 59*cef94aa9SNicolas Bonnefon QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished( bool ) ) ); 6062819106SNicolas Bonnefon 6162819106SNicolas Bonnefon // Register for notification file is loaded 62*cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 6362819106SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 6462819106SNicolas Bonnefon 6562819106SNicolas Bonnefon // Start loading the VBL 665f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 6762819106SNicolas Bonnefon 6862819106SNicolas Bonnefon // Immediately interrupt the loading 6962819106SNicolas Bonnefon logData.interruptLoading(); 7062819106SNicolas Bonnefon 7162819106SNicolas Bonnefon // and wait for the signal 7262819106SNicolas Bonnefon QApplication::exec(); 7362819106SNicolas Bonnefon 7462819106SNicolas Bonnefon // Check we have an empty file 7562819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 1 ); 76*cef94aa9SNicolas Bonnefon // TODO: check loadingFinished arg == false 775f0a91faSNicolas Bonnefon QCOMPARE( logData.getNbLine(), 0LL ); 7862819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), 0 ); 7962819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), 0LL ); 8062819106SNicolas Bonnefon 8162819106SNicolas Bonnefon // Restart the VBL 825f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 8362819106SNicolas Bonnefon 8462819106SNicolas Bonnefon // Ensure the counting has started 85a5fe2cedSNicolas Bonnefon { 8662819106SNicolas Bonnefon QMutex mutex; 8762819106SNicolas Bonnefon QWaitCondition sleep; 88a5fe2cedSNicolas Bonnefon // sleep.wait( &mutex, 10 ); 89a5fe2cedSNicolas Bonnefon } 9062819106SNicolas Bonnefon 9162819106SNicolas Bonnefon // Load the SL (should block until VBL is fully indexed) 925f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/smalllog.txt" ); 9362819106SNicolas Bonnefon 9462819106SNicolas Bonnefon // and wait for the 2 signals (one for each file) 9562819106SNicolas Bonnefon QApplication::exec(); 9662819106SNicolas Bonnefon QApplication::exec(); 9762819106SNicolas Bonnefon 9862819106SNicolas Bonnefon // Check we have the small log loaded 9962819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 3 ); 10062819106SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 10162819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 10262819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 10362819106SNicolas Bonnefon 10462819106SNicolas Bonnefon // Restart the VBL again 1055f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 10662819106SNicolas Bonnefon 10762819106SNicolas Bonnefon // Immediately interrupt the loading 10862819106SNicolas Bonnefon logData.interruptLoading(); 10962819106SNicolas Bonnefon 11062819106SNicolas Bonnefon // and wait for the signal 11162819106SNicolas Bonnefon QApplication::exec(); 11262819106SNicolas Bonnefon 11362819106SNicolas Bonnefon // Check the small log has been restored 11462819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 4 ); 11562819106SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 11662819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 11762819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 11862819106SNicolas Bonnefon 11962819106SNicolas Bonnefon // Disconnect all signals 12062819106SNicolas Bonnefon disconnect( &logData, 0 ); 12162819106SNicolas Bonnefon } 12262819106SNicolas Bonnefon 12362819106SNicolas Bonnefon void TestLogData::changingFile() 12462819106SNicolas Bonnefon { 12562819106SNicolas Bonnefon char newLine[90]; 12662819106SNicolas Bonnefon LogData logData; 12762819106SNicolas Bonnefon 128*cef94aa9SNicolas Bonnefon QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished( bool ) ) ); 12962819106SNicolas Bonnefon QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) ); 1303cf2e1a1SNicolas Bonnefon QSignalSpy changedSpy( &logData, 1313cf2e1a1SNicolas Bonnefon SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ) ); 13262819106SNicolas Bonnefon 13362819106SNicolas Bonnefon // Register for notification file is loaded 134*cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 13562819106SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 13662819106SNicolas Bonnefon 13762819106SNicolas Bonnefon // Generate a small file 13862819106SNicolas Bonnefon QFile file( TMPDIR "/changingfile.txt" ); 13962819106SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 14062819106SNicolas Bonnefon for (int i = 0; i < 200; i++) { 14162819106SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 14262819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 14362819106SNicolas Bonnefon } 14462819106SNicolas Bonnefon } 14562819106SNicolas Bonnefon file.close(); 14662819106SNicolas Bonnefon 14762819106SNicolas Bonnefon // Start loading it 1485f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/changingfile.txt" ); 14962819106SNicolas Bonnefon 15062819106SNicolas Bonnefon // and wait for the signal 15162819106SNicolas Bonnefon QApplication::exec(); 15262819106SNicolas Bonnefon 15362819106SNicolas Bonnefon // Check we have the small file 15462819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 1 ); 1555f0a91faSNicolas Bonnefon QCOMPARE( logData.getNbLine(), 200LL ); 15662819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 15762819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), 200 * (SL_LINE_LENGTH+1LL) ); 15862819106SNicolas Bonnefon 15962819106SNicolas Bonnefon // Add some data to it 16062819106SNicolas Bonnefon if ( file.open( QIODevice::Append ) ) { 16162819106SNicolas Bonnefon for (int i = 0; i < 200; i++) { 16262819106SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 16362819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 16462819106SNicolas Bonnefon } 16562819106SNicolas Bonnefon } 16662819106SNicolas Bonnefon file.close(); 16762819106SNicolas Bonnefon 1683cf2e1a1SNicolas Bonnefon // and wait for the signals 16962819106SNicolas Bonnefon QApplication::exec(); 17062819106SNicolas Bonnefon 17162819106SNicolas Bonnefon // Check we have a bigger file 1723cf2e1a1SNicolas Bonnefon QCOMPARE( changedSpy.count(), 1 ); 17362819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 2 ); 1745f0a91faSNicolas Bonnefon QCOMPARE( logData.getNbLine(), 400LL ); 17562819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 17662819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), 400 * (SL_LINE_LENGTH+1LL) ); 17762819106SNicolas Bonnefon 17862819106SNicolas Bonnefon // Truncate the file 17962819106SNicolas Bonnefon QVERIFY( file.open( QIODevice::WriteOnly ) ); 18062819106SNicolas Bonnefon file.close(); 18162819106SNicolas Bonnefon 1823cf2e1a1SNicolas Bonnefon // and wait for the signals 18362819106SNicolas Bonnefon QApplication::exec(); 18462819106SNicolas Bonnefon 18562819106SNicolas Bonnefon // Check we have an empty file 1863cf2e1a1SNicolas Bonnefon QCOMPARE( changedSpy.count(), 2 ); 18762819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 3 ); 1885f0a91faSNicolas Bonnefon QCOMPARE( logData.getNbLine(), 0LL ); 18962819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), 0 ); 19062819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), 0LL ); 19162819106SNicolas Bonnefon } 19262819106SNicolas Bonnefon 19362819106SNicolas Bonnefon void TestLogData::sequentialRead() 19462819106SNicolas Bonnefon { 195270ea970SNicolas Bonnefon LogData logData; 19662819106SNicolas Bonnefon 197270ea970SNicolas Bonnefon // Register for notification file is loaded 198*cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 199270ea970SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 200270ea970SNicolas Bonnefon 2015f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 2025b29309eSNicolas Bonnefon 203270ea970SNicolas Bonnefon // Wait for the loading to be done 20462819106SNicolas Bonnefon { 205270ea970SNicolas Bonnefon QApplication::exec(); 20662819106SNicolas Bonnefon } 20762819106SNicolas Bonnefon 20862819106SNicolas Bonnefon // Read all lines sequentially 20962819106SNicolas Bonnefon QString s; 21062819106SNicolas Bonnefon QBENCHMARK { 211270ea970SNicolas Bonnefon for (int i = 0; i < VBL_NB_LINES; i++) { 212270ea970SNicolas Bonnefon s = logData.getLineString(i); 21362819106SNicolas Bonnefon } 214270ea970SNicolas Bonnefon } 215270ea970SNicolas Bonnefon QCOMPARE( s.length(), VBL_LINE_LENGTH ); 21662819106SNicolas Bonnefon } 21762819106SNicolas Bonnefon 21862819106SNicolas Bonnefon void TestLogData::randomPageRead() 21962819106SNicolas Bonnefon { 2205b29309eSNicolas Bonnefon LogData logData; 22162819106SNicolas Bonnefon 2225b29309eSNicolas Bonnefon // Register for notification file is loaded 2235b29309eSNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished() ), 2245b29309eSNicolas Bonnefon this, SLOT( loadingFinished() ) ); 2255b29309eSNicolas Bonnefon 2265b29309eSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" ); 2275b29309eSNicolas Bonnefon // Wait for the loading to be done 22862819106SNicolas Bonnefon { 2295b29309eSNicolas Bonnefon QApplication::exec(); 23062819106SNicolas Bonnefon } 23162819106SNicolas Bonnefon 2325b29309eSNicolas Bonnefon QWARN("Starting random page read test"); 2335b29309eSNicolas Bonnefon 2345b29309eSNicolas Bonnefon // Read page by page from the beginning and the end, using the QStringList 2355b29309eSNicolas Bonnefon // function 2365b29309eSNicolas Bonnefon QStringList list; 23762819106SNicolas Bonnefon QBENCHMARK { 2385b29309eSNicolas Bonnefon for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++) 23962819106SNicolas Bonnefon { 2405b29309eSNicolas Bonnefon list = logData.getLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE ); 2415b29309eSNicolas Bonnefon QCOMPARE(list.count(), VBL_LINE_PER_PAGE); 2425b29309eSNicolas Bonnefon int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1; 2435b29309eSNicolas Bonnefon list = logData.getLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE ); 2445b29309eSNicolas Bonnefon QCOMPARE(list.count(), VBL_LINE_PER_PAGE); 24562819106SNicolas Bonnefon } 24662819106SNicolas Bonnefon } 24762819106SNicolas Bonnefon } 24862819106SNicolas Bonnefon 24962819106SNicolas Bonnefon // 25062819106SNicolas Bonnefon // Private functions 25162819106SNicolas Bonnefon // 25262819106SNicolas Bonnefon void TestLogData::loadingFinished() 25362819106SNicolas Bonnefon { 25462819106SNicolas Bonnefon QApplication::quit(); 25562819106SNicolas Bonnefon } 25662819106SNicolas Bonnefon 25762819106SNicolas Bonnefon bool TestLogData::generateDataFiles() 25862819106SNicolas Bonnefon { 25962819106SNicolas Bonnefon char newLine[90]; 26062819106SNicolas Bonnefon 26162819106SNicolas Bonnefon QFile file( TMPDIR "/verybiglog.txt" ); 26262819106SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 26362819106SNicolas Bonnefon for (int i = 0; i < VBL_NB_LINES; i++) { 26462819106SNicolas Bonnefon snprintf(newLine, 89, vbl_format, i); 26562819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 26662819106SNicolas Bonnefon } 26762819106SNicolas Bonnefon } 26862819106SNicolas Bonnefon else { 26962819106SNicolas Bonnefon return false; 27062819106SNicolas Bonnefon } 27162819106SNicolas Bonnefon file.close(); 27262819106SNicolas Bonnefon 27362819106SNicolas Bonnefon file.setFileName( TMPDIR "/smalllog.txt" ); 27462819106SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 27562819106SNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) { 27662819106SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 27762819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 27862819106SNicolas Bonnefon } 27962819106SNicolas Bonnefon } 28062819106SNicolas Bonnefon else { 28162819106SNicolas Bonnefon return false; 28262819106SNicolas Bonnefon } 28362819106SNicolas Bonnefon file.close(); 28462819106SNicolas Bonnefon 28562819106SNicolas Bonnefon return true; 28662819106SNicolas Bonnefon } 287