Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] refactor test server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Oct 28, 2015
1 parent c80c823 commit b9452bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
38 changes: 23 additions & 15 deletions test/fixtures/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace mbgl {
namespace test {

pid_t startServer(const char *executable) {
const std::string parent_pid = std::to_string(getpid());
int pipefd[2];

if (pipe(pipefd)) {
Expand All @@ -24,19 +23,17 @@ pid_t startServer(const char *executable) {

pid_t pid = fork();
if (pid < 0) {
throw std::runtime_error("Cannot create server process");
Log::Error(Event::Setup, "Cannot create server process");
exit(1);
} else if (pid == 0) {
close(STDIN_FILENO);

if (dup(pipefd[1])) {
Log::Error(Event::Setup, "Failed to start server: %s", strerror(errno));
}
// This is the child process.

// Close the input side of the pipe.
close(pipefd[0]);
close(pipefd[1]);

// Launch the actual server process, with the pipe ID as the first argument.
char *args[] = { const_cast<char *>(executable),
const_cast<char *>(parent_pid.c_str()),
const_cast<char *>(std::to_string(pipefd[1]).c_str()),
nullptr };
int ret = execv(executable, args);
// This call should not return. In case execve failed, we exit anyway.
Expand All @@ -45,15 +42,26 @@ pid_t startServer(const char *executable) {
}
exit(0);
} else {
char buffer[8];
// This is the parent process.

// Wait until the server process sends something on the pipe.
if (!read(pipefd[0], buffer, sizeof(buffer))) {
throw std::runtime_error("Error reading a message from the server");
}
// Close output side of the pipe.
close(pipefd[1]);

// Wait until the server process closes the handle.
char buffer[2];
ssize_t bytes = 0, total = 0;
while ((bytes = read(pipefd[0], buffer, sizeof(buffer))) != 0) {
total += bytes;
}
if (bytes < 0) {
Log::Error(Event::Setup, "Failed to start server: %s", strerror(errno));
exit(1);
}
if (total != 2 || strncmp(buffer, "OK", 2) != 0) {
Log::Error(Event::Setup, "Failed to start server");
exit(1);
}
close(pipefd[0]);
close(pipefd[1]);
}
return pid;
}
Expand Down
4 changes: 3 additions & 1 deletion test/storage/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* jshint node: true */
'use strict';

var fs = require('fs');
var express = require('express');
var app = express();

Expand Down Expand Up @@ -106,6 +107,7 @@ var server = app.listen(3000, function () {

if (process.argv[2]) {
// Allow the test to continue running.
process.stdin.write("Go!\n");
fs.write(+process.argv[2], 'OK');
fs.close(+process.argv[2]);
}
});

0 comments on commit b9452bc

Please sign in to comment.