Skip to content

Commit

Permalink
#16: A more solid take at starting a browsermob proxy on an unused port.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelkiessling committed Jan 16, 2017
1 parent 9b94701 commit 0232440
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
26 changes: 18 additions & 8 deletions bin/run-testcase.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
#!/bin/bash

# Starting the proxy as early as possible to minimize the risk of having the port taken by another proxy process
/usr/bin/curl -s -X POST -d "port=$2" http://localhost:9090/proxy
/usr/bin/curl -s -X PUT -d "captureHeaders=1" http://localhost:9090/proxy/$2/har

/usr/bin/Xvfb :$1 -nolisten tcp -ac > /dev/null 2> /dev/null &
XVFB_PID=$!

export DISPLAY=:$1
mkdir /var/tmp/journeymonitor-firefox-profile-$XVFB_PID

# Launching a browsermob proxy, ensuring that we don't work with a port that is already taken
PROXY_STARTED=0
while [ $PROXY_STARTED -eq 0 ]
do
PROXY_PORT=$(( ( RANDOM % 32767 ) + 9091 ))
PROXY_START_OUTPUT=`/usr/bin/curl -s -X POST -d "port=$PROXY_PORT" http://localhost:9090/proxy`
if [ "$PROXY_START_OUTPUT" == "{\"port\":$PROXY_PORT}" ]
then
PROXY_STARTED=1
fi
done

/usr/bin/curl -s -X PUT -d "captureHeaders=1" http://localhost:9090/proxy/$PROXY_PORT/har

# Firefoxes should not be started in parallel it seems
sleep $[ ( $RANDOM % 10 ) + 1 ]s

/usr/bin/java \
-jar /opt/selenese-runner-java/selenese-runner.jar \
--driver firefox \
--proxy localhost:$2 \
--proxy localhost:$PROXY_PORT \
--cli-args "--new-instance" \
--cli-args "--profile /var/tmp/journeymonitor-firefox-profile-$XVFB_PID" \
--width 1920 \
--height 1200 \
--screenshot-on-fail /var/tmp/journeymonitor-screenshots \
--strict-exit-code \
--timeout 120000 \
$3
$2
STATUS=$?

echo $STATUS > /var/tmp/journeymonitor-testcase-run-$1-exit-status

/usr/bin/curl -s http://localhost:9090/proxy/$2/har > /var/tmp/journeymonitor-testcase-run-$1-har
/usr/bin/curl -s -X DELETE http://localhost:9090/proxy/$2
/usr/bin/curl -s http://localhost:9090/proxy/$PROXY_PORT/har > /var/tmp/journeymonitor-testcase-run-$1-har
/usr/bin/curl -s -X DELETE http://localhost:9090/proxy/$PROXY_PORT

rm -rf /var/tmp/journeymonitor-firefox-profile-$XVFB_PID
kill $XVFB_PID
17 changes: 0 additions & 17 deletions src/JourneyMonitor/Monitor/JobRunner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ public function run($retry = 0)
}
}

$found = false;
while (!$found) {
$proxyPort = mt_rand(9091, 60000);
// https://github.com/journeymonitor/monitor/issues/16:
// "browsermob proxy instance not starting every now and then because port is already taken"
// Only "reserving" a port via the lock file is not enough because browsermob also grabs a lot of
// other ports while requests run over its proxy childs
if ( !file_exists('/var/tmp/journeymonitor-testcase-run-proxyport-' . $proxyPort . '.lock')
&& shell_exec('/usr/bin/lsof -P -i:' . $proxyPort . ' -n -sTCP:LISTEN 2>&1 | /usr/bin/wc -l') === "0\n") {
$found = true;
touch('/var/tmp/journeymonitor-testcase-run-proxyport-' . $proxyPort . '.lock');
}
}

$output = [];
$exitCode = 0;
$datetimeRun = new \DateTime('now');
Expand All @@ -59,8 +45,6 @@ public function run($retry = 0)
__DIR__ . DIRECTORY_SEPARATOR . '../../../../bin/run-testcase.sh ' .
$jobId .
' ' .
$proxyPort .
' ' .
$this->directory . DIRECTORY_SEPARATOR . 'journeymonitor-testcase-' . $this->testcaseModel->getId() . '.html ' .
'2>&1 ' .
'| tee -a /var/tmp/journeymonitor-run-testcase-' . $this->testcaseModel->getId() . '.log ; ' .
Expand All @@ -79,7 +63,6 @@ public function run($retry = 0)

unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '-har');
unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '.lock');
unlink('/var/tmp/journeymonitor-testcase-run-proxyport-' . $proxyPort . '.lock');
unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '-exit-status');

if ($exitCode === 1 && $retry < 5) { // Internal selenium-runner error, retry
Expand Down

0 comments on commit 0232440

Please sign in to comment.