-
Notifications
You must be signed in to change notification settings - Fork 7
/
Server.php
126 lines (96 loc) · 3.59 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php namespace Dredd;
use UnexpectedValueException;
class Server
{
const RECV_LENGTH = 10;
const MESSAGE_END = "\n";
private $host;
private $port;
/** @var Runner */
private $runner;
public function __construct($host, $port)
{
$this->runner = new Runner;
$this->host = $host;
$this->port = $port;
}
public function run($force = false)
{
if ($force) {
$this->killProgramsOnDreddPort();
}
$socket = sprintf('tcp://%s:%s', $this->host, $this->port);
$server = stream_socket_server($socket, $errno, $errorMessage);
if ($server === false) {
throw new UnexpectedValueException("Server could not bind to socket: $errorMessage");
}
$buffer = "";
while ($connection = stream_socket_accept($server)) {
while ($socketData = stream_socket_recvfrom($connection, self::RECV_LENGTH)) {
$buffer .= $socketData;
// determine if message terminating character is present.
if (strpos($buffer, self::MESSAGE_END) === false) {
continue;
}
$messages = [];
foreach (explode(self::MESSAGE_END, $buffer) as $data) {
$message = json_decode($data);
// if not valid json the partial message needs saved
if (! $message) {
$buffer = $message;
continue;
}
$messages[] = $message;
}
foreach ($messages as $message) {
$this->processMessage($message, $connection);
}
}
}
}
public function processMessage($message, $client)
{
$event = $message->event;
$data = $message->data;
$uuid = $message->uuid;
if ($event == "beforeEach") {
$data = $this->runner->runBeforeEachHooksForTransaction($data);
$data = $this->runner->runBeforeHooksForTransaction($data);
}
if ($event == "beforeEachValidation") {
$data = $this->runner->runBeforeEachValidationHooksForTransaction($data);
$data = $this->runner->runBeforeValidationHooksForTransaction($data);
}
if ($event == "afterEach") {
$data = $this->runner->runAfterHooksForTransaction($data);
$data = $this->runner->runAfterEachHooksForTransaction($data);
}
if ($event == "beforeAll") {
$data = $this->runner->runBeforeAllHooksForTransaction($data);
}
if ($event == "afterAll") {
$data = $this->runner->runAfterAllHooksForTransaction($data);
}
$messageToSend = [
'uuid' => $uuid,
'data' => $data,
'event' => $event
];
$messageToSend = json_encode($messageToSend);
stream_socket_sendto($client, $messageToSend . self::MESSAGE_END);
}
private function killProgramsOnDreddPort()
{
foreach ([SIGTERM, SIGINT, SIGHUP, SIGKILL] as $signal) {
// get any programs running on the dredd port
if ($string = shell_exec(sprintf("lsof -i tcp:%s", $this->port))) {
$regex = '/(?:php\s*)(\d*)/';
// get matches if any programs are returned
preg_match($regex, $string, $matches);
// execute kill command so server can listen on port
shell_exec("kill -$signal {$matches[1]}");
sleep(3);
}
}
}
}