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