Fix potential server test failure

Due to scheduling, the client could attempt to connect to the server
before the server starts to accept. Make the client retry multiple
times before returning with error.
This commit is contained in:
Hui, Chunyang 2023-03-17 11:33:11 +08:00 committed by volcano
parent 478d0d381f
commit 492814132a

@ -38,12 +38,29 @@ int connect_with_server(const char *addr_string, const char *port_string) {
THROW_ERROR("inet_pton error"); THROW_ERROR("inet_pton error");
} }
int retry_num = 5;
while (retry_num > 0) {
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret < 0) { if (ret == 0) {
break;
}
if (ret < 0 && errno == ECONNREFUSED) {
// The server maynot be ready to accept. Try again later.
sleep(1);
retry_num--;
continue;
} else {
close(sockfd); close(sockfd);
THROW_ERROR("connect error"); THROW_ERROR("connect error");
} }
}
if (retry_num == 0) {
close(sockfd);
THROW_ERROR("Retry connect multiple times. All failure");
}
printf("connected with server");
return sockfd; return sockfd;
} }