summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-10-28 16:51:13 +0100
committerKonstantin Käfer <mail@kkaefer.com>2015-10-28 16:51:13 +0100
commitb9452bcbba627ec9c032b7cc6040594f74c245a0 (patch)
treea126cf3c1d7c4715134a5ba9a9a150ef3d801170 /test
parentc80c823da25bcc67d89158f339be7cfb93c45f50 (diff)
downloadqtlocation-mapboxgl-b9452bcbba627ec9c032b7cc6040594f74c245a0.tar.gz
[core] refactor test server startup
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/util.cpp38
-rwxr-xr-xtest/storage/server.js4
2 files changed, 26 insertions, 16 deletions
diff --git a/test/fixtures/util.cpp b/test/fixtures/util.cpp
index 7527f64f91..aad349d668 100644
--- a/test/fixtures/util.cpp
+++ b/test/fixtures/util.cpp
@@ -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)) {
@@ -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.
@@ -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;
}
diff --git a/test/storage/server.js b/test/storage/server.js
index efc1e1d6c0..249c6a124b 100755
--- a/test/storage/server.js
+++ b/test/storage/server.js
@@ -2,6 +2,7 @@
/* jshint node: true */
'use strict';
+var fs = require('fs');
var express = require('express');
var app = express();
@@ -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]);
}
});