aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/equeue/equeue_ut.cpp
blob: 47b1029a2f97ef795a09ee83244b4d7c8947e041 (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
155
156
157
158
159
160
161
162
163
164
#include "equeue.h"
#include <library/cpp/threading/equeue/fast/equeue.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/system/event.h>
#include <util/datetime/base.h>
#include <util/generic/vector.h>

Y_UNIT_TEST_SUITE(TElasticQueueTest) {
    const size_t MaxQueueSize = 20;
    const size_t ThreadCount = 10;

    template <typename T>
    THolder<T> MakeQueue();

    template <>
    THolder<TElasticQueue> MakeQueue() {
        return MakeHolder<TElasticQueue>(MakeHolder<TSimpleThreadPool>());
    }

    template <>
    THolder<TFastElasticQueue> MakeQueue() {
        return MakeHolder<TFastElasticQueue>();
    }

    template <typename T>
    struct TEnv {
        static inline THolder<T> Queue;

        struct TQueueSetup {
            TQueueSetup() {
                Queue.Reset(MakeQueue<T>());
                Queue->Start(ThreadCount, MaxQueueSize);
            }
            ~TQueueSetup() {
                Queue->Stop();
            }
        };
    };

    struct TCounters {
        void Reset() {
            Processed = Scheduled = Discarded = Total = 0;
        }

        TAtomic Processed;
        TAtomic Scheduled;
        TAtomic Discarded;
        TAtomic Total;
    };
    static TCounters Counters;

//fill test -- fill queue with "endless" jobs
    TSystemEvent WaitEvent;

    template <typename T>
    void FillTest() {
        Counters.Reset();

        struct TWaitJob: public IObjectInQueue {
            void Process(void*) override {
                WaitEvent.Wait();
                AtomicIncrement(Counters.Processed);
            }
        } job;

        struct TLocalSetup: TEnv<T>::TQueueSetup {
            TLocalSetup() {
                WaitEvent.Reset();
            }
            ~TLocalSetup() {
                WaitEvent.Signal();
            }
        };

        size_t enqueued = 0;
        {
            TLocalSetup setup;
            while (TEnv<T>::Queue->Add(&job) && enqueued < MaxQueueSize + 100) {
                ++enqueued;
            }

            UNIT_ASSERT_VALUES_EQUAL(enqueued, MaxQueueSize);
            UNIT_ASSERT_VALUES_EQUAL(enqueued, TEnv<T>::Queue->ObjectCount());
        }

        UNIT_ASSERT_VALUES_EQUAL(0u, TEnv<T>::Queue->ObjectCount());
        UNIT_ASSERT_VALUES_EQUAL(0u, TEnv<T>::Queue->Size());
        UNIT_ASSERT_VALUES_EQUAL((size_t)Counters.Processed, enqueued);
    }

    Y_UNIT_TEST(FillTest) {
        FillTest<TElasticQueue>();
    }

    Y_UNIT_TEST(FillTestFast) {
        FillTest<TFastElasticQueue>();
    }

//concurrent test -- send many jobs from different threads
    struct TJob: public IObjectInQueue {
        void Process(void*) override {
            AtomicIncrement(Counters.Processed);
        }
    };
    static TJob Job;

    template <typename T>
    static bool TryAdd() {
        AtomicIncrement(Counters.Total);
        if (TEnv<T>::Queue->Add(&Job)) {
            AtomicIncrement(Counters.Scheduled);
            return true;
        } else {
            AtomicIncrement(Counters.Discarded);
            return false;
        }
    }

    const size_t N = 100000;
    static size_t TryCounter;

    template <typename T>
    void ConcurrentTest() {
        Counters.Reset();
        TryCounter = 0;

        struct TSender: public IThreadFactory::IThreadAble {
            void DoExecute() override {
                while ((size_t)AtomicIncrement(TryCounter) <= N) {
                    if (!TryAdd<T>()) {
                        Sleep(TDuration::MicroSeconds(50));
                    }
                }
            }
        } sender;

        {
            typename TEnv<T>::TQueueSetup setup;

            TVector< TAutoPtr<IThreadFactory::IThread> > senders;
            for (size_t i = 0; i < ThreadCount; ++i) {
                senders.push_back(::SystemThreadFactory()->Run(&sender));
            }

            for (size_t i = 0; i < senders.size(); ++i) {
                senders[i]->Join();
            }
        }

        UNIT_ASSERT_VALUES_EQUAL((size_t)Counters.Total, N);
        UNIT_ASSERT_VALUES_EQUAL(Counters.Processed, Counters.Scheduled);
        UNIT_ASSERT_VALUES_EQUAL(Counters.Total, Counters.Scheduled + Counters.Discarded);
    }

    Y_UNIT_TEST(ConcurrentTest) {
        ConcurrentTest<TElasticQueue>();
    }

    Y_UNIT_TEST(ConcurrentTestFast) {
        ConcurrentTest<TFastElasticQueue>();
    }
}