dont call notify callback if we dont have a proper change

This commit is contained in:
Robin Appelman 2017-04-24 15:30:06 +02:00
commit 4ca899f582
2 changed files with 42 additions and 3 deletions

View file

@ -59,7 +59,10 @@ class NotifyHandler implements INotifyHandler {
public function listen($callback) { public function listen($callback) {
if ($this->listening) { if ($this->listening) {
$this->connection->read(function ($line) use ($callback) { $this->connection->read(function ($line) use ($callback) {
return $callback($this->parseChangeLine($line)); $change = $this->parseChangeLine($line);
if ($change) {
return $callback($change);
}
}); });
} }
} }

View file

@ -31,8 +31,8 @@ class NotifyHandlerTest extends TestCase {
* *
* filter them out so we can compare changes properly * filter them out so we can compare changes properly
* *
* @param array $changes * @param Change[] $changes
* @return array * @return Change[]
*/ */
private function filterModifiedChanges(array $changes) { private function filterModifiedChanges(array $changes) {
return array_values(array_filter($changes, function (Change $change) { return array_values(array_filter($changes, function (Change $change) {
@ -114,4 +114,40 @@ class NotifyHandlerTest extends TestCase {
$process->stop(); $process->stop();
$this->assertEquals([], $process->getChanges()); $this->assertEquals([], $process->getChanges());
} }
public function testListenAfterGetChanges() {
$share = $this->server->getShare($this->config->share);
$process = $share->notify('');
usleep(1000 * 100);// give it some time to start listening
$share->put(__FILE__, 'source.txt');
$share->rename('source.txt', 'target.txt');
$share->del('target.txt');
usleep(1000 * 100);// give it some time
$changes = $process->getChanges();
$expected = [
new Change(INotifyHandler::NOTIFY_ADDED, 'source.txt'),
new Change(INotifyHandler::NOTIFY_RENAMED_OLD, 'source.txt'),
new Change(INotifyHandler::NOTIFY_RENAMED_NEW, 'target.txt'),
new Change(INotifyHandler::NOTIFY_REMOVED, 'target.txt'),
];
$this->assertEquals($expected, $this->filterModifiedChanges($changes));
usleep(1000 * 200);
$share->put(__FILE__, 'source2.txt');
$share->del('source2.txt');
$results = null;
// the notify process buffers incoming messages so callback will be triggered for the above changes
$process->listen(function ($change) use (&$results) {
$results = $change;
return false; // stop listening
});
$this->assertNotNull($results);
}
} }