aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/poller_ut.cpp
blob: 1d542f8c67709ec70a8c3909e5d55e2d6ebfdb49 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <library/cpp/testing/unittest/registar.h>
#include <util/system/error.h>
 
#include "pair.h"
#include "poller.h" 
#include "pollerimpl.h"
 
Y_UNIT_TEST_SUITE(TSocketPollerTest) {
    Y_UNIT_TEST(TestSimple) {
        SOCKET sockets[2]; 
        UNIT_ASSERT(SocketPair(sockets) == 0); 
 
        TSocketHolder s1(sockets[0]); 
        TSocketHolder s2(sockets[1]); 
 
        TSocketPoller poller; 
        poller.WaitRead(sockets[1], (void*)17);
 
        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
 
        for (ui32 i = 0; i < 3; ++i) { 
            char buf[] = {18};
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0)); 
 
            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
 
            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0)); 
            UNIT_ASSERT_VALUES_EQUAL(18, buf[0]); 
 
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
        } 
    } 
 
    Y_UNIT_TEST(TestSimpleOneShot) {
        SOCKET sockets[2]; 
        UNIT_ASSERT(SocketPair(sockets) == 0); 
 
        TSocketHolder s1(sockets[0]); 
        TSocketHolder s2(sockets[1]); 
 
        TSocketPoller poller; 
 
        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
 
        for (ui32 i = 0; i < 3; ++i) { 
            poller.WaitReadOneShot(sockets[1], (void*)17);
 
            char buf[1]; 
 
            buf[0] = i + 20; 
 
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0)); 
 
            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
 
            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0)); 
            UNIT_ASSERT_VALUES_EQUAL(char(i + 20), buf[0]); 
 
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
 
            buf[0] = i + 21; 
 
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0)); 
 
            // this fails if socket is not oneshot 
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
 
            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0)); 
            UNIT_ASSERT_VALUES_EQUAL(char(i + 21), buf[0]); 
        } 
    } 
 
    Y_UNIT_TEST(TestItIsSafeToUnregisterUnregisteredDescriptor) {
        SOCKET sockets[2]; 
        UNIT_ASSERT(SocketPair(sockets) == 0); 
 
        TSocketHolder s1(sockets[0]); 
        TSocketHolder s2(sockets[1]); 
 
        TSocketPoller poller; 
 
        poller.Unwait(s1); 
    } 
 
    Y_UNIT_TEST(TestItIsSafeToReregisterDescriptor) {
        SOCKET sockets[2]; 
        UNIT_ASSERT(SocketPair(sockets) == 0); 
 
        TSocketHolder s1(sockets[0]); 
        TSocketHolder s2(sockets[1]); 
 
        TSocketPoller poller; 
 
        poller.WaitRead(s1, nullptr);
        poller.WaitRead(s1, nullptr);
        poller.WaitWrite(s1, nullptr);
    } 

    Y_UNIT_TEST(TestSimpleEdgeTriggered) {
        SOCKET sockets[2];
        UNIT_ASSERT(SocketPair(sockets) == 0);

        TSocketHolder s1(sockets[0]);
        TSocketHolder s2(sockets[1]);

        SetNonBlock(sockets[1]);

        TSocketPoller poller;

        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
        UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

        for (ui32 i = 0; i < 3; ++i) {
            poller.WaitReadWriteEdgeTriggered(sockets[1], (void*)17);

            // notify about writeble
            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            char buf[2];

            buf[0] = i + 10;
            buf[1] = i + 20;

            // send one byte
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));

            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // restart without reading
            poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, false);

            // after restart read and write might generate separate events
            {
                void* events[3];
                size_t count = poller.WaitT(events, 3, TDuration::Zero());
                UNIT_ASSERT_GE(count, 1);
                UNIT_ASSERT_LE(count, 2);
                UNIT_ASSERT_VALUES_EQUAL(events[0], (void*)17);
            }

            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // second two more bytes
            UNIT_ASSERT_VALUES_EQUAL(2, send(sockets[0], buf, 2, 0));

            // here poller could notify or not because we haven't seen end
            Y_UNUSED(poller.WaitT(TDuration::Zero()));

            // recv one, leave two
            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 1, 0));
            UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);

            // nothing new
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // recv the rest
            UNIT_ASSERT_VALUES_EQUAL(2, recv(sockets[1], buf, 2, 0));
            UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);
            UNIT_ASSERT_VALUES_EQUAL(char(i + 20), buf[1]);

            // still nothing new
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // hit end
            ClearLastSystemError();
            UNIT_ASSERT_VALUES_EQUAL(-1, recv(sockets[1], buf, 1, 0));
            UNIT_ASSERT_VALUES_EQUAL(EAGAIN, LastSystemError());

            // restart after end (noop for epoll)
            poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, true);

            // send and recv byte
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));

            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // recv and see end
            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 2, 0));
            UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);

            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            // the same but send before restart
            UNIT_ASSERT_VALUES_EQUAL(1, send(sockets[0], buf, 1, 0));

            // restart after end (noop for epoll)
            poller.RestartReadWriteEdgeTriggered(sockets[1], (void*)17, true);

            UNIT_ASSERT_VALUES_EQUAL((void*)17, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            UNIT_ASSERT_VALUES_EQUAL(1, recv(sockets[1], buf, 2, 0));
            UNIT_ASSERT_VALUES_EQUAL(char(i + 10), buf[0]);

            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));
            UNIT_ASSERT_VALUES_EQUAL(nullptr, poller.WaitT(TDuration::Zero()));

            poller.Unwait(sockets[1]);
        }
    }

#if defined(HAVE_EPOLL_POLLER)
    Y_UNIT_TEST(TestRdhup) {
        SOCKET sockets[2];
        UNIT_ASSERT(SocketPair(sockets) == 0);

        TSocketHolder s1(sockets[0]);
        TSocketHolder s2(sockets[1]);

        char buf[1] = {0};
        UNIT_ASSERT_VALUES_EQUAL(1, send(s1, buf, 1, 0));
        shutdown(s1, SHUT_WR);

        using TPoller = TGenericPoller<TEpollPoller<TWithoutLocking>>;
        TPoller poller;
        poller.Set((void*)17, s2, CONT_POLL_RDHUP);

        TPoller::TEvent e;
        UNIT_ASSERT_VALUES_EQUAL(poller.WaitD(&e, 1, TDuration::Zero().ToDeadLine()), 1);
        UNIT_ASSERT_EQUAL(TPoller::ExtractStatus(&e), 0);
        UNIT_ASSERT_EQUAL(TPoller::ExtractFilter(&e), CONT_POLL_RDHUP);
        UNIT_ASSERT_EQUAL(TPoller::ExtractEvent(&e), (void*)17);
    }
#endif
}