1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
/*
* Copyright (C) 2009, 2010, 2011 Nicolas Bonnefon and other contributors
*
* This file is part of glogg.
*
* glogg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* glogg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with glogg. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QSignalSpy>
#include <QMutexLocker>
#include <QFile>
#include "testlogfiltereddata.h"
#include "logdata.h"
#include "logfiltereddata.h"
#if QT_VERSION < 0x040500
#define QBENCHMARK
#endif
#if !defined( TMPDIR )
#define TMPDIR "/tmp"
#endif
static const qint64 ML_NB_LINES = 15000LL;
static const char* ml_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%06d\n";
static const int ML_VISIBLE_LINE_LENGTH = (76+8+4+6); // Without the final '\n' !
static const qint64 SL_NB_LINES = 2000LL;
static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n";
static const char* partial_line_begin = "123... beginning of line.";
static const char* partial_line_end = " end of line 123.\n";
static const char* partial_nonmatching_line_begin = "Beginning of line.";
TestLogFilteredData::TestLogFilteredData() : QObject(),
loadingFinishedMutex_(),
searchProgressedMutex_(),
loadingFinishedCondition_(),
searchProgressedCondition_()
{
loadingFinished_received_ = false;
loadingFinished_read_ = false;
searchProgressed_received_ = false;
searchProgressed_read_ = false;
}
void TestLogFilteredData::initTestCase()
{
QVERIFY( generateDataFiles() );
}
void TestLogFilteredData::simpleSearch()
{
logData_ = new LogData();
// Register for notification file is loaded
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinished() ) );
filteredData_ = logData_->getNewFilteredData();
connect( filteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( searchProgressed( int, int ) ) );
QFuture<void> future = QtConcurrent::run(this, &TestLogFilteredData::simpleSearchTest);
QApplication::exec();
disconnect( filteredData_, 0 );
disconnect( logData_, 0 );
delete filteredData_;
delete logData_;
}
void TestLogFilteredData::simpleSearchTest()
{
// First load the tests file
logData_->attachFile( TMPDIR "/mediumlog.txt" );
// Wait for the loading to be done
waitLoadingFinished();
QCOMPARE( logData_->getNbLine(), ML_NB_LINES );
signalLoadingFinishedRead();
// Now perform a simple search
qint64 matches[] = { 0, 15, 20, 135 };
QBENCHMARK {
// Start the search
filteredData_->runSearch( QRegExp( "123" ) );
// And check we receive data in 4 chunks (the first being empty)
for ( int i = 0; i < 4; i++ ) {
std::pair<int,int> progress = waitSearchProgressed();
// FIXME: The test for this is unfortunately not reliable
// (race conditions)
// QCOMPARE( (qint64) progress.first, matches[i] );
signalSearchProgressedRead();
}
}
QCOMPARE( filteredData_->getNbLine(), matches[3] );
// Check the search
QCOMPARE( filteredData_->isLineInMatchingList( 123 ), true );
QCOMPARE( filteredData_->isLineInMatchingList( 124 ), false );
QCOMPARE( filteredData_->getMaxLength(), ML_VISIBLE_LINE_LENGTH );
QCOMPARE( filteredData_->getLineLength( 12 ), ML_VISIBLE_LINE_LENGTH );
QCOMPARE( filteredData_->getNbLine(), 135LL );
// Line beyond limit
QCOMPARE( filteredData_->isLineInMatchingList( 60000 ), false );
QCOMPARE( filteredData_->getMatchingLineNumber( 0 ), 123LL );
// Now let's try interrupting a search
filteredData_->runSearch( QRegExp( "123" ) );
// ... wait for two chunks.
waitSearchProgressed();
signalSearchProgressedRead();
// and interrupt!
filteredData_->interruptSearch();
{
std::pair<int,int> progress;
do {
progress = waitSearchProgressed();
signalSearchProgressedRead();
} while ( progress.second < 100 );
// (because there is no guarantee when the search is
// interrupted, we are not sure how many chunk of result
// we will get.)
}
QApplication::quit();
}
void TestLogFilteredData::multipleSearch()
{
logData_ = new LogData();
// Register for notification file is loaded
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinished() ) );
filteredData_ = logData_->getNewFilteredData();
connect( filteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( searchProgressed( int, int ) ) );
QFuture<void> future = QtConcurrent::run(this, &TestLogFilteredData::multipleSearchTest);
QApplication::exec();
disconnect( filteredData_, 0 );
disconnect( logData_, 0 );
delete filteredData_;
delete logData_;
}
void TestLogFilteredData::multipleSearchTest()
{
// First load the tests file
logData_->attachFile( TMPDIR "/smalllog.txt" );
// Wait for the loading to be done
waitLoadingFinished();
QCOMPARE( logData_->getNbLine(), SL_NB_LINES );
signalLoadingFinishedRead();
// Performs two searches in a row
// Start the search, and immediately another one
// (the second call should block until the first search is done)
filteredData_->runSearch( QRegExp( "1234" ) );
filteredData_->runSearch( QRegExp( "123" ) );
for ( int i = 0; i < 3; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// We should have the result for the 2nd search after the last chunk
waitSearchProgressed();
QCOMPARE( filteredData_->getNbLine(), 12LL );
signalSearchProgressedRead();
// Now a tricky one: we run a search and immediately attach a new file
/* FIXME: sometimes we receive loadingFinished before searchProgressed
* -> deadlock in the test.
filteredData_->runSearch( QRegExp( "123" ) );
waitSearchProgressed();
signalSearchProgressedRead();
logData_->attachFile( TMPDIR "/mediumlog.txt" );
// We don't expect meaningful results but it should not crash!
for ( int i = 0; i < 1; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
*/
sleep(10);
QApplication::quit();
}
void TestLogFilteredData::updateSearch()
{
logData_ = new LogData();
// Register for notification file is loaded
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinished() ) );
filteredData_ = logData_->getNewFilteredData();
connect( filteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( searchProgressed( int, int ) ) );
QFuture<void> future = QtConcurrent::run(this, &TestLogFilteredData::updateSearchTest);
QApplication::exec();
disconnect( filteredData_, 0 );
disconnect( logData_, 0 );
delete filteredData_;
delete logData_;
}
void TestLogFilteredData::updateSearchTest()
{
// First load the tests file
logData_->attachFile( TMPDIR "/smalllog.txt" );
// Wait for the loading to be done
waitLoadingFinished();
QCOMPARE( logData_->getNbLine(), SL_NB_LINES );
signalLoadingFinishedRead();
// Perform a first search
filteredData_->runSearch( QRegExp( "123" ) );
for ( int i = 0; i < 2; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// Check the result
QCOMPARE( filteredData_->getNbLine(), 12LL );
sleep(1);
QWARN("Starting stage 2");
// Add some data to the file
char newLine[90];
QFile file( TMPDIR "/smalllog.txt" );
if ( file.open( QIODevice::Append ) ) {
for (int i = 0; i < 3000; i++) {
snprintf(newLine, 89, sl_format, i);
file.write( newLine, qstrlen(newLine) );
}
// To test the edge case when the final line is not complete and matching
file.write( partial_line_begin, qstrlen( partial_line_begin ) );
}
file.close();
// Let the system do the update (there might be several ones)
do {
waitLoadingFinished();
signalLoadingFinishedRead();
} while ( logData_->getNbLine() < 5001LL );
sleep(1);
// Start an update search
filteredData_->updateSearch();
for ( int i = 0; i < 2; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// Check the result
QCOMPARE( logData_->getNbLine(), 5001LL );
QCOMPARE( filteredData_->getNbLine(), 26LL );
QWARN("Starting stage 3");
// Add a couple more lines, including the end of the unfinished one.
if ( file.open( QIODevice::Append ) ) {
file.write( partial_line_end, qstrlen( partial_line_end ) );
for (int i = 0; i < 20; i++) {
snprintf(newLine, 89, sl_format, i);
file.write( newLine, qstrlen(newLine) );
}
// To test the edge case when the final line is not complete and not matching
file.write( partial_nonmatching_line_begin,
qstrlen( partial_nonmatching_line_begin ) );
}
file.close();
// Let the system do the update
waitLoadingFinished();
signalLoadingFinishedRead();
// Start an update search
filteredData_->updateSearch();
for ( int i = 0; i < 2; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// Check the result
QCOMPARE( logData_->getNbLine(), 5022LL );
QCOMPARE( filteredData_->getNbLine(), 26LL );
QWARN("Starting stage 4");
// Now test the case where a match is found at the end of an updated line.
if ( file.open( QIODevice::Append ) ) {
file.write( partial_line_end, qstrlen( partial_line_end ) );
for (int i = 0; i < 20; i++) {
snprintf(newLine, 89, sl_format, i);
file.write( newLine, qstrlen(newLine) );
}
}
file.close();
// Let the system do the update
waitLoadingFinished();
signalLoadingFinishedRead();
// Start an update search
filteredData_->updateSearch();
for ( int i = 0; i < 2; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// Check the result
QCOMPARE( logData_->getNbLine(), 5042LL );
QCOMPARE( filteredData_->getNbLine(), 27LL );
QApplication::quit();
}
void TestLogFilteredData::marks()
{
logData_ = new LogData();
// Register for notification file is loaded
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinished() ) );
filteredData_ = logData_->getNewFilteredData();
connect( filteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( searchProgressed( int, int ) ) );
QFuture<void> future = QtConcurrent::run(this, &TestLogFilteredData::marksTest);
QApplication::exec();
disconnect( filteredData_, 0 );
disconnect( logData_, 0 );
delete filteredData_;
delete logData_;
}
void TestLogFilteredData::marksTest()
{
// First load the tests file
logData_->attachFile( TMPDIR "/smalllog.txt" );
// Wait for the loading to be done
waitLoadingFinished();
QCOMPARE( logData_->getNbLine(), SL_NB_LINES );
signalLoadingFinishedRead();
// First check no line is marked
for ( int i = 0; i < SL_NB_LINES; i++ )
QVERIFY( filteredData_->isLineMarked( i ) == false );
// Try to create some "out of limit" marks
filteredData_->addMark( -10 );
filteredData_->addMark( SL_NB_LINES + 25 );
// Check no line is marked still
for ( int i = 0; i < SL_NB_LINES; i++ )
QVERIFY( filteredData_->isLineMarked( i ) == false );
// Create a couple of unnamed marks
filteredData_->addMark( 10 );
filteredData_->addMark( 44 ); // This one will also be a match
filteredData_->addMark( 25 );
// Check they are marked
QVERIFY( filteredData_->isLineMarked( 10 ) );
QVERIFY( filteredData_->isLineMarked( 25 ) );
QVERIFY( filteredData_->isLineMarked( 44 ) );
// But others are not
QVERIFY( filteredData_->isLineMarked( 15 ) == false );
QVERIFY( filteredData_->isLineMarked( 20 ) == false );
QCOMPARE( filteredData_->getNbLine(), 3LL );
// Performs a search
QSignalSpy progressSpy( filteredData_,
SIGNAL( searchProgressed( int, int ) ) );
filteredData_->runSearch( QRegExp( "0000.4" ) );
for ( int i = 0; i < 1; i++ ) {
waitSearchProgressed();
signalSearchProgressedRead();
}
// We should have the result of the search and the marks
waitSearchProgressed();
QCOMPARE( filteredData_->getNbLine(), 12LL );
signalSearchProgressedRead();
QString startline = "LOGDATA is a part of glogg, we are going to test it thoroughly, this is line ";
QCOMPARE( filteredData_->getLineString(0), startline + "000004" );
QCOMPARE( filteredData_->getLineString(1), startline + "000010" );
QCOMPARE( filteredData_->getLineString(2), startline + "000014" );
QCOMPARE( filteredData_->getLineString(3), startline + "000024" );
QCOMPARE( filteredData_->getLineString(4), startline + "000025" );
QCOMPARE( filteredData_->getLineString(5), startline + "000034" );
QCOMPARE( filteredData_->getLineString(6), startline + "000044" );
QCOMPARE( filteredData_->getLineString(7), startline + "000054" );
QCOMPARE( filteredData_->getLineString(8), startline + "000064" );
QCOMPARE( filteredData_->getLineString(9), startline + "000074" );
QCOMPARE( filteredData_->getLineString(10), startline + "000084" );
QCOMPARE( filteredData_->getLineString(11), startline + "000094" );
filteredData_->setVisibility( LogFilteredData::MatchesOnly );
QCOMPARE( filteredData_->getNbLine(), 10LL );
QCOMPARE( filteredData_->getLineString(0), startline + "000004" );
QCOMPARE( filteredData_->getLineString(1), startline + "000014" );
QCOMPARE( filteredData_->getLineString(2), startline + "000024" );
QCOMPARE( filteredData_->getLineString(3), startline + "000034" );
QCOMPARE( filteredData_->getLineString(4), startline + "000044" );
QCOMPARE( filteredData_->getLineString(5), startline + "000054" );
QCOMPARE( filteredData_->getLineString(6), startline + "000064" );
QCOMPARE( filteredData_->getLineString(7), startline + "000074" );
QCOMPARE( filteredData_->getLineString(8), startline + "000084" );
QCOMPARE( filteredData_->getLineString(9), startline + "000094" );
filteredData_->setVisibility( LogFilteredData::MarksOnly );
QCOMPARE( filteredData_->getNbLine(), 3LL );
QCOMPARE( filteredData_->getLineString(0), startline + "000010" );
QCOMPARE( filteredData_->getLineString(1), startline + "000025" );
QCOMPARE( filteredData_->getLineString(2), startline + "000044" );
// Another test with marks only
filteredData_->clearSearch();
filteredData_->clearMarks();
filteredData_->setVisibility( LogFilteredData::MarksOnly );
filteredData_->addMark(18);
filteredData_->addMark(19);
filteredData_->addMark(20);
QCOMPARE( filteredData_->getMatchingLineNumber(0), 18LL );
QCOMPARE( filteredData_->getMatchingLineNumber(1), 19LL );
QCOMPARE( filteredData_->getMatchingLineNumber(2), 20LL );
QApplication::quit();
}
void TestLogFilteredData::lineLength()
{
logData_ = new LogData();
// Register for notification file is loaded
connect( logData_, SIGNAL( loadingFinished( bool ) ),
this, SLOT( loadingFinished() ) );
filteredData_ = logData_->getNewFilteredData();
connect( filteredData_, SIGNAL( searchProgressed( int, int ) ),
this, SLOT( searchProgressed( int, int ) ) );
QFuture<void> future = QtConcurrent::run(this, &TestLogFilteredData::lineLengthTest);
QApplication::exec();
disconnect( filteredData_, 0 );
disconnect( logData_, 0 );
delete filteredData_;
delete logData_;
}
void TestLogFilteredData::lineLengthTest()
{
// Line length tests
logData_->attachFile( TMPDIR "/length_test.txt" );
// Wait for the loading to be done
waitLoadingFinished();
QCOMPARE( logData_->getNbLine(), 4LL );
signalLoadingFinishedRead();
// Performs a search (the two middle lines matche)
filteredData_->setVisibility( LogFilteredData::MatchesOnly );
filteredData_->runSearch( QRegExp( "longer" ) );
std::pair<int,int> progress;
do {
progress = waitSearchProgressed();
signalSearchProgressedRead();
QWARN("progress");
} while ( progress.second < 100 );
filteredData_->addMark( 3 );
QCOMPARE( filteredData_->getNbLine(), 2LL );
QCOMPARE( filteredData_->getMaxLength(), 40 );
filteredData_->setVisibility( LogFilteredData::MarksAndMatches );
QCOMPARE( filteredData_->getNbLine(), 3LL );
QCOMPARE( filteredData_->getMaxLength(), 103 );
filteredData_->setVisibility( LogFilteredData::MarksOnly );
QCOMPARE( filteredData_->getNbLine(), 1LL );
QCOMPARE( filteredData_->getMaxLength(), 103 );
filteredData_->addMark( 0 );
QCOMPARE( filteredData_->getNbLine(), 2LL );
QCOMPARE( filteredData_->getMaxLength(), 103 );
filteredData_->deleteMark( 3 );
QCOMPARE( filteredData_->getNbLine(), 1LL );
QCOMPARE( filteredData_->getMaxLength(), 27 );
filteredData_->setVisibility( LogFilteredData::MarksAndMatches );
QCOMPARE( filteredData_->getMaxLength(), 40 );
QApplication::quit();
}
//
// Private functions
//
void TestLogFilteredData::loadingFinished()
{
QMutexLocker locker( &loadingFinishedMutex_ );
QWARN("loadingFinished");
loadingFinished_received_ = true;
loadingFinished_read_ = false;
loadingFinishedCondition_.wakeOne();
// Wait for the test thread to read the signal
while ( ! loadingFinished_read_ )
loadingFinishedCondition_.wait( locker.mutex() );
}
void TestLogFilteredData::searchProgressed( int nbMatches, int completion )
{
QMutexLocker locker( &searchProgressedMutex_ );
QWARN("searchProgressed");
searchProgressed_received_ = true;
searchProgressed_read_ = false;
searchLastMatches_ = nbMatches;
searchLastProgress_ = completion;
searchProgressedCondition_.wakeOne();
// Wait for the test thread to read the signal
while ( ! searchProgressed_read_ )
searchProgressedCondition_.wait( locker.mutex() );
}
std::pair<int,int> TestLogFilteredData::waitSearchProgressed()
{
QMutexLocker locker( &searchProgressedMutex_ );
while ( ! searchProgressed_received_ )
searchProgressedCondition_.wait( locker.mutex() );
QWARN("searchProgressed Received");
return std::pair<int,int>(searchLastMatches_, searchLastProgress_);
}
void TestLogFilteredData::waitLoadingFinished()
{
QMutexLocker locker( &loadingFinishedMutex_ );
while ( ! loadingFinished_received_ )
loadingFinishedCondition_.wait( locker.mutex() );
QWARN("loadingFinished Received");
}
void TestLogFilteredData::signalSearchProgressedRead()
{
QMutexLocker locker( &searchProgressedMutex_ );
searchProgressed_received_ = false;
searchProgressed_read_ = true;
searchProgressedCondition_.wakeOne();
}
void TestLogFilteredData::signalLoadingFinishedRead()
{
QMutexLocker locker( &loadingFinishedMutex_ );
loadingFinished_received_ = false;
loadingFinished_read_ = true;
loadingFinishedCondition_.wakeOne();
}
bool TestLogFilteredData::generateDataFiles()
{
char newLine[90];
QFile file( TMPDIR "/mediumlog.txt" );
if ( file.open( QIODevice::WriteOnly ) ) {
for (int i = 0; i < ML_NB_LINES; i++) {
snprintf(newLine, 89, ml_format, i);
file.write( newLine, qstrlen(newLine) );
}
}
else {
return false;
}
file.close();
file.setFileName( TMPDIR "/smalllog.txt" );
if ( file.open( QIODevice::WriteOnly ) ) {
for (int i = 0; i < SL_NB_LINES; i++) {
snprintf(newLine, 89, sl_format, i);
file.write( newLine, qstrlen(newLine) );
}
}
else {
return false;
}
file.close();
file.setFileName( TMPDIR "/length_test.txt" );
if ( file.open( QIODevice::WriteOnly ) ) {
file.write( "This line is 27 characters.\n" );
file.write( "This line is longer: 36 characters.\n" );
file.write( "This line is even longer: 40 characters.\n" );
file.write( "This line is very long, it's actually hard to count but it is\
probably something around 103 characters.\n" );
}
file.close();
return true;
}
|