Skip to content

Commit 197ee11

Browse files
authored
Merge pull request #456 from andig/push-fix
Fix push server not starting
2 parents fb68636 + a5fa66d commit 197ee11

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed

.travis.yml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020
global:
2121
- DEPENDENCIES=standard
2222
- APC=true
23-
- TRAVIS_TEST_EXCLUDES="--exclude-group slow,jpgraph"
23+
- TRAVIS_TEST_EXCLUDES="--exclude-group slow,jpgraph,pushserver"
2424
matrix:
2525
- DB=mysql
2626
- DB=pgsql
@@ -30,7 +30,10 @@ matrix:
3030
include:
3131
# httpd-based
3232
- php: 5.4
33-
env: DB=mysql TEST_ADAPTER=HTTP
33+
env: DB=mysql TEST_COMPONENT=HTTPD
34+
# push-server
35+
- php: 5.4
36+
env: DB=mysql TEST_COMPONENT=PUSH_SERVER
3437
# from..to
3538
- php: 5.4
3639
env: DB=mysql DEPENDENCIES=lowest
@@ -76,7 +79,7 @@ install:
7679
- if [ "$DEPENDENCIES" = "highest" ]; then composer update -n; fi
7780
- if [ "$JSLINT" = true ]; then npm install; fi
7881
- if [ "$SECURITY" = true ]; then wget http://get.sensiolabs.org/security-checker.phar; fi
79-
- if [ "$TEST_ADAPTER" = "HTTP" ]; then composer require volkszaehler/httpd:dev-master jenssegers/proxy:~2.2; fi
82+
- if [ "$TEST_COMPONENT" = "HTTPD" ]; then composer require volkszaehler/httpd:dev-master jenssegers/proxy:~2.2; fi
8083

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

106109
# create database
@@ -112,15 +115,21 @@ before_script:
112115

113116
# setup local middleware
114117
- |
115-
if [ "$TEST_ADAPTER" = "HTTP" ]; then
116-
sed -i "s/testAdapter\" value=\".*\"/testAdapter\" value=\"$TEST_ADAPTER\"/" phpunit.xml
118+
if [ "$TEST_COMPONENT" = "HTTPD" ]; then
119+
sed -i "s/testAdapter\" value=\".*\"/testAdapter\" value=\"$TEST_COMPONENT\"/" phpunit.xml
117120
vendor/bin/httpd.php start &
118121
HTTPD_PID=$!
119122
echo "Started httpd with pid $HTTPD_PID"
120123
fi
121124
125+
# push server tests
126+
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then sed -i "s/\?>/\$config['push']['enabled']\ =\ true\;\n?>/" etc/volkszaehler.conf.php; fi
127+
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then
128+
php misc/tools/push-server.php &
129+
fi
130+
122131
after_script:
123-
- if [ "$TEST_ADAPTER" = "HTTP" ]; then kill -9 $HTTPD_PID; fi
132+
- if [ "$TEST_COMPONENT" = "HTTPD" ]; then kill -9 $HTTPD_PID; fi
124133

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

153+
# push server tests
154+
- if [ "$TEST_COMPONENT" = "PUSH_SERVER" ]; then phpunit --group pushserver; fi
155+
144156
# jslint javascript sources
145157
- if [ "$JSLINT" = true ]; then gulp jshint; fi
146158

lib/Server/MiddlewareAdapter.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ class MiddlewareAdapter {
4545
*/
4646
protected $em;
4747

48-
/**
49-
* @var EntityController
50-
*/
51-
protected $controller;
52-
5348
/**
5449
* @var array
5550
*/
@@ -68,17 +63,16 @@ public function removeAdapter(PushTransportInterface $adapter) {
6863
}
6964

7065
protected function openController($force = false) {
71-
if ($force || $this->em == null || $this->controller == null || !$this->em->isOpen()) {
66+
if ($force || $this->em == null || !$this->em->isOpen()) {
7267
$this->em = Router::createEntityManager(false);
73-
$this->controller = new EntityController(new Request(), $this->em);
7468
}
7569
}
7670

7771
protected function connectToMiddleware($uuid) {
7872
try {
7973
$this->openController();
8074

81-
$entity = $this->controller->getSingleEntity($uuid);
75+
$entity = EntityController::factory($this->em, $uuid);
8276
$class = $entity->getDefinition()->getInterpreter();
8377
$interpreter = new $class($entity, $this->em, null, null);
8478

@@ -132,24 +126,27 @@ protected function convertRawTuple(Interpreter\Interpreter $interpreter, $tuple)
132126
* @return null|string Returns null on invalid request
133127
*/
134128
public function handlePushMessage($json) {
135-
$msg = json_decode($json, true);
136129
$response = array(
137130
'version' => VZ_VERSION,
138131
'data' => array()
139132
);
140133

141134
// validate input message
135+
if (null === ($msg = json_decode($json, true))) {
136+
return null;
137+
}
142138
if (null === ($data = @$msg['data']) || !is_array($data)) {
143139
return null;
144140
}
145141

146142
// loop through channels
147143
foreach ($data as $channel) {
144+
// validate channel structure
148145
if (null === ($uuid = @$channel['uuid'])) {
149-
break;
146+
return null;
150147
}
151148
if (null === ($tuples = @$channel['tuples']) || !is_array($tuples) || !count($tuples)) {
152-
break;
149+
return null;
153150
}
154151

155152
// get interpreter if no client has connected yet
@@ -161,6 +158,10 @@ public function handlePushMessage($json) {
161158
// convert raw tuples using interpreter rules
162159
$transformed = array();
163160
foreach ($tuples as $tuple) {
161+
if (!is_array($tuple) || count($tuple) < 2) {
162+
return null;
163+
}
164+
164165
if (count($tuple) < 3) {
165166
$tuple[] = 1;
166167
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<exclude>
1313
<group>slow</group>
1414
<group>jpgraph</group>
15+
<group>pushserver</group>
1516
</exclude>
1617
</groups>
1718

test/PushServerTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Meter tests
4+
*
5+
* @package Test
6+
* @author Andreas Götz <[email protected]>
7+
*/
8+
9+
namespace Tests;
10+
11+
use Volkszaehler\Util;
12+
13+
class PushServerTest extends Data
14+
{
15+
// channel properties
16+
static $resolution = 100;
17+
18+
/**
19+
* Create channel
20+
*/
21+
static function setupBeforeClass() {
22+
parent::setupBeforeClass();
23+
self::$uuid = self::createChannel('Counter', 'electric meter', self::$resolution);
24+
}
25+
26+
/**
27+
* @group pushserver
28+
*/
29+
function testPushMessage() {
30+
$this->assertTrue(Util\Configuration::read('push.enabled'), 'Push server disabled');
31+
32+
$exitCode = null;
33+
$port = Util\Configuration::read('push.server');
34+
$curl = "curl %s -s -m 3 -X POST -d '{\"data\":[{\"uuid\":\"%s\",\"tuples\":[[1,1,1]]}]}' localhost:%d 2>&1";
35+
36+
// run and test for failure
37+
$cmd = sprintf($curl, '-f', self::$uuid, $port);
38+
passthru($cmd, $exitCode);
39+
40+
// run to get output
41+
if ($exitCode !== 0) {
42+
$cmd = sprintf($curl, '-i', self::$uuid, $port);
43+
passthru($cmd);
44+
}
45+
46+
$this->assertTrue($exitCode === 0, sprintf('Curl failed with exit code %d', $exitCode));
47+
}
48+
}
49+
50+
?>

0 commit comments

Comments
 (0)