Skip to content

Commit

Permalink
Prep 1.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed May 28, 2017
1 parent a5b052e commit 05059bc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 45 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.x.x (2017-xx-xx)
## 1.4.4 (2017-05-28)

## Fixed

- Ensure BrokenConnectionException if socket write/read fails (communication interrupted)
- Minor fix and cleanup in `SimpleHttpHandler` channel creation.

## Added
Expand Down
2 changes: 1 addition & 1 deletion src/Soluble/Japha/Bridge/Driver/Pjb62/NativeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected function begin($parser, $name, $param)
case 'X':
case 'A':
$this->level += 1;
break;
}
$this->client->begin($name, $param);
}
Expand Down Expand Up @@ -128,7 +129,6 @@ public function parse()
{
do {
$this->event = false;

$this->buf = $this->client->read($this->java_recv_size);
$len = strlen($this->buf);
if (!xml_parse($this->parser, $this->buf, $len == 0)) {
Expand Down
55 changes: 21 additions & 34 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/PjbProxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ class PjbProxyClient implements ClientInterface
*/
protected function __construct(array $options, LoggerInterface $logger)
{
self::$instanceOptionsKey = serialize($options);
$this->options = new ArrayObject(array_merge($options, $this->defaultOptions));
self::$instanceOptionsKey = serialize((array) $this->options);

$this->logger = $logger;
$this->loadClient();
}
Expand Down Expand Up @@ -203,7 +204,7 @@ protected function loadClient()
self::$client = new Client($params, $this->logger);

// Added in order to work with custom exceptions
self::$client->throwExceptionProxyFactory = new Proxy\DefaultThrowExceptionProxyFactory(self::$client, $this->logger);
self::getClient()->throwExceptionProxyFactory = new Proxy\DefaultThrowExceptionProxyFactory(self::$client, $this->logger);

$this->bootstrap();
}
Expand All @@ -216,6 +217,10 @@ protected function loadClient()
*/
public static function getClient()
{
if (self::$client === null) {
throw new Exception\BrokenConnectionException('Client is not registered');
}

return self::$client;
}

Expand Down Expand Up @@ -264,7 +269,7 @@ public function invokeMethod(Interfaces\JavaType $object = null, $method, array
{
$id = ($object == null) ? 0 : $object->__getJavaInternalObjectId();

return self::$client->invokeMethod($id, $method, $args);
return self::getClient()->invokeMethod($id, $method, $args);
}

/**
Expand All @@ -282,7 +287,7 @@ public function inspect(Interfaces\JavaType $object)
{
//$client = self::getClient();
//return $client->invokeMethod(0, "inspect", array($object));
return self::$client->invokeMethod(0, 'inspect', [$object]);
return self::getClient()->invokeMethod(0, 'inspect', [$object]);
}

/**
Expand All @@ -307,7 +312,7 @@ public function isInstanceOf(Interfaces\JavaObject $object, $class)
throw new Exception\InvalidArgumentException(__METHOD__ . 'Class $class parameter must be of Interfaces\JavaClass, Interfaces\JavaObject or string');
}

return self::$client->invokeMethod(0, 'instanceOf', [$object, $class]);
return self::getClient()->invokeMethod(0, 'instanceOf', [$object, $class]);
}

/**
Expand Down Expand Up @@ -350,7 +355,7 @@ public function isInstanceOf(Interfaces\JavaObject $object, $class)
*/
public function getValues(Interfaces\JavaObject $object)
{
return self::$client->invokeMethod(0, 'getValues', [$object]);
return self::getClient()->invokeMethod(0, 'getValues', [$object]);
}

/**
Expand All @@ -364,7 +369,7 @@ public function getValues(Interfaces\JavaObject $object)
*/
public function getLastException()
{
return self::$client->invokeMethod(0, 'getLastException', []);
return self::getClient()->invokeMethod(0, 'getLastException', []);
}

/**
Expand All @@ -376,7 +381,7 @@ public function getLastException()
*/
public function clearLastException()
{
self::$client->invokeMethod(0, 'clearLastException', []);
self::getClient()->invokeMethod(0, 'clearLastException', []);
}

/**
Expand Down Expand Up @@ -475,28 +480,26 @@ public function getOption($name)
*/
public static function unregisterInstance()
{
if (self::isInitialized()) {
if (self::$client !== null) {
// TODO CHECK WITH SESSIONS
if (session_id()) {
session_write_close();
}
if (!isset(self::$client->protocol) || self::$client->inArgs) {
return;
}
//if (session_id()) {
// session_write_close();
//}
//if (!isset(self::$client->protocol) || self::$client->inArgs) {
//return;
//}
if (self::$client->preparedToSendBuffer) {
self::$client->sendBuffer .= self::$client->preparedToSendBuffer;
}

self::$client->sendBuffer .= self::$client->protocol->getKeepAlive();

self::$client->protocol->flush();

// TODO MUST TEST, IT WAS REMOVED FROM FUNCTION
// BECAUSE IT SIMPLY LOOKS LIKE THE LINES BEFORE
// ADDED AN IF TO CHECK THE CHANNEL In CASE OF
//
if (isset(self::$client->protocol->handler->channel) &&
!preg_match('/EmptyChannel/', get_class(self::$client->protocol->handler->channel))) {
!preg_match('/EmptyChannel/', get_class(self::getClient()->protocol->handler->channel))) {
try {
self::$client->protocol->keepAlive();
} catch (\Exception $e) {
Expand All @@ -507,22 +510,6 @@ public static function unregisterInstance()
self::$client = null;
self::$instance = null;
self::$instanceOptionsKey = null;
//unset(self::$instance);
}
}

public static function destroy()
{
self::$client = null;
self::$instance = null;
self::$instanceOptionsKey = null;
}

/**
* Before removing instance.
*/
public function __destruct()
{
$this->unregisterInstance();
}
}
18 changes: 16 additions & 2 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/SocketChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

namespace Soluble\Japha\Bridge\Driver\Pjb62;

use Soluble\Japha\Bridge\Exception\BrokenConnectionException;

abstract class SocketChannel extends EmptyChannel
{
public $peer;
Expand Down Expand Up @@ -73,7 +75,13 @@ public function __construct($peer, $host, $recv_size, $send_size)
*/
public function fwrite($data)
{
return fwrite($this->peer, $data);
$written = @fwrite($this->peer, $data);
if ($written === false) {
PjbProxyClient::unregisterInstance();
throw new BrokenConnectionException('Broken socket communication with the php-java-bridge (write)');
}

return $written;
}

/**
Expand All @@ -83,7 +91,13 @@ public function fwrite($data)
*/
public function fread($size)
{
return fread($this->peer, $size);
$read = @fread($this->peer, $size);
if ($read === false) {
PjbProxyClient::unregisterInstance();
throw new BrokenConnectionException('Broken socket communication with the php-java-bridge (read)');
}

return $read;
}

public function shutdownBrokenConnection()
Expand Down
1 change: 1 addition & 0 deletions src/Soluble/Japha/Bridge/Driver/Pjb62/SocketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function dieWithBrokenConnection($msg = '')

// Log error
$client = $this->protocol->getClient();

$client->getLogger()->critical("[soluble-japha] Broken connection: $msg, check the backend log for details\" (" . __METHOD__ . ')');

PjbProxyClient::unregisterInstance();
Expand Down
32 changes: 32 additions & 0 deletions test/src/SolubleTest/Japha/Bridge/Driver/Pjb62/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,36 @@ public function testGetInternalEncoding()
$enc = $this->client->getInternalEncoding();
$this->assertEquals('UTF-8', $enc);
}

public function testUnsupportedXMLWillCallClientParserError()
{
// SHOULD BE TESTED
//$this->client->begin('G', []);

/*
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
$clientMock->method('read')
->withAnyParameters()
// Unsupported protocol object identification
->willReturn('<G v="efd" m="sun.util.calendar.ZoneInfo" p="O" n="T"/>');
$parserStub = $this->prophesize(NativeParser::class);
$parserStub->parserError()->shouldBeCalled();
$clientMock->parser = $parserStub->reveal();
*/

//$clientMock->protocol->handler = $handlerStub->reveal();

/*
$parser = new NativeParser($clientMock);
$parser->parse();
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class NativeParserTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Skipped native parser tests, HHVM use SimpleParser instead');
}

$this->servlet_address = \SolubleTestFactories::getJavaBridgeServerAddress();
$this->adapter = new Adapter([
'driver' => 'Pjb62',
Expand All @@ -47,12 +51,8 @@ protected function setUp()
$this->client = $this->adapter->getDriver()->getClient()->getClient();
}

public function testParseError()
public function testInvalidXMLWillShutdownBrokenConnection()
{
//$clientStub = $this->prophesize(Client::class);
//$clientStub->read()->with($this->client->java_recv_size)->willReturn($this->returnValue('<xml></xml>'));
//$clientMock = $this->createMock(Client::class);

$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->disableOriginalClone()
Expand Down Expand Up @@ -82,10 +82,12 @@ public function testParserGetDataBase64()
$this->assertEquals('你好,世界', $nativeParser->getData(base64_encode('你好,世界')));
}

/*
protected function getNativeParserMock()
{
$stub = $this->createMock(NativeParser::class);
$stub->method('getData')
->will($this->returnCallback('base64_decode'));
}
*/
}
5 changes: 3 additions & 2 deletions test/src/SolubleTest/Japha/Bridge/EdgeCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testJavaBigMemory()
{
$ba = $this->adapter;
$save_mem = ini_get('memory_limit');
$memory_test = 31554432;
if (defined('HHVM_VERSION')) {
ini_set('memory_limit', '800M');
} else {
Expand All @@ -69,9 +70,9 @@ public function testJavaBigMemory()

// Very big string
$initial_mem = memory_get_usage();
$s = str_repeat('1', 31554432);
$s = str_repeat('1', $memory_test);
$str = $ba->java('java.lang.String', $s);
$this->assertEquals(31554432, $str->length());
$this->assertEquals($memory_test, $str->length());
$full_mem = memory_get_usage();

// releasing
Expand Down

0 comments on commit 05059bc

Please sign in to comment.