aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/queue/unordered_ut.cpp
blob: a43b7f520e5f074db5a21acd52e3d61bcf16e8cb (plain) (blame)
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
#include <library/cpp/testing/unittest/registar.h>
#include <util/system/thread.h>
#include <algorithm>
#include <util/generic/vector.h>
#include <util/random/fast.h>

#include "ut_helpers.h"

template <typename TQueueType>
class TTestUnorderedQueue: public TTestBase {
private:
    using TLink = TIntrusiveLink;

    UNIT_TEST_SUITE_DEMANGLE(TTestUnorderedQueue<TQueueType>);
    UNIT_TEST(Push1M_Pop1M_Unordered)
    UNIT_TEST_SUITE_END();

public:
    void Push1M_Pop1M_Unordered() {
        constexpr int REPEAT = 1000000;
        TQueueType queue;
        TLink msg[REPEAT];

        auto pmsg = queue.Pop();
        UNIT_ASSERT_VALUES_EQUAL(pmsg, nullptr);

        for (int i = 0; i < REPEAT; ++i) {
            queue.Push(&msg[i]);
        }

        TVector<TLink*> popped;
        popped.reserve(REPEAT);
        for (int i = 0; i < REPEAT; ++i) {
            popped.push_back((TLink*)queue.Pop());
        }

        pmsg = queue.Pop();
        UNIT_ASSERT_VALUES_EQUAL(pmsg, nullptr);

        std::sort(popped.begin(), popped.end());
        for (int i = 0; i < REPEAT; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(&msg[i], popped[i]);
        }
    }
};

template <typename TQueueType>
class TTestWeakQueue: public TTestBase {
private:
    UNIT_TEST_SUITE_DEMANGLE(TTestWeakQueue<TQueueType>);
    UNIT_TEST(Threads8_Rnd_Exchange)
    UNIT_TEST_SUITE_END();

public:
    template <ui16 COUNT = 48, ui32 MSG_COUNT = 10000>
    void ManyThreadsRndExchange() {
        TQueueType queues[COUNT];

        class TWorker: public ISimpleThread {
        public:
            TWorker(
                TQueueType* queues_,
                ui16 mine,
                TAtomic* pushDone)
                : Queues(queues_)
                , MineQueue(mine)
                , PushDone(pushDone)
            {
            }

            TQueueType* Queues;
            ui16 MineQueue;
            TVector<uintptr_t> Received;
            TAtomic* PushDone;

            void* ThreadProc() override {
                TReallyFastRng32 rng(GetCycleCount());
                Received.reserve(MSG_COUNT * 2);

                for (ui32 loop = 1; loop <= MSG_COUNT; ++loop) {
                    for (;;) {
                        auto msg = Queues[MineQueue].Pop();
                        if (msg == nullptr) {
                            break;
                        }

                        Received.push_back((uintptr_t)msg);
                    }

                    ui16 rnd = rng.GenRand64() % COUNT;
                    ui64 msg = ((ui64)MineQueue << 32) + loop;
                    while (!Queues[rnd].Push((void*)msg)) {
                    }
                }

                AtomicIncrement(*PushDone);

                for (;;) {
                    bool isItLast = AtomicGet(*PushDone) == COUNT;
                    auto msg = Queues[MineQueue].Pop();
                    if (msg != nullptr) {
                        Received.push_back((uintptr_t)msg);
                    } else {
                        if (isItLast) {
                            break;
                        }
                        SpinLockPause();
                    }
                }

                for (ui64 last = 0;;) {
                    auto msg = Queues[MineQueue].UnsafeScanningPop(&last);
                    if (msg == nullptr) {
                        break;
                    }
                    Received.push_back((uintptr_t)msg);
                }

                return nullptr;
            }
        };

        TVector<TAutoPtr<TWorker>> workers;
        TAtomic pushDone = 0;

        for (ui32 i = 0; i < COUNT; ++i) {
            workers.emplace_back(new TWorker(&queues[0], i, &pushDone));
            workers.back()->Start();
        }

        TVector<uintptr_t> all;
        for (ui32 i = 0; i < COUNT; ++i) {
            workers[i]->Join();
            all.insert(all.begin(),
                       workers[i]->Received.begin(), workers[i]->Received.end());
        }

        std::sort(all.begin(), all.end());
        auto iter = all.begin();
        for (ui32 i = 0; i < COUNT; ++i) {
            for (ui32 k = 1; k <= MSG_COUNT; ++k) {
                UNIT_ASSERT_VALUES_EQUAL(((ui64)i << 32) + k, *iter);
                ++iter;
            }
        }
    }

    void Threads8_Rnd_Exchange() {
        ManyThreadsRndExchange<8>();
    }
};

REGISTER_TESTS_FOR_ALL_UNORDERED_QUEUES(TTestUnorderedQueue);
UNIT_TEST_SUITE_REGISTRATION(TTestWeakQueue<TMPMCUnorderedRing>);