Skip to content

Commit

Permalink
kill the previous process which occupying the specifice port (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonYao287 authored Jan 12, 2024
1 parent 7801ac6 commit 85e2e1c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/tests/test_common/hs_repl_test_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class HSReplTestHelper {
if (count == SISL_OPTIONS["replicas"].as< uint32_t >()) {
phase_ = new_phase;
cv_.notify_all();
}
cv_.wait(lg, [this, new_phase]() { return (phase_ == new_phase); });
} else
cv_.wait(lg, [this, new_phase]() { return (phase_ == new_phase); });
}
};

Expand Down Expand Up @@ -136,6 +136,11 @@ class HSReplTestHelper {
if (replica_num_ == 0) {
// Erase previous shmem and create a new shmem with IPCData structure
bip::shared_memory_object::remove("raft_repl_test_shmem");

// kill the previous processes using the port
for (uint32_t i = 0; i < num_replicas; ++i)
check_and_kill(SISL_OPTIONS["base_port"].as< uint16_t >() + i);

shm_ = std::make_unique< bip::shared_memory_object >(bip::create_only, "raft_repl_test_shmem",
bip::read_write);
shm_->truncate(sizeof(IPCData));
Expand Down Expand Up @@ -223,6 +228,22 @@ class HSReplTestHelper {
void sync_dataset_size(uint64_t dataset_size) { ipc_data_->test_dataset_size_ = dataset_size; }
uint64_t dataset_size() const { return ipc_data_->test_dataset_size_; }

void check_and_kill(int port) {
std::string command = "lsof -t -i:" + std::to_string(port);
if (system(command.c_str())) {
std::cout << "Port " << port << " is not in use." << std::endl;
} else {
std::cout << "Port " << port << " is in use. Trying to kill the process..." << std::endl;
command += " | xargs kill -9";
int result = system(command.c_str());
if (result == 0) {
std::cout << "Killed the process using port " << port << std::endl;
} else {
std::cout << "Failed to kill the process." << std::endl;
}
}
}

private:
uint16_t replica_num_;
std::string name_;
Expand Down

0 comments on commit 85e2e1c

Please sign in to comment.