Skip to content

Commit

Permalink
tst_QFile::largeUncFileSupport(): Use QTRY_VERIFY() to open the file
Browse files Browse the repository at this point in the history
Open failures due to sharing violations have been observed in Coin.

Change-Id: If7fbe01a454b3c343c0b87f73db50c28eae901c3
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
  • Loading branch information
FriedemannKleint committed Oct 27, 2017
1 parent cd45d0f commit 1c3dc8c
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions tests/auto/corelib/io/qfile/tst_qfile.cpp
Expand Up @@ -1615,6 +1615,27 @@ void tst_QFile::writeTextFile()
}

#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
// Helper for executing QFile::open() with warning in QTRY_VERIFY(), which evaluates the condition
// multiple times
static bool qFileOpen(QFile &file, QIODevice::OpenMode ioFlags)
{
const bool result = file.isOpen() || file.open(ioFlags);
if (!result)
qWarning() << "Cannot open" << file.fileName() << ':' << file.errorString();
return result;
}

// Helper for executing fopen() with warning in QTRY_VERIFY(), which evaluates the condition
// multiple times
static bool fOpen(const QByteArray &fileName, const char *mode, FILE **file)
{
if (*file == nullptr)
*file = fopen(fileName.constData(), mode);
if (*file == nullptr)
qWarning("Cannot open %s: %s", fileName.constData(), strerror(errno));
return *file != nullptr;
}

void tst_QFile::largeUncFileSupport()
{
qint64 size = Q_INT64_C(8589934592);
Expand All @@ -1629,15 +1650,18 @@ void tst_QFile::largeUncFileSupport()
QVERIFY2(file.exists(), msgFileDoesNotExist(largeFile));

QCOMPARE(file.size(), size);
QVERIFY2(file.open(QIODevice::ReadOnly), msgOpenFailed(file).constData());
// Retry in case of sharing violation
QTRY_VERIFY2(qFileOpen(file, QIODevice::ReadOnly), msgOpenFailed(file).constData());
QCOMPARE(file.size(), size);
QVERIFY(file.seek(dataOffset));
QCOMPARE(file.read(knownData.size()), knownData);
}
{
// 2) stdlib file handling.
StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb"));
QVERIFY(fh);
FILE *fhF = nullptr;
// Retry in case of sharing violation
QTRY_VERIFY(fOpen(largeFileEncoded, "rb", &fhF));
StdioFileGuard fh(fhF);
QFile file;
QVERIFY(file.open(fh, QIODevice::ReadOnly));
QCOMPARE(file.size(), size);
Expand All @@ -1646,8 +1670,10 @@ void tst_QFile::largeUncFileSupport()
}
{
// 3) stdio file handling.
StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb"));
QVERIFY(fh);
FILE *fhF = nullptr;
// Retry in case of sharing violation
QTRY_VERIFY(fOpen(largeFileEncoded, "rb", &fhF));
StdioFileGuard fh(fhF);
int fd = int(_fileno(fh));
QFile file;
QVERIFY(file.open(fd, QIODevice::ReadOnly));
Expand Down

0 comments on commit 1c3dc8c

Please sign in to comment.