File: transfertest.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (158 lines) | stat: -rw-r--r-- 4,731 bytes parent folder | download
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
/*
    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
    SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
*/

#include <QTest>

#include <future>
#include <thread>

#include "transfer.h"

class TransferTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testSegmentOnSmallFile()
    {
        // Files smaller than our minimal segment size ought to be transferred in one go
        // otherwise we have a chance of degrading performance.
        QCOMPARE(TransferSegment(1).buf.size(), 1);
    }

    void testMaxSegment()
    {
        // Large files may only use up to a given maximum.
        QCOMPARE(TransferSegment(512 * 1024 * 1024).buf.size(), c_maxSegmentSize);
    }

    void testIdealSegmentSize()
    {
        QCOMPARE(TransferSegment(64 * 1024 * 1024).buf.size(), 1342177);
    }

    void testSegment()
    {
        TransferSegment s(8);
        QCOMPARE(s.buf.size(), 8);
        memset(s.buf.data(), 1, 8);
        QCOMPARE(s.buf.data()[0], 1);
    }

    void testRing()
    {
        TransferRingBuffer ring(8);
        for (auto i = 0; i <= 32; ++i) {
            {
                auto s = ring.nextFree();
                memset(s->buf.data(), i, 8);
                ring.push();
            }
            {
                auto s = ring.pop();
                QCOMPARE(s->buf.data()[0], static_cast<char>(i));
                ring.unpop();
            }
        }
    }

    void testRingThreadedSlowPush()
    {
        const auto runs = 127;
        const auto fileSize = 8;
        TransferRingBuffer ring(fileSize);

        std::atomic<bool> abort(false);

        auto pullFuture = std::async(std::launch::async, [&ring, &abort]() -> bool {
            for (auto i = 0; i <= runs && !abort; ++i) {
                auto s = ring.pop();
                if (!QTest::qCompare(s->buf.data()[0],
                                     static_cast<char>(i),
                                     qPrintable(QStringLiteral("On pull iteration %1").arg(i)),
                                     "",
                                     __FILE__,
                                     __LINE__)) {
                    abort = true;
                    return false;
                }
                ring.unpop();
            }
            return true;
        });

        auto pushFuture = std::async(std::launch::async, [&ring, &abort]() -> bool {
            for (auto i = 0; i <= runs && !abort; ++i) {
                auto s = ring.nextFree();
                memset(s->buf.data(), i, fileSize);
                ring.push();
                if (abort) {
                    ring.done();
                    return false;
                }
                // Slow down this thread to simulate slow network reads.
                std::this_thread::sleep_for(std::chrono::milliseconds(5));
            }
            ring.done();
            return true;
        });

        pushFuture.wait();
        pullFuture.wait();

        QVERIFY(pushFuture.get());
        QVERIFY(pullFuture.get());
    }

    void testRingThreadedSlowPull()
    {
        const auto runs = 127;
        const auto fileSize = 8;
        TransferRingBuffer ring(fileSize);

        std::atomic<bool> abort(false);

        auto pullFuture = std::async(std::launch::async, [&ring, &abort]() -> bool {
            for (auto i = 0; i <= runs && !abort; ++i) {
                auto s = ring.pop();
                if (!QTest::qCompare(s->buf.data()[0],
                                     static_cast<char>(i),
                                     qPrintable(QStringLiteral("On pull iteration %1").arg(i)),
                                     "",
                                     __FILE__,
                                     __LINE__)) {
                    abort = true;
                }
                // Slow down this thread to simulate slow local writes.
                std::this_thread::sleep_for(std::chrono::milliseconds(5));
                ring.unpop();
            }
            return true;
        });

        auto pushFuture = std::async(std::launch::async, [&ring, &abort]() -> bool {
            for (auto i = 0; i <= runs && !abort; ++i) {
                auto s = ring.nextFree();
                memset(s->buf.data(), i, fileSize);
                if (abort) {
                    ring.done();
                    return false;
                }
                ring.push();
            }
            ring.done();
            return true;
        });

        pushFuture.wait();
        pullFuture.wait();

        QVERIFY(pushFuture.get());
        QVERIFY(pullFuture.get());
    }
};

QTEST_GUILESS_MAIN(TransferTest)

#include "transfertest.moc"