Skip to content

Commit

Permalink
Merge pull request #456 from andig/push-fix
Browse files Browse the repository at this point in the history
Fix push server not starting
  • Loading branch information
andig authored Aug 25, 2016
2 parents fb68636 + a5fa66d commit 197ee11
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
28 changes: 20 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
global:
- DEPENDENCIES=standard
- APC=true
- TRAVIS_TEST_EXCLUDES="--exclude-group slow,jpgraph"
- TRAVIS_TEST_EXCLUDES="--exclude-group slow,jpgraph,pushserver"
matrix:
- DB=mysql
- DB=pgsql
Expand All @@ -30,7 +30,10 @@ matrix:
include:
# httpd-based
- php: 5.4
env: DB=mysql TEST_ADAPTER=HTTP
env: DB=mysql TEST_COMPONENT=HTTPD
# push-server
- php: 5.4
env: DB=mysql TEST_COMPONENT=PUSH_SERVER
# from..to
- php: 5.4
env: DB=mysql DEPENDENCIES=lowest
Expand Down Expand Up @@ -76,7 +79,7 @@ install:
- if [ "$DEPENDENCIES" = "highest" ]; then composer update -n; fi
- if [ "$JSLINT" = true ]; then npm install; fi
- if [ "$SECURITY" = true ]; then wget http://get.sensiolabs.org/security-checker.phar; fi
- if [ "$TEST_ADAPTER" = "HTTP" ]; then composer require volkszaehler/httpd:dev-master jenssegers/proxy:~2.2; fi
- if [ "$TEST_COMPONENT" = "HTTPD" ]; then composer require volkszaehler/httpd:dev-master jenssegers/proxy:~2.2; fi

# add apc cache
- |
Expand All @@ -100,7 +103,7 @@ before_script:
- sed -i "s/'vz'/'$USER'/" etc/volkszaehler.conf.php
- sed -i "s/'demo'/'$PASSWORD'/" etc/volkszaehler.conf.php
- sed -i "s/'volkszaehler'/'$DATABASE'/" etc/volkszaehler.conf.php
- if [ "$DB" = "sqlite" ]; then sed -i "s/\?>/\$config['db']['path']\ =\ VZ_DIR.'\/sqlite.db3'\n?>/" etc/volkszaehler.conf.php; fi
- if [ "$DB" = "sqlite" ]; then sed -i "s/\?>/\$config['db']['path']\ =\ VZ_DIR.'\/sqlite.db3'\;\n?>/" etc/volkszaehler.conf.php; fi
- cat etc/volkszaehler.conf.php

# create database
Expand All @@ -112,15 +115,21 @@ before_script:

# setup local middleware
- |
if [ "$TEST_ADAPTER" = "HTTP" ]; then
sed -i "s/testAdapter\" value=\".*\"/testAdapter\" value=\"$TEST_ADAPTER\"/" phpunit.xml
if [ "$TEST_COMPONENT" = "HTTPD" ]; then
sed -i "s/testAdapter\" value=\".*\"/testAdapter\" value=\"$TEST_COMPONENT\"/" phpunit.xml
vendor/bin/httpd.php start &
HTTPD_PID=$!
echo "Started httpd with pid $HTTPD_PID"
fi
# push server tests
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then sed -i "s/\?>/\$config['push']['enabled']\ =\ true\;\n?>/" etc/volkszaehler.conf.php; fi
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then
php misc/tools/push-server.php &
fi

after_script:
- if [ "$TEST_ADAPTER" = "HTTP" ]; then kill -9 $HTTPD_PID; fi
- if [ "$TEST_COMPONENT" = "HTTPD" ]; then kill -9 $HTTPD_PID; fi

script:
# run core tests
Expand All @@ -129,7 +138,7 @@ script:
# run aggregation tests (mysql only)
- if [ "$DB" = "mysql" ]; then sed -i "s/\?>/\$config['aggregation']\ =\ true;\n?>/" etc/volkszaehler.conf.php; fi
- |
if [ "$DB" = "mysql" -a "$TEST_ADAPTER" = "HTTP" ]; then
if [ "$DB" = "mysql" -a "$TEST_COMPONENT" = "HTTPD" ]; then
kill -9 $HTTPD_PID
sleep 10
vendor/bin/httpd.php start &
Expand All @@ -141,6 +150,9 @@ script:
# run aggregation tool itself (mysql only)
- if [ "$DB" = "mysql" ]; then php misc/tools/aggregate.php run -m delta -l hour; fi

