Skip to content

Commit 9a6a056

Browse files
pschafhalterrobertnishihara
authored andcommitted
Convert UT datastructures in tests (#1203)
* bind_ipc_sock_retry returns std::string * snprintf -> std::snprintf * Fix formatting * Use stringstream instead of snprintf * Fix typo
1 parent e798a65 commit 9a6a056

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/common/test/test_common.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
#define TEST_COMMON_H
33

44
#include <unistd.h>
5+
6+
#include <cstdio>
7+
#include <string>
58
#include <vector>
69

710
#include "common.h"
811
#include "io.h"
912
#include "hiredis/hiredis.h"
10-
#include "utstring.h"
1113
#include "state/redis.h"
1214

1315
#ifndef _WIN32
@@ -16,14 +18,17 @@ extern int usleep(useconds_t usec);
1618
#endif
1719

1820
/* I/O helper methods to retry binding to sockets. */
19-
static inline UT_string *bind_ipc_sock_retry(const char *socket_name_format,
20-
int *fd) {
21-
UT_string *socket_name = NULL;
21+
static inline std::string bind_ipc_sock_retry(const char *socket_name_format,
22+
int *fd) {
23+
std::string socket_name;
2224
for (int num_retries = 0; num_retries < 5; ++num_retries) {
2325
LOG_INFO("trying to find plasma socket (attempt %d)", num_retries);
24-
utstring_renew(socket_name);
25-
utstring_printf(socket_name, socket_name_format, rand());
26-
*fd = bind_ipc_sock(utstring_body(socket_name), true);
26+
size_t size = std::snprintf(nullptr, 0, socket_name_format, rand()) + 1;
27+
char socket_name_c_str[size];
28+
std::snprintf(socket_name_c_str, size, socket_name_format, rand());
29+
socket_name = std::string(socket_name_c_str);
30+
31+
*fd = bind_ipc_sock(socket_name.c_str(), true);
2732
if (*fd < 0) {
2833
/* Sleep for 100ms. */
2934
usleep(100000);

src/local_scheduler/test/local_scheduler_tests.cc

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
#include <sys/socket.h>
88
#include <sys/wait.h>
99

10+
#include <cstdio>
11+
#include <sstream>
12+
#include <string>
1013
#include <thread>
1114

1215
#include "common.h"
1316
#include "test/test_common.h"
1417
#include "test/example_task.h"
1518
#include "event_loop.h"
1619
#include "io.h"
17-
#include "utstring.h"
1820
#include "task.h"
1921
#include "state/object_table.h"
2022
#include "state/task_table.h"
@@ -83,31 +85,30 @@ LocalSchedulerMock *LocalSchedulerMock_init(int num_workers,
8385
memset(mock, 0, sizeof(LocalSchedulerMock));
8486
mock->loop = event_loop_create();
8587
/* Bind to the local scheduler port and initialize the local scheduler. */
86-
UT_string *plasma_manager_socket_name = bind_ipc_sock_retry(
88+
std::string plasma_manager_socket_name = bind_ipc_sock_retry(
8789
plasma_manager_socket_name_format, &mock->plasma_manager_fd);
8890
mock->plasma_store_fd =
8991
connect_ipc_sock_retry(plasma_store_socket_name, 5, 100);
90-
UT_string *local_scheduler_socket_name = bind_ipc_sock_retry(
92+
std::string local_scheduler_socket_name = bind_ipc_sock_retry(
9193
local_scheduler_socket_name_format, &mock->local_scheduler_fd);
9294
CHECK(mock->plasma_store_fd >= 0 && mock->local_scheduler_fd >= 0);
9395

94-
UT_string *worker_command;
95-
utstring_new(worker_command);
96-
utstring_printf(worker_command,
97-
"python ../../../python/ray/workers/default_worker.py "
98-
"--node-ip-address=%s --object-store-name=%s "
99-
"--object-store-manager-name=%s --local-scheduler-name=%s "
100-
"--redis-address=%s:%d",
101-
node_ip_address, plasma_store_socket_name,
102-
utstring_body(plasma_manager_socket_name),
103-
utstring_body(local_scheduler_socket_name), redis_addr,
104-
redis_port);
96+
/* Construct worker command */
97+
std::stringstream worker_command_ss;
98+
worker_command_ss << "python ../../../python/ray/workers/default_worker.py"
99+
<< " --node-ip-address=" << node_ip_address
100+
<< " --object-store-name=" << plasma_store_socket_name
101+
<< " --object-store-manager-name="
102+
<< plasma_manager_socket_name
103+
<< " --local-scheduler-name=" << local_scheduler_socket_name
104+
<< " --redis-address=" << redis_addr << ":" << redis_port;
105+
std::string worker_command = worker_command_ss.str();
105106

106107
mock->local_scheduler_state = LocalSchedulerState_init(
107108
"127.0.0.1", mock->loop, redis_addr, redis_port,
108-
utstring_body(local_scheduler_socket_name), plasma_store_socket_name,
109-
utstring_body(plasma_manager_socket_name), NULL, false,
110-
static_resource_conf, utstring_body(worker_command), num_workers);
109+
local_scheduler_socket_name.c_str(), plasma_store_socket_name,
110+
plasma_manager_socket_name.c_str(), NULL, false, static_resource_conf,
111+
worker_command.c_str(), num_workers);
111112

112113
/* Accept the workers as clients to the plasma manager. */
113114
for (int i = 0; i < num_workers; ++i) {
@@ -123,16 +124,13 @@ LocalSchedulerMock *LocalSchedulerMock_init(int num_workers,
123124
std::thread(register_clients, num_mock_workers, mock);
124125

125126
for (int i = 0; i < num_mock_workers; ++i) {
126-
mock->conns[i] = LocalSchedulerConnection_init(
127-
utstring_body(local_scheduler_socket_name), NIL_WORKER_ID, NIL_ACTOR_ID,
128-
true, 0);
127+
mock->conns[i] =
128+
LocalSchedulerConnection_init(local_scheduler_socket_name.c_str(),
129+
NIL_WORKER_ID, NIL_ACTOR_ID, true, 0);
129130
}
130131

131132
background_thread.join();
132133

133-
utstring_free(worker_command);
134-
utstring_free(plasma_manager_socket_name);
135-
utstring_free(local_scheduler_socket_name);
136134
return mock;
137135
}
138136

src/plasma/test/manager_tests.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <sys/socket.h>
88
#include <fcntl.h>
99

10+
#include <string>
11+
1012
#include "common.h"
1113
#include "test/test_common.h"
1214
#include "event_loop.h"
1315
#include "io.h"
14-
#include "utstring.h"
1516

1617
#include "plasma/plasma.h"
1718
#include "plasma/client.h"
@@ -62,13 +63,13 @@ plasma_mock *init_plasma_mock(plasma_mock *remote_mock) {
6263
/* Start listening on all the ports and initiate the local plasma manager. */
6364
mock->port = bind_inet_sock_retry(&mock->manager_remote_fd);
6465
mock->local_store = connect_ipc_sock_retry(plasma_store_socket_name, 5, 100);
65-
UT_string *manager_socket_name = bind_ipc_sock_retry(
66+
std::string manager_socket_name = bind_ipc_sock_retry(
6667
plasma_manager_socket_name_format, &mock->manager_local_fd);
6768

6869
CHECK(mock->manager_local_fd >= 0 && mock->local_store >= 0);
6970

7071
mock->state = PlasmaManagerState_init(plasma_store_socket_name,
71-
utstring_body(manager_socket_name),
72+
manager_socket_name.c_str(),
7273
manager_addr, mock->port, NULL, 0);
7374
mock->loop = get_event_loop(mock->state);
7475
/* Accept a connection from the local manager on the remote manager. */
@@ -86,12 +87,11 @@ plasma_mock *init_plasma_mock(plasma_mock *remote_mock) {
8687
/* Connect a new client to the local plasma manager and mock a request to an
8788
* object. */
8889
mock->plasma_client = new plasma::PlasmaClient();
89-
ARROW_CHECK_OK(mock->plasma_client->Connect(
90-
plasma_store_socket_name, utstring_body(manager_socket_name), 0));
90+
ARROW_CHECK_OK(mock->plasma_client->Connect(plasma_store_socket_name,
91+
manager_socket_name.c_str(), 0));
9192
wait_for_pollin(mock->manager_local_fd);
9293
mock->client_conn = ClientConnection_listen(
9394
mock->loop, mock->manager_local_fd, mock->state, 0);
94-
utstring_free(manager_socket_name);
9595
return mock;
9696
}
9797

0 commit comments

Comments
 (0)