@@ -68,6 +68,76 @@ TEST(coroutine_socket, connect_timeout) {
68
68
});
69
69
}
70
70
71
+ TEST (coroutine_socket, timeout_controller) {
72
+ coroutine::run ([](void *arg) {
73
+ const int port = __LINE__ + TEST_PORT;
74
+ Coroutine::create ([](void *arg) {
75
+ Socket sock (SW_SOCK_TCP);
76
+ bool retval = sock.bind (" 127.0.0.1" , port);
77
+ ASSERT_EQ (retval, true );
78
+ ASSERT_EQ (sock.listen (128 ), true );
79
+
80
+ Socket *conn = sock.accept ();
81
+ conn->send (TEST_STR);
82
+ System::sleep (1 );
83
+ delete conn;
84
+ });
85
+
86
+ Socket sock (SW_SOCK_TCP);
87
+ Socket::TimeoutController tc (&sock, 0.5 , Socket::TIMEOUT_ALL);
88
+ ASSERT_TRUE (sock.connect (" 127.0.0.1" , port));
89
+
90
+ char buf[128 ];
91
+ off_t offset = 0 ;
92
+ sock.errCode = 0 ;
93
+ while (true ) {
94
+ if (sw_unlikely (tc.has_timedout (Socket::TIMEOUT_READ))) {
95
+ break ;
96
+ }
97
+ auto rv = sock.recv (buf + offset, sizeof (buf) - offset);
98
+ if (rv <= 0 ) {
99
+ break ;
100
+ }
101
+ offset += rv;
102
+ }
103
+ ASSERT_TRUE (tc.has_timedout (Socket::TIMEOUT_READ));
104
+ ASSERT_EQ (sock.errCode , ETIMEDOUT);
105
+ });
106
+ }
107
+
108
+ TEST (coroutine_socket, timeout_setter) {
109
+ coroutine::run ([](void *arg) {
110
+ const int port = __LINE__ + TEST_PORT;
111
+ Coroutine::create ([](void *arg) {
112
+ Socket sock (SW_SOCK_TCP);
113
+ bool retval = sock.bind (" 127.0.0.1" , port);
114
+ ASSERT_EQ (retval, true );
115
+ ASSERT_EQ (sock.listen (128 ), true );
116
+
117
+ Socket *conn = sock.accept ();
118
+ conn->send (TEST_STR);
119
+ System::sleep (1 );
120
+ delete conn;
121
+ });
122
+
123
+ Socket sock (SW_SOCK_TCP);
124
+ Socket::TimeoutSetter ts (&sock, 0.5 , Socket::TIMEOUT_ALL);
125
+ ASSERT_TRUE (sock.connect (" 127.0.0.1" , port));
126
+
127
+ char buf[128 ];
128
+ off_t offset = 0 ;
129
+ sock.errCode = 0 ;
130
+ while (true ) {
131
+ auto rv = sock.recv (buf + offset, sizeof (buf) - offset);
132
+ if (rv <= 0 ) {
133
+ break ;
134
+ }
135
+ offset += rv;
136
+ }
137
+ ASSERT_EQ (sock.errCode , ETIMEDOUT);
138
+ });
139
+ }
140
+
71
141
TEST (coroutine_socket, connect_with_dns) {
72
142
coroutine::run ([](void *arg) {
73
143
Socket sock (SW_SOCK_TCP);
0 commit comments