# push server tests
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then phpunit --group pushserver; fi

# jslint javascript sources
- if [ "$JSLINT" = true ]; then gulp jshint; fi

Expand Down
23 changes: 12 additions & 11 deletions lib/Server/MiddlewareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ class MiddlewareAdapter {
*/
protected $em;

/**
* @var EntityController
*/
protected $controller;

/**
* @var array
*/
Expand All @@ -68,17 +63,16 @@ public function removeAdapter(PushTransportInterface $adapter) {
}

protected function openController($force = false) {
if ($force || $this->em == null || $this->controller == null || !$this->em->isOpen()) {
if ($force || $this->em == null || !$this->em->isOpen()) {
$this->em = Router::createEntityManager(false);
$this->controller = new EntityController(new Request(), $this->em);
}
}

protected function connectToMiddleware($uuid) {
try {
$this->openController();

$entity = $this->controller->getSingleEntity($uuid);
$entity = EntityController::factory($this->em, $uuid);
$class = $entity->getDefinition()->getInterpreter();
$interpreter = new $class($entity, $this->em, null, null);

Expand Down Expand Up @@ -132,24 +126,27 @@ protected function convertRawTuple(Interpreter\Interpreter $interpreter, $tuple)
* @return null|string Returns null on invalid request
*/
public function handlePushMessage($json) {
$msg = json_decode($json, true);
$response = array(
'version' => VZ_VERSION,
'data' => array()
);

// validate input message
if (null === ($msg = json_decode($json, true))) {
return null;
}
if (null === ($data = @$msg['data']) || !is_array($data)) {
return null;
}

// loop through channels
foreach ($data as $channel) {
// validate channel structure
if (null === ($uuid = @$channel['uuid'])) {
break;
return null;
}
if (null === ($tuples = @$channel['tuples']) || !is_array($tuples) || !count($tuples)) {
break;
return null;
}

// get interpreter if no client has connected yet
Expand All @@ -161,6 +158,10 @@ public function handlePushMessage($json) {
// convert raw tuples using interpreter rules
$transformed = array();
foreach ($tuples as $tuple) {
if (!is_array($tuple) || count($tuple) < 2) {
return null;
}

if (count($tuple) < 3) {
$tuple[] = 1;
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<exclude>
<group>slow</group>
<group>jpgraph</group>
<group>pushserver</group>
</exclude>
</groups>

Expand Down
50 changes: 50 additions & 0 deletions test/PushServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Meter tests
*
* @package Test
* @author Andreas Götz <[email protected]>
*/

namespace Tests;

use Volkszaehler\Util;

class PushServerTest extends Data
{
// channel properties
static $resolution = 100;

/**
* Create channel
*/
static function setupBeforeClass() {
parent::setupBeforeClass();
self::$uuid = self::createChannel('Counter', 'electric meter', self::$resolution);
}

/**
* @group pushserver
*/
function testPushMessage() {
$this->assertTrue(Util\Configuration::read('push.enabled'), 'Push server disabled');

$exitCode = null;
$port = Util\Configuration::read('push.server');
$curl = "curl %s -s -m 3 -X POST -d '{\"data\":[{\"uuid\":\"%s\",\"tuples\":[[1,1,1]]}]}' localhost:%d 2>&1";

// run and test for failure
$cmd = sprintf($curl, '-f', self::$uuid, $port);
passthru($cmd, $exitCode);

// run to get output
if ($exitCode !== 0) {
$cmd = sprintf($curl, '-i', self::$uuid, $port);
passthru($cmd);
}

$this->assertTrue($exitCode === 0, sprintf('Curl failed with exit code %d', $exitCode));
}
}

?>

0 comments on commit 197ee11

Please sign in to comment.