1
- #include < src/library/testing/unittest/registar.h>
2
- #include < ydb-cpp-sdk/util/system/error.h>
3
-
4
1
#include " pair.h"
5
2
#include " poller.h"
6
3
#include " pollerimpl.h"
7
4
5
+ #include < src/library/testing/unittest/registar.h>
6
+
7
+ #include < ydb-cpp-sdk/util/system/error.h>
8
+
9
+
8
10
Y_UNIT_TEST_SUITE (TSocketPollerTest) {
11
+ // We need to pass a pointer to any data to the tested poller methods.
12
+ // It doesn't matter what kind of data it actually is.
13
+ static int mock_data = 42 ;
14
+
9
15
Y_UNIT_TEST (TestSimple) {
10
16
SOCKET sockets[2 ];
11
17
UNIT_ASSERT (SocketPair (sockets) == 0 );
@@ -14,7 +20,7 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
14
20
TSocketHolder s2 (sockets[1 ]);
15
21
16
22
TSocketPoller poller;
17
- poller.WaitRead (sockets[1 ], ( void *) 17 );
23
+ poller.WaitRead (sockets[1 ], &mock_data );
18
24
19
25
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
20
26
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
@@ -23,7 +29,7 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
23
29
char buf[] = {18 };
24
30
UNIT_ASSERT_VALUES_EQUAL (1 , send (sockets[0 ], buf, 1 , 0 ));
25
31
26
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
32
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
27
33
28
34
UNIT_ASSERT_VALUES_EQUAL (1 , recv (sockets[1 ], buf, 1 , 0 ));
29
35
UNIT_ASSERT_VALUES_EQUAL (18 , buf[0 ]);
@@ -46,15 +52,15 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
46
52
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
47
53
48
54
for (ui32 i = 0 ; i < 3 ; ++i) {
49
- poller.WaitReadOneShot (sockets[1 ], ( void *) 17 );
55
+ poller.WaitReadOneShot (sockets[1 ], &mock_data );
50
56
51
57
char buf[1 ];
52
58
53
59
buf[0 ] = i + 20 ;
54
60
55
61
UNIT_ASSERT_VALUES_EQUAL (1 , send (sockets[0 ], buf, 1 , 0 ));
56
62
57
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
63
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
58
64
59
65
UNIT_ASSERT_VALUES_EQUAL (1 , recv (sockets[1 ], buf, 1 , 0 ));
60
66
UNIT_ASSERT_VALUES_EQUAL (char (i + 20 ), buf[0 ]);
@@ -116,10 +122,10 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
116
122
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
117
123
118
124
for (ui32 i = 0 ; i < 3 ; ++i) {
119
- poller.WaitReadWriteEdgeTriggered (sockets[1 ], ( void *) 17 );
125
+ poller.WaitReadWriteEdgeTriggered (sockets[1 ], &mock_data );
120
126
121
127
// notify about writeble
122
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
128
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
123
129
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
124
130
125
131
char buf[2 ];
@@ -130,19 +136,19 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
130
136
// send one byte
131
137
UNIT_ASSERT_VALUES_EQUAL (1 , send (sockets[0 ], buf, 1 , 0 ));
132
138
133
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
139
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
134
140
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
135
141
136
142
// restart without reading
137
- poller.RestartReadWriteEdgeTriggered (sockets[1 ], ( void *) 17 , false );
143
+ poller.RestartReadWriteEdgeTriggered (sockets[1 ], &mock_data , false );
138
144
139
145
// after restart read and write might generate separate events
140
146
{
141
147
void * events[3 ];
142
148
size_t count = poller.WaitT (events, 3 , TDuration::Zero ());
143
149
UNIT_ASSERT_GE (count, 1 );
144
150
UNIT_ASSERT_LE (count, 2 );
145
- UNIT_ASSERT_VALUES_EQUAL (events[0 ], ( void *) 17 );
151
+ UNIT_ASSERT_VALUES_EQUAL (events[0 ], &mock_data );
146
152
}
147
153
148
154
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
@@ -176,12 +182,12 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
176
182
UNIT_ASSERT_VALUES_EQUAL (EAGAIN, LastSystemError ());
177
183
178
184
// restart after end (noop for epoll)
179
- poller.RestartReadWriteEdgeTriggered (sockets[1 ], ( void *) 17 , true );
185
+ poller.RestartReadWriteEdgeTriggered (sockets[1 ], &mock_data , true );
180
186
181
187
// send and recv byte
182
188
UNIT_ASSERT_VALUES_EQUAL (1 , send (sockets[0 ], buf, 1 , 0 ));
183
189
184
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
190
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
185
191
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
186
192
187
193
// recv and see end
@@ -195,9 +201,9 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
195
201
UNIT_ASSERT_VALUES_EQUAL (1 , send (sockets[0 ], buf, 1 , 0 ));
196
202
197
203
// restart after end (noop for epoll)
198
- poller.RestartReadWriteEdgeTriggered (sockets[1 ], ( void *) 17 , true );
204
+ poller.RestartReadWriteEdgeTriggered (sockets[1 ], &mock_data , true );
199
205
200
- UNIT_ASSERT_VALUES_EQUAL (( void *) 17 , poller.WaitT (TDuration::Zero ()));
206
+ UNIT_ASSERT_VALUES_EQUAL (&mock_data , poller.WaitT (TDuration::Zero ()));
201
207
UNIT_ASSERT_VALUES_EQUAL (nullptr , poller.WaitT (TDuration::Zero ()));
202
208
203
209
UNIT_ASSERT_VALUES_EQUAL (1 , recv (sockets[1 ], buf, 2 , 0 ));
@@ -224,13 +230,13 @@ Y_UNIT_TEST_SUITE(TSocketPollerTest) {
224
230
225
231
using TPoller = TGenericPoller<TEpollPoller<TWithoutLocking>>;
226
232
TPoller poller;
227
- poller.Set (( void *) 17 , s2, CONT_POLL_RDHUP);
233
+ poller.Set (&mock_data , s2, CONT_POLL_RDHUP);
228
234
229
235
TPoller::TEvent e;
230
236
UNIT_ASSERT_VALUES_EQUAL (poller.WaitD (&e, 1 , TDuration::Zero ().ToDeadLine ()), 1 );
231
237
UNIT_ASSERT_EQUAL (TPoller::ExtractStatus (&e), 0 );
232
238
UNIT_ASSERT_EQUAL (TPoller::ExtractFilter (&e), CONT_POLL_RDHUP);
233
- UNIT_ASSERT_EQUAL (TPoller::ExtractEvent (&e), ( void *) 17 );
239
+ UNIT_ASSERT_EQUAL (TPoller::ExtractEvent (&e), &mock_data );
234
240
}
235
241
236
242
Y_UNIT_TEST (TestSetSocketErrors) {
0 commit comments