xref: /glogg/tests/testlogdata.cpp (revision c633ced33b4f2c2cf77c0c80a15fa73a7f13ad9f)
1 /*
2  * Copyright (C) 2009, 2010 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <QSignalSpy>
21 #include <QMutexLocker>
22 #include <QFile>
23 
24 #include "testlogdata.h"
25 #include "logdata.h"
26 
27 #if !defined( TMPDIR )
28 #define TMPDIR "/tmp"
29 #endif
30 
31 static const qint64 VBL_NB_LINES = 4999999LL;
32 static const int VBL_LINE_PER_PAGE = 70;
33 static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%07d\n";
34 static const int VBL_LINE_LENGTH = (76+2+7) ; // Without the final '\n' !
35 static const int VBL_VISIBLE_LINE_LENGTH = (76+8+4+7); // Without the final '\n' !
36 
37 static const qint64 SL_NB_LINES = 5000LL;
38 static const int SL_LINE_PER_PAGE = 70;
39 static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n";
40 static const int SL_LINE_LENGTH = 83; // Without the final '\n' !
41 
42 static const char* partial_line_begin = "123... beginning of line.";
43 static const char* partial_line_end = " end of line 123.\n";
44 
45 void TestLogData::initTestCase()
46 {
47     QVERIFY( generateDataFiles() );
48 }
49 
50 void TestLogData::simpleLoad()
51 {
52     LogData logData;
53     QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) );
54 
55     // Register for notification file is loaded
56     connect( &logData, SIGNAL( loadingFinished( bool ) ),
57             this, SLOT( loadingFinished() ) );
58 
59     QBENCHMARK {
60         logData.attachFile( TMPDIR "/verybiglog.txt" );
61         // Wait for the loading to be done
62         {
63             QApplication::exec();
64         }
65     }
66 
67     // Disconnect all signals
68     disconnect( &logData, 0 );
69 }
70 
71 void TestLogData::multipleLoad()
72 {
73     LogData logData;
74     QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished( bool ) ) );
75 
76     // Register for notification file is loaded
77     connect( &logData, SIGNAL( loadingFinished( bool ) ),
78             this, SLOT( loadingFinished() ) );
79 
80     // Start loading the VBL
81     logData.attachFile( TMPDIR "/verybiglog.txt" );
82 
83     // Immediately interrupt the loading
84     logData.interruptLoading();
85 
86     // and wait for the signal
87     QApplication::exec();
88 
89     // Check we have an empty file
90     QCOMPARE( finishedSpy.count(), 1 );
91     // TODO: check loadingFinished arg == false
92     QCOMPARE( logData.getNbLine(), 0LL );
93     QCOMPARE( logData.getMaxLength(), 0 );
94     QCOMPARE( logData.getFileSize(), 0LL );
95 
96     // Restart the VBL
97     logData.attachFile( TMPDIR "/verybiglog.txt" );
98 
99     // Ensure the counting has started
100     {
101         QMutex mutex;
102         QWaitCondition sleep;
103         // sleep.wait( &mutex, 10 );
104     }
105 
106     // Load the SL (should block until VBL is fully indexed)
107     logData.attachFile( TMPDIR "/smalllog.txt" );
108 
109     // and wait for the 2 signals (one for each file)
110     QApplication::exec();
111     QApplication::exec();
112 
113     // Check we have the small log loaded
114     QCOMPARE( finishedSpy.count(), 3 );
115     QCOMPARE( logData.getNbLine(), SL_NB_LINES );
116     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
117     QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
118 
119     // Restart the VBL again
120     logData.attachFile( TMPDIR "/verybiglog.txt" );
121 
122     // Immediately interrupt the loading
123     logData.interruptLoading();
124 
125     // and wait for the signal
126     QApplication::exec();
127 
128     // Check the small log has been restored
129     QCOMPARE( finishedSpy.count(), 4 );
130     QCOMPARE( logData.getNbLine(), SL_NB_LINES );
131     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
132     QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
133 
134     // Disconnect all signals
135     disconnect( &logData, 0 );
136 }
137 
138 //
139 // Private functions
140 //
141 void TestLogData::loadingFinished()
142 {
143     QApplication::quit();
144 }
145 
146 bool TestLogData::generateDataFiles()
147 {
148     char newLine[90];
149 
150     QFile file( TMPDIR "/verybiglog.txt" );
151     if ( file.open( QIODevice::WriteOnly ) ) {
152         for (int i = 0; i < VBL_NB_LINES; i++) {
153             snprintf(newLine, 89, vbl_format, i);
154             file.write( newLine, qstrlen(newLine) );
155         }
156     }
157     else {
158         return false;
159     }
160     file.close();
161 
162     file.setFileName( TMPDIR "/smalllog.txt" );
163     if ( file.open( QIODevice::WriteOnly ) ) {
164         for (int i = 0; i < SL_NB_LINES; i++) {
165             snprintf(newLine, 89, sl_format, i);
166             file.write( newLine, qstrlen(newLine) );
167         }
168     }
169     else {
170         return false;
171     }
172     file.close();
173 
174     return true;
175 }
176