libuv-svace-build

Форк
0
/
test-tcp-close-after-read-timeout.c 
183 строки · 5.0 Кб
1
/* Copyright libuv project and contributors. All rights reserved.
2
 *
3
 * Permission is hereby granted, free of charge, to any person obtaining a copy
4
 * of this software and associated documentation files (the "Software"), to
5
 * deal in the Software without restriction, including without limitation the
6
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
 * sell copies of the Software, and to permit persons to whom the Software is
8
 * furnished to do so, subject to the following conditions:
9
 *
10
 * The above copyright notice and this permission notice shall be included in
11
 * all copies or substantial portions of the Software.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
 * IN THE SOFTWARE.
20
 */
21

22
#include "uv.h"
23
#include "task.h"
24

25
static uv_tcp_t client;
26
static uv_tcp_t connection;
27
static uv_connect_t connect_req;
28
static uv_timer_t timer;
29

30
static int read_cb_called;
31
static int on_close_called;
32

33
static void on_connection(uv_stream_t* server, int status);
34

35
static void on_client_connect(uv_connect_t* req, int status);
36
static void on_client_alloc(uv_handle_t* handle,
37
                            size_t suggested_size,
38
                            uv_buf_t* buf);
39
static void on_client_read(uv_stream_t* stream,
40
                           ssize_t nread,
41
                           const uv_buf_t* buf);
42
static void on_client_timeout(uv_timer_t* handle);
43

44
static void on_close(uv_handle_t* handle);
45

46

47
static void on_client_connect(uv_connect_t* conn_req, int status) {
48
  int r;
49

50
  r = uv_read_start((uv_stream_t*) &client, on_client_alloc, on_client_read);
51
  ASSERT_OK(r);
52

53
  r = uv_timer_start(&timer, on_client_timeout, 1000, 0);
54
  ASSERT_OK(r);
55
}
56

57

58
static void on_client_alloc(uv_handle_t* handle,
59
                            size_t suggested_size,
60
                            uv_buf_t* buf) {
61
  static char slab[8];
62
  buf->base = slab;
63
  buf->len = sizeof(slab);
64
}
65

66

67
static void on_client_read(uv_stream_t* stream, ssize_t nread,
68
                           const uv_buf_t* buf) {
69
  ASSERT_LT(nread, 0);
70
  read_cb_called++;
71
}
72

73

74
static void on_client_timeout(uv_timer_t* handle) {
75
  ASSERT_PTR_EQ(handle, &timer);
76
  ASSERT_OK(read_cb_called);
77
  uv_read_stop((uv_stream_t*) &client);
78
  uv_close((uv_handle_t*) &client, on_close);
79
  uv_close((uv_handle_t*) &timer, on_close);
80
}
81

82

83
static void on_connection_alloc(uv_handle_t* handle,
84
                     size_t suggested_size,
85
                     uv_buf_t* buf) {
86
  static char slab[8];
87
  buf->base = slab;
88
  buf->len = sizeof(slab);
89
}
90

91

92
static void on_connection_read(uv_stream_t* stream,
93
                               ssize_t nread,
94
                               const uv_buf_t* buf) {
95
  ASSERT_EQ(nread, UV_EOF);
96
  read_cb_called++;
97
  uv_close((uv_handle_t*) stream, on_close);
98
}
99

100

101
static void on_connection(uv_stream_t* server, int status) {
102
  int r;
103

104
  ASSERT_OK(status);
105
  ASSERT_OK(uv_accept(server, (uv_stream_t*) &connection));
106

107
  r = uv_read_start((uv_stream_t*) &connection,
108
                    on_connection_alloc,
109
                    on_connection_read);
110
  ASSERT_OK(r);
111
}
112

113

114
static void on_close(uv_handle_t* handle) {
115
  ASSERT_NE(handle == (uv_handle_t*) &client ||
116
            handle == (uv_handle_t*) &connection ||
117
            handle == (uv_handle_t*) &timer, 0);
118
  on_close_called++;
119
}
120

121

122
static void start_server(uv_loop_t* loop, uv_tcp_t* handle) {
123
  struct sockaddr_in addr;
124
  int r;
125

126
  ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
127

128
  r = uv_tcp_init(loop, handle);
129
  ASSERT_OK(r);
130

131
  r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0);
132
  ASSERT_OK(r);
133

134
  r = uv_listen((uv_stream_t*) handle, 128, on_connection);
135
  ASSERT_OK(r);
136

137
  uv_unref((uv_handle_t*) handle);
138
}
139

140

141
/* Check that pending write requests have their callbacks
142
 * invoked when the handle is closed.
143
 */
144
TEST_IMPL(tcp_close_after_read_timeout) {
145
  struct sockaddr_in addr;
146
  uv_tcp_t tcp_server;
147
  uv_loop_t* loop;
148
  int r;
149

150
  ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
151

152
  loop = uv_default_loop();
153

154
  /* We can't use the echo server, it doesn't handle ECONNRESET. */
155
  start_server(loop, &tcp_server);
156

157
  r = uv_tcp_init(loop, &client);
158
  ASSERT_OK(r);
159

160
  r = uv_tcp_connect(&connect_req,
161
                     &client,
162
                     (const struct sockaddr*) &addr,
163
                     on_client_connect);
164
  ASSERT_OK(r);
165

166
  r = uv_tcp_init(loop, &connection);
167
  ASSERT_OK(r);
168

169
  r = uv_timer_init(loop, &timer);
170
  ASSERT_OK(r);
171

172
  ASSERT_OK(read_cb_called);
173
  ASSERT_OK(on_close_called);
174

175
  r = uv_run(loop, UV_RUN_DEFAULT);
176
  ASSERT_OK(r);
177

178
  ASSERT_EQ(1, read_cb_called);
179
  ASSERT_EQ(3, on_close_called);
180

181
  MAKE_VALGRIND_HAPPY(loop);
182
  return 0;
183
}
184

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.