Skip to content

Commit eba79bf

Browse files
Optimized some packing and unpacking operations. Refactored phpunit tests. (#87)
* Optimized some packing and unpacking operations. Refactored phpunit tests. * change of github workflows * backward compatibility * added read mode to test queries
1 parent 509e05d commit eba79bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+528
-693
lines changed

.github/workflows/db-tests.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ name: Tests with a database
22

33
on:
44
push:
5-
branches: [ '**' ]
5+
branches: [ master ]
66
pull_request:
7-
branches: ['**']
7+
branches: [ master ]
88

99
jobs:
1010
tests:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-18.04
1212
name: "Running Integration tests for PHP 7.1 on Neo4j ${{ matrix.neo4j-version }}"
1313
strategy:
1414
fail-fast: false
@@ -28,15 +28,19 @@ jobs:
2828
--health-cmd "wget http://localhost:7474 || exit 1"
2929
3030
steps:
31-
- uses: actions/checkout@v2
32-
- uses: php-actions/composer@v6
33-
with:
34-
progress: yes
35-
php_version: 7.1
36-
version: 2
37-
- uses: php-actions/phpunit@v3
31+
- name: Checkout
32+
uses: actions/checkout@v3
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
3836
with:
39-
configuration: phpunit.xml.dist
40-
php_version: 7.1
41-
version: 7.5
42-
args: --testsuite "Database"
37+
php-version: '7.1'
38+
extensions: mbstring, sockets
39+
coverage: xdebug2
40+
ini-values: max_execution_time=0
41+
42+
- name: Install dependencies
43+
run: composer install --no-progress
44+
45+
- name: Test with phpunit
46+
run: vendor/bin/phpunit --configuration phpunit.xml --testsuite "Database"

.github/workflows/no-db-test-php-7.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ name: Tests without a database PHP7
22

33
on:
44
push:
5-
branches: [ '**' ]
5+
branches: [ master ]
66
pull_request:
7-
branches: ['**']
7+
branches: [ master ]
88

99
jobs:
1010
tests:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-18.04
1212
name: "Running Tests for PHP ${{ matrix.php-version }}"
1313
strategy:
1414
fail-fast: false
1515
matrix:
1616
php-version: ['7.1', '7.2', '7.3', '7.4']
1717

1818
steps:
19-
- uses: actions/checkout@v2
20-
- uses: php-actions/composer@v6
21-
with:
22-
progress: yes
23-
php_version: ${{ matrix.php-version }}
24-
version: 2
25-
- uses: php-actions/phpunit@v3
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
2624
with:
27-
configuration: phpunit.xml.dist
28-
version: 7.5
29-
php_version: ${{ matrix.php-version }}
30-
args: --testsuite "NoDatabase"
25+
php-version: ${{ matrix.php-version }}
26+
extensions: mbstring, sockets
27+
coverage: xdebug
28+
ini-values: max_execution_time=0
29+
30+
- name: Install dependencies
31+
run: composer install --no-progress
32+
33+
- name: Test with phpunit
34+
run: vendor/bin/phpunit --configuration phpunit.xml --testsuite "NoDatabase"

.github/workflows/no-db-test-php-8.yml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ name: Tests without a database PHP8
22

33
on:
44
push:
5-
branches: [ '**' ]
5+
branches: [ master ]
66
pull_request:
7-
branches: ['**']
7+
branches: [ master ]
88

99
jobs:
1010
tests:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-20.04
1212
name: "Running Tests for PHP ${{ matrix.php-version }}"
1313
strategy:
1414
fail-fast: false
1515
matrix:
1616
php-version: ['8.0', '8.1']
1717

1818
steps:
19-
- uses: actions/checkout@v2
20-
- uses: php-actions/composer@v6
21-
with:
22-
progress: yes
23-
php_version: ${{ matrix.php-version }}
24-
version: 2
25-
- uses: php-actions/phpunit@v3
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
2624
with:
27-
configuration: phpunit.xml.dist
28-
php_version: ${{ matrix.php-version }}
29-
args: --testsuite "NoDatabase"
25+
php-version: ${{ matrix.php-version }}
26+
extensions: mbstring, sockets
27+
coverage: xdebug
28+
ini-values: max_execution_time=0
29+
30+
- name: Install dependencies
31+
run: composer install --no-progress
32+
33+
- name: Test with phpunit
34+
run: vendor/bin/phpunit --configuration phpunit.xml --testsuite "NoDatabase"

phpunit.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
<phpunit bootstrap="src/autoload.php">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="src/autoload.php">
23
<testsuites>
3-
<testsuite name="Bolt">
4-
<directory>tests</directory>
4+
<testsuite name="Database">
5+
<directory>./tests/connection</directory>
6+
<file>./tests/BoltTest.php</file>
7+
<directory>./tests/error</directory>
8+
<directory>./tests/PackStream</directory>
9+
</testsuite>
10+
<testsuite name="NoDatabase">
11+
<directory>./tests/protocol</directory>
512
</testsuite>
613
</testsuites>
714
<php>
815
<var name="NEO_USER" value="neo4j"/>
916
<var name="NEO_PASS" value="nothing"/>
1017
</php>
11-
<filter>
12-
<whitelist>
13-
<directory suffix=".php">src/connection</directory>
14-
<directory suffix=".php">src/PackStream</directory>
15-
<directory suffix=".php">src/protocol</directory>
16-
<directory suffix=".php">src/helpers</directory>
17-
<directory suffix=".php">src/structures</directory>
18-
<file>src/Bolt.php</file>
19-
</whitelist>
20-
</filter>
21-
</phpunit>
18+
</phpunit>

phpunit.xml.dist

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/PackStream/v1/Packer.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Generator;
88
use Bolt\structures\{
99
IStructure,
10-
Relationship,
1110
Date,
1211
Time,
1312
LocalTime,
@@ -158,7 +157,8 @@ private function packString(string $str): string
158157
*/
159158
private function packFloat(float $value): string
160159
{
161-
return chr(0xC1) . strrev(pack('d', $value));
160+
$packed = pack('d', $value);
161+
return chr(0xC1) . ($this->littleEndian ? strrev($packed) : $packed);
162162
}
163163

164164
/**
@@ -168,15 +168,10 @@ private function packFloat(float $value): string
168168
*/
169169
private function packInteger(int $value): string
170170
{
171-
if ($value >= 0 && $value <= 127) { //+TINY_INT
172-
$packed = pack('C', 0b00000000 | $value);
173-
return $this->littleEndian ? strrev($packed) : $packed;
174-
} elseif ($value >= -16 && $value < 0) { //-TINY_INT
175-
$packed = pack('c', 0b11110000 | $value);
176-
return $this->littleEndian ? strrev($packed) : $packed;
171+
if ($value >= -16 && $value <= 127) { //TINY_INT
172+
return pack('c', $value);
177173
} elseif ($value >= -128 && $value <= -17) { //INT_8
178-
$packed = pack('c', $value);
179-
return chr(0xC8) . ($this->littleEndian ? strrev($packed) : $packed);
174+
return chr(0xC8) . pack('c', $value);
180175
} elseif (($value >= 128 && $value <= 32767) || ($value >= -32768 && $value <= -129)) { //INT_16
181176
$packed = pack('s', $value);
182177
return chr(0xC9) . ($this->littleEndian ? strrev($packed) : $packed);

src/PackStream/v1/Unpacker.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,8 @@ private function unpackString(int $marker): ?string
252252
*/
253253
private function unpackInteger(int $marker): ?int
254254
{
255-
if ($marker >> 7 == 0b0) { //+TINY_INT
256-
return $marker;
257-
} elseif ($marker >> 4 == 0b1111) { //-TINY_INT
258-
return (int)unpack('c', strrev(chr($marker)))[1];
255+
if ($marker >> 4 >= 0xF || $marker >> 4 <= 0x7) { //TINY_INT
256+
return (int)unpack('c', chr($marker))[1];
259257
} elseif ($marker == 0xC8) { //INT_8
260258
return (int)unpack('c', $this->next(1))[1];
261259
} elseif ($marker == 0xC9) { //INT_16
@@ -266,7 +264,7 @@ private function unpackInteger(int $marker): ?int
266264
return (int)unpack('l', $this->littleEndian ? strrev($value) : $value)[1];
267265
} elseif ($marker == 0xCB) { //INT_64
268266
$value = $this->next(8);
269-
return (int)unpack("q", $this->littleEndian ? strrev($value) : $value)[1];
267+
return (int)unpack('q', $this->littleEndian ? strrev($value) : $value)[1];
270268
} else {
271269
return null;
272270
}
@@ -279,7 +277,8 @@ private function unpackInteger(int $marker): ?int
279277
private function unpackFloat(int $marker): ?float
280278
{
281279
if ($marker == 0xC1) {
282-
return (float)unpack('d', strrev($this->next(8)))[1];
280+
$value = $this->next(8);
281+
return (float)unpack('d', $this->littleEndian ? strrev($value) : $value)[1];
283282
} else {
284283
return null;
285284
}

src/connection/AConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function printHex(string $str, string $prefix = 'C: ')
5353
echo implode(' ', str_split($chunk, 2));
5454
echo ' ';
5555
}
56-
echo '</pre>';
56+
echo '</pre>' . PHP_EOL;
5757
}
5858

5959
public function getIp(): string
@@ -71,7 +71,7 @@ public function getTimeout(): float
7171
return $this->timeout;
7272
}
7373

74-
public function setTimeout(float $timeout): void
74+
public function setTimeout(float $timeout)
7575
{
7676
$this->timeout = $timeout;
7777
}

src/connection/IConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public function getPort(): int;
3232

3333
public function getTimeout(): float;
3434

35-
public function setTimeout(float $timeout): void;
35+
public function setTimeout(float $timeout);
3636
}

src/connection/Socket.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class Socket extends AConnection
2222
private $socket = false;
2323

2424
private const POSSIBLE_TIMEOUTS_CODES = [11, 10060];
25-
/** @var float|null */
26-
private $timetAtTimeoutConfiguration;
2725

2826
/**
2927
* Create socket connection
@@ -69,18 +67,14 @@ public function write(string $buffer)
6967
throw new ConnectException('Not initialized socket');
7068
}
7169

72-
$size = mb_strlen($buffer, '8bit');
73-
$sent = 0;
74-
7570
if (Bolt::$debug)
7671
$this->printHex($buffer);
7772

73+
$size = mb_strlen($buffer, '8bit');
7874
while (0 < $size) {
7975
$sent = @socket_write($this->socket, $buffer, $size);
80-
if ($sent === false) {
76+
if ($sent === false)
8177
$this->throwConnectException();
82-
}
83-
8478
$buffer = mb_strcut($buffer, $sent, null, '8bit');
8579
$size -= $sent;
8680
}
@@ -94,17 +88,14 @@ public function write(string $buffer)
9488
*/
9589
public function read(int $length = 2048): string
9690
{
97-
$output = '';
98-
99-
if ($this->socket === false) {
91+
if ($this->socket === false)
10092
throw new ConnectException('Not initialized socket');
101-
}
10293

94+
$output = '';
10395
do {
10496
$readed = @socket_read($this->socket, $length - mb_strlen($output, '8bit'), PHP_BINARY_READ);
105-
if ($readed === false) {
97+
if ($readed === false)
10698
$this->throwConnectException();
107-
}
10899
$output .= $readed;
109100
} while (mb_strlen($output, '8bit') < $length);
110101

@@ -125,20 +116,21 @@ public function disconnect()
125116
}
126117
}
127118

128-
public function setTimeout(float $timeout): void
119+
public function setTimeout(float $timeout)
129120
{
130121
parent::setTimeout($timeout);
131122
$this->configureTimeout();
132123
}
133124

134-
private function configureTimeout(): void
125+
private function configureTimeout()
135126
{
127+
if ($this->socket === false)
128+
return;
136129
$timeoutSeconds = floor($this->timeout);
137130
$microSeconds = floor(($this->timeout - $timeoutSeconds) * 1000000);
138131
$timeoutOption = ['sec' => $timeoutSeconds, 'usec' => $microSeconds];
139132
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $timeoutOption);
140133
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, $timeoutOption);
141-
$this->timetAtTimeoutConfiguration = microtime(true);
142134
}
143135

144136
/**
@@ -149,11 +141,8 @@ private function throwConnectException(): void
149141
{
150142
$code = socket_last_error($this->socket);
151143
if (in_array($code, self::POSSIBLE_TIMEOUTS_CODES)) {
152-
$timediff = microtime(true) - $this->timetAtTimeoutConfiguration;
153-
if ($timediff >= $this->timeout) {
154-
throw ConnectionTimeoutException::createFromTimeout($this->timeout);
155-
}
156-
} else if ($code !== 0) {
144+
throw new ConnectionTimeoutException('Connection timeout reached after ' . $this->timeout . ' seconds.');
145+
} elseif ($code !== 0) {
157146
throw new ConnectException(socket_strerror($code), $code);
158147
}
159148
}

0 commit comments

Comments
 (0)