xref: /glogg/tests/testlogdata.cpp (revision 5f0a91faa9e2f6de15eb03c110e6167cbc9b5b3a) !
1 #include <QSignalSpy>
2 #include <QMutexLocker>
3 #include <QFile>
4 
5 #include "testlogdata.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 qint64 VBL_NB_LINES = 4999999LL;
17 static const int VBL_LINE_PER_PAGE = 70;
18 static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %07d\n";
19 static const int VBL_LINE_LENGTH = 84; // Without the final '\n' !
20 
21 static const qint64 SL_NB_LINES = 5000LL;
22 static const int SL_LINE_PER_PAGE = 70;
23 static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n";
24 static const int SL_LINE_LENGTH = 83; // Without the final '\n' !
25 
26 void TestLogData::initTestCase()
27 {
28     QVERIFY( generateDataFiles() );
29 }
30 
31 void TestLogData::simpleLoad()
32 {
33     LogData logData;
34     QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) );
35 
36     // Register for notification file is loaded
37     connect( &logData, SIGNAL( loadingFinished() ),
38             this, SLOT( loadingFinished() ) );
39 
40     QBENCHMARK {
41         logData.attachFile( TMPDIR "/verybiglog.txt" );
42         // Wait for the loading to be done
43         {
44             QApplication::exec();
45         }
46     }
47     QCOMPARE( (qint64) progressSpy.count(), logData.getFileSize() / (5LL*1024*1024) + 1 );
48     QCOMPARE( logData.getNbLine(), VBL_NB_LINES );
49     QCOMPARE( logData.getMaxLength(), VBL_LINE_LENGTH );
50     QCOMPARE( logData.getFileSize(), VBL_NB_LINES * (VBL_LINE_LENGTH+1LL) );
51 
52     // Disconnect all signals
53     disconnect( &logData, 0 );
54 }
55 
56 void TestLogData::multipleLoad()
57 {
58     LogData logData;
59     QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished() ) );
60 
61     // Register for notification file is loaded
62     connect( &logData, SIGNAL( loadingFinished() ),
63             this, SLOT( loadingFinished() ) );
64 
65     // Start loading the VBL
66     logData.attachFile( TMPDIR "/verybiglog.txt" );
67 
68     // Immediately interrupt the loading
69     logData.interruptLoading();
70 
71     // and wait for the signal
72     QApplication::exec();
73 
74     // Check we have an empty file
75     QCOMPARE( finishedSpy.count(), 1 );
76     QCOMPARE( logData.getNbLine(), 0LL );
77     QCOMPARE( logData.getMaxLength(), 0 );
78     QCOMPARE( logData.getFileSize(), 0LL );
79 
80     // Restart the VBL
81     logData.attachFile( TMPDIR "/verybiglog.txt" );
82 
83     // Ensure the counting has started
84     {
85         QMutex mutex;
86         QWaitCondition sleep;
87         // sleep.wait( &mutex, 10 );
88     }
89 
90     // Load the SL (should block until VBL is fully indexed)
91     logData.attachFile( TMPDIR "/smalllog.txt" );
92 
93     // and wait for the 2 signals (one for each file)
94     QApplication::exec();
95     QApplication::exec();
96 
97     // Check we have the small log loaded
98     QCOMPARE( finishedSpy.count(), 3 );
99     QCOMPARE( logData.getNbLine(), SL_NB_LINES );
100     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
101     QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
102 
103     // Restart the VBL again
104     logData.attachFile( TMPDIR "/verybiglog.txt" );
105 
106     // Immediately interrupt the loading
107     logData.interruptLoading();
108 
109     // and wait for the signal
110     QApplication::exec();
111 
112     // Check the small log has been restored
113     QCOMPARE( finishedSpy.count(), 4 );
114     QCOMPARE( logData.getNbLine(), SL_NB_LINES );
115     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
116     QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
117 
118     // Disconnect all signals
119     disconnect( &logData, 0 );
120 }
121 
122 void TestLogData::changingFile()
123 {
124     char newLine[90];
125     LogData logData;
126 
127     QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished() ) );
128     QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) );
129     QSignalSpy changedSpy( &logData,
130             SIGNAL( fileChanged( LogData::MonitoredFileStatus ) ) );
131 
132     // Register for notification file is loaded
133     connect( &logData, SIGNAL( loadingFinished() ),
134             this, SLOT( loadingFinished() ) );
135 
136     // Generate a small file
137     QFile file( TMPDIR "/changingfile.txt" );
138     if ( file.open( QIODevice::WriteOnly ) ) {
139         for (int i = 0; i < 200; i++) {
140             snprintf(newLine, 89, sl_format, i);
141             file.write( newLine, qstrlen(newLine) );
142         }
143     }
144     file.close();
145 
146     // Start loading it
147     logData.attachFile( TMPDIR "/changingfile.txt" );
148 
149     // and wait for the signal
150     QApplication::exec();
151 
152     // Check we have the small file
153     QCOMPARE( finishedSpy.count(), 1 );
154     QCOMPARE( logData.getNbLine(), 200LL );
155     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
156     QCOMPARE( logData.getFileSize(), 200 * (SL_LINE_LENGTH+1LL) );
157 
158     // Add some data to it
159     if ( file.open( QIODevice::Append ) ) {
160         for (int i = 0; i < 200; i++) {
161             snprintf(newLine, 89, sl_format, i);
162             file.write( newLine, qstrlen(newLine) );
163         }
164     }
165     file.close();
166 
167     // and wait for the signals
168     QApplication::exec();
169 
170     // Check we have a bigger file
171     QCOMPARE( changedSpy.count(), 1 );
172     QCOMPARE( finishedSpy.count(), 2 );
173     QCOMPARE( logData.getNbLine(), 400LL );
174     QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
175     QCOMPARE( logData.getFileSize(), 400 * (SL_LINE_LENGTH+1LL) );
176 
177     // Truncate the file
178     QVERIFY( file.open( QIODevice::WriteOnly ) );
179     file.close();
180 
181     // and wait for the signals
182     QApplication::exec();
183 
184     // Check we have an empty file
185     QCOMPARE( changedSpy.count(), 2 );
186     QCOMPARE( finishedSpy.count(), 3 );
187     QCOMPARE( logData.getNbLine(), 0LL );
188     QCOMPARE( logData.getMaxLength(), 0 );
189     QCOMPARE( logData.getFileSize(), 0LL );
190 }
191 
192 void TestLogData::sequentialRead()
193 {
194     LogData logData;
195 
196     // Register for notification file is loaded
197     connect( &logData, SIGNAL( loadingFinished() ),
198             this, SLOT( loadingFinished() ) );
199 
200     logData.attachFile( TMPDIR "/verybiglog.txt" );
201 
202     // Wait for the loading to be done
203     {
204         QApplication::exec();
205     }
206 
207     // Read all lines sequentially
208     QString s;
209     QBENCHMARK {
210         for (int i = 0; i < VBL_NB_LINES; i++) {
211             s = logData.getLineString(i);
212         }
213     }
214     QCOMPARE( s.length(), VBL_LINE_LENGTH );
215 }
216 
217 void TestLogData::randomPageRead()
218 {
219     LogData logData;
220 
221     // Register for notification file is loaded
222     connect( &logData, SIGNAL( loadingFinished() ),
223             this, SLOT( loadingFinished() ) );
224 
225     logData.attachFile( TMPDIR "/verybiglog.txt" );
226     // Wait for the loading to be done
227     {
228         QApplication::exec();
229     }
230 
231     QWARN("Starting random page read test");
232 
233     // Read page by page from the beginning and the end, using the QStringList
234     // function
235     QStringList list;
236     QBENCHMARK {
237         for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++)
238         {
239             list = logData.getLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
240             QCOMPARE(list.count(), VBL_LINE_PER_PAGE);
241             int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1;
242             list = logData.getLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
243             QCOMPARE(list.count(), VBL_LINE_PER_PAGE);
244         }
245     }
246 }
247 
248 //
249 // Private functions
250 //
251 void TestLogData::loadingFinished()
252 {
253     QApplication::quit();
254 }
255 
256 bool TestLogData::generateDataFiles()
257 {
258     char newLine[90];
259 
260     QFile file( TMPDIR "/verybiglog.txt" );
261     if ( file.open( QIODevice::WriteOnly ) ) {
262         for (int i = 0; i < VBL_NB_LINES; i++) {
263             snprintf(newLine, 89, vbl_format, i);
264             file.write( newLine, qstrlen(newLine) );
265         }
266     }
267     else {
268         return false;
269     }
270     file.close();
271 
272     file.setFileName( TMPDIR "/smalllog.txt" );
273     if ( file.open( QIODevice::WriteOnly ) ) {
274         for (int i = 0; i < SL_NB_LINES; i++) {
275             snprintf(newLine, 89, sl_format, i);
276             file.write( newLine, qstrlen(newLine) );
277         }
278     }
279     else {
280         return false;
281     }
282     file.close();
283 
284     return true;
285 }
286