1 #include <QSignalSpy> 2 #include <QMutexLocker> 3 #include <QFile> 4 5 #include "testlogfiltereddata.h" 6 #include "logdata.h" 7 8 #if QT_VERSION < 0x040500 9 #define QBENCHMARK 10 #endif 11 12 #if !defined( TMPDIR ) 13 #define TMPDIR "/tmp" 14 #endif 15 16 static const int ML_NB_LINES = 15000; 17 static const char* ml_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 18 19 static const int SL_NB_LINES = 2000; 20 static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 21 22 void TestLogFilteredData::initTestCase() 23 { 24 QVERIFY( generateDataFiles() ); 25 } 26 27 void TestLogFilteredData::simpleSearch() 28 { 29 LogData logData; 30 31 // First load the tests file 32 // Register for notification file is loaded 33 connect( &logData, SIGNAL( loadingFinished() ), 34 this, SLOT( loadingFinished() ) ); 35 36 QVERIFY( logData.attachFile( TMPDIR "/mediumlog.txt" ) ); 37 // Wait for the loading to be done 38 { 39 QApplication::exec(); 40 } 41 42 QCOMPARE( logData.getNbLine(), ML_NB_LINES ); 43 44 // Now perform a simple search 45 LogFilteredData* filteredData = logData.getNewFilteredData(); 46 connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 47 this, SLOT( searchProgressed( int, int ) ) ); 48 49 QSignalSpy progressSpy( filteredData, SIGNAL( searchProgressed( int, int ) ) ); 50 51 int matches[] = { 0, 15, 20, 135 }; 52 QBENCHMARK { 53 // Start the search 54 filteredData->runSearch( QRegExp( "123" ) ); 55 56 // And check we receive data in 4 chunks (the first being empty) 57 for ( int i = 0; i < 4; i++ ) { 58 QApplication::exec(); 59 QCOMPARE( filteredData->getNbLine(), matches[i] ); 60 } 61 } 62 63 QCOMPARE( progressSpy.count(), 4 ); 64 65 // Check the search 66 QCOMPARE( filteredData->isLineInMatchingList( 123 ), true ); 67 QCOMPARE( filteredData->isLineInMatchingList( 124 ), false ); 68 // Line beyond limit 69 QCOMPARE( filteredData->isLineInMatchingList( 60000 ), false ); 70 QCOMPARE( filteredData->getMatchingLineNumber( 0 ), 123 ); 71 72 // Now let's try interrupting a search 73 filteredData->runSearch( QRegExp( "123" ) ); 74 // ... wait for two chunks. 75 QApplication::exec(); 76 QApplication::exec(); 77 // and interrupt! 78 filteredData->interruptSearch(); 79 QCOMPARE( filteredData->getNbLine(), matches[1] ); 80 QApplication::exec(); 81 // After interrupt: should be 100% and the same number of matches 82 QCOMPARE( filteredData->getNbLine(), matches[2] ); 83 84 // (because there is no guarantee when the search is 85 // interrupted, we are not sure how many chunk of result 86 // we will get.) 87 88 // Disconnect all signals 89 disconnect( &logData, 0 ); 90 91 // Destroy the filtered data 92 delete filteredData; 93 } 94 95 void TestLogFilteredData::multipleSearch() 96 { 97 LogData logData; 98 99 // First load the tests file 100 // Register for notification file is loaded 101 connect( &logData, SIGNAL( loadingFinished() ), 102 this, SLOT( loadingFinished() ) ); 103 104 QVERIFY( logData.attachFile( TMPDIR "/smalllog.txt" ) ); 105 // Wait for the loading to be done 106 { 107 QApplication::exec(); 108 } 109 110 QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 111 112 // Performs two searches in a row 113 LogFilteredData* filteredData = logData.getNewFilteredData(); 114 connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 115 this, SLOT( searchProgressed( int, int ) ) ); 116 117 QSignalSpy progressSpy( filteredData, 118 SIGNAL( searchProgressed( int, int ) ) ); 119 120 // Start the search, and immediately another one 121 // (the second call should block until the first search is done) 122 filteredData->runSearch( QRegExp( "1234" ) ); 123 filteredData->runSearch( QRegExp( "123" ) ); 124 125 for ( int i = 0; i < 3; i++ ) 126 QApplication::exec(); 127 128 // We should have the result for the 2nd search 129 QCOMPARE( filteredData->getNbLine(), 12 ); 130 131 QCOMPARE( progressSpy.count(), 4 ); 132 133 // Now a tricky one: we run a search and immediately attach a new file 134 filteredData->runSearch( QRegExp( "123" ) ); 135 QApplication::exec(); 136 logData.attachFile( TMPDIR "/mediumlog.txt" ); 137 138 // We don't expect meaningful results but it should not crash! 139 for ( int i = 0; i < 2; i++ ) 140 QApplication::exec(); 141 142 // Disconnect all signals 143 disconnect( &logData, 0 ); 144 145 // Destroy the filtered data 146 delete filteredData; 147 } 148 149 // 150 // Private functions 151 // 152 void TestLogFilteredData::loadingFinished() 153 { 154 QApplication::quit(); 155 } 156 157 void TestLogFilteredData::searchProgressed( int nbMatches, int completion ) 158 { 159 QApplication::quit(); 160 } 161 162 bool TestLogFilteredData::generateDataFiles() 163 { 164 char newLine[90]; 165 166 QFile file( TMPDIR "/mediumlog.txt" ); 167 if ( file.open( QIODevice::WriteOnly ) ) { 168 for (int i = 0; i < ML_NB_LINES; i++) { 169 snprintf(newLine, 89, ml_format, i); 170 file.write( newLine, qstrlen(newLine) ); 171 } 172 } 173 else { 174 return false; 175 } 176 file.close(); 177 178 file.setFileName( TMPDIR "/smalllog.txt" ); 179 if ( file.open( QIODevice::WriteOnly ) ) { 180 for (int i = 0; i < SL_NB_LINES; i++) { 181 snprintf(newLine, 89, sl_format, i); 182 file.write( newLine, qstrlen(newLine) ); 183 } 184 } 185 else { 186 return false; 187 } 188 file.close(); 189 190 return true; 191 } 192