Skip to content

Wait for pending update method #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,4 @@ A webhook will be triggered and push the new package on [Packagist](https://pack

## 🤖 Compatibility with MeiliSearch

This package works for MeiliSearch `v0.9.x`.
This package works for MeiliSearch `>=v0.10`.
6 changes: 3 additions & 3 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ public function getAllUpdateStatus()
return $this->httpGet('/indexes/'.$this->uid.'/updates');
}

public function waitForUpdateStatus($update_id, $timeout_in_ms = 2000, $range_in_ms = 10)
public function waitForPendingUpdate($update_id, $timeout_in_ms = 5000, $interval_in_ms = 50)
{
$timeout_temp = 0;
while ($timeout_in_ms > $timeout_temp) {
$res = $this->getUpdateStatus($update_id);
if ('enqueued' != $res['status']) {
return $res;
}
$timeout_temp += $range_in_ms;
usleep(1000 * $range_in_ms);
$timeout_temp += $interval_in_ms;
usleep(1000 * $interval_in_ms);
}
throw new TimeOutException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/MeiliSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class MeiliSearch
{
const VERSION = '0.9.0';
const VERSION = '0.10.0';
}
18 changes: 9 additions & 9 deletions tests/DocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testAddDocuments()
$res = static::$index->addDocuments(static::$documents);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
}

// DOCUMENTS
Expand All @@ -67,7 +67,7 @@ public function testReplaceDocuments()
$res = static::$index->addDocuments([['id' => $id, 'title' => $new_title]]);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocument($id);
$this->assertSame($id, $res['id']);
$this->assertSame($new_title, $res['title']);
Expand All @@ -83,7 +83,7 @@ public function testUpdateDocuments()
$res = static::$index->updateDocuments([['id' => $id, 'title' => $new_title]]);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocument($id);
$this->assertSame($id, $res['id']);
$this->assertSame($new_title, $res['title']);
Expand All @@ -99,7 +99,7 @@ public function testAddWithUpdateDocuments()
$res = static::$index->updateDocuments([['id' => $id, 'title' => $title]]);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocument($id);
$this->assertSame($id, $res['id']);
$this->assertSame($title, $res['title']);
Expand All @@ -114,7 +114,7 @@ public function testDeleteDocument()
$res = static::$index->deleteDocument($id);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocuments();
$this->assertCount(count(static::$documents), $res);
$this->assertNull($this->findDocumentWithId($res, $id));
Expand All @@ -126,7 +126,7 @@ public function testDeleteDocuments()
$res = static::$index->deleteDocuments($ids);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocuments();
$this->assertCount(count(static::$documents) - 2, $res);
$this->assertNull($this->findDocumentWithId($res, $ids[0]));
Expand All @@ -138,7 +138,7 @@ public function testDeleteAllDocuments()
$res = static::$index->deleteAllDocuments();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$res = static::$index->getDocuments();
$this->assertCount(0, $res);
}
Expand All @@ -161,7 +161,7 @@ public function testAddDocumentWithPrimaryKey()
$index = static::$client->createIndex('addUid');
$res = $index->addDocuments($documents, 'unique');
$this->assertArrayHasKey('updateId', $res);
$index->waitForUpdateStatus($res['updateId']);
$index->waitForPendingUpdate($res['updateId']);
$this->assertSame('unique', $index->getPrimaryKey());
$this->assertCount(1, $index->getDocuments());
}
Expand All @@ -178,7 +178,7 @@ public function testUpdateDocumentWithPrimaryKey()
$index = static::$client->createIndex('udpateUid');
$res = $index->updateDocuments($documents, 'unique');
$this->assertArrayHasKey('updateId', $res);
$index->waitForUpdateStatus($res['updateId']);
$index->waitForPendingUpdate($res['updateId']);
$this->assertSame('unique', $index->getPrimaryKey());
$this->assertCount(1, $index->getDocuments());
}
Expand Down
50 changes: 50 additions & 0 deletions tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use MeiliSearch\Client;
use MeiliSearch\Exceptions\HTTPRequestException;
use MeiliSearch\Exceptions\TimeOutException;
use PHPUnit\Framework\TestCase;

require_once 'utils.php';
Expand Down Expand Up @@ -74,6 +75,55 @@ public function testIndexStats()
$this->assertArrayHasKey('fieldsFrequency', $res);
}

public function testWaitForPendingUpdateDefault()
{
$first_upd = static::$index1->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
$res = static::$index1->waitForPendingUpdate($first_upd['updateId']);
$this->assertIsArray($res);
$this->assertSame($res['status'], 'processed');
$this->assertSame($res['updateId'], $first_upd['updateId']);
$this->assertArrayHasKey('type', $res);
$this->assertIsArray($res['type']);
$this->assertArrayHasKey('duration', $res);
$this->assertArrayHasKey('enqueuedAt', $res);
$this->assertArrayHasKey('processedAt', $res);
}

public function testWaitForPendingUpdateCustom()
{
$first_upd = static::$index1->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
$res = static::$index1->waitForPendingUpdate($first_upd['updateId'], 100, 20);
$this->assertIsArray($res);
$this->assertSame($res['status'], 'processed');
$this->assertSame($res['updateId'], $first_upd['updateId']);
$this->assertArrayHasKey('type', $res);
$this->assertIsArray($res['type']);
$this->assertArrayHasKey('duration', $res);
$this->assertArrayHasKey('enqueuedAt', $res);
$this->assertArrayHasKey('processedAt', $res);
}

public function testWaitForPendingUpdateCustom2()
{
$first_upd = static::$index1->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
$res = static::$index1->waitForPendingUpdate($first_upd['updateId'], 100);
$this->assertIsArray($res);
$this->assertSame($res['status'], 'processed');
$this->assertSame($res['updateId'], $first_upd['updateId']);
$this->assertArrayHasKey('type', $res);
$this->assertIsArray($res['type']);
$this->assertArrayHasKey('duration', $res);
$this->assertArrayHasKey('enqueuedAt', $res);
$this->assertArrayHasKey('processedAt', $res);
}

public function testExceptionWhenPendingUpdateTimeOut()
{
$this->expectException(TimeOutException::class);
$res = static::$index1->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
static::$index1->waitForPendingUpdate($res['updateId'], 0, 20);
}

public function testDelete()
{
$res = static::$index1->delete();
Expand Down
2 changes: 1 addition & 1 deletion tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function setUpBeforeClass(): void
['id' => 42, 'title' => 'The Hitchhiker\'s Guide to the Galaxy'],
];
$res = static::$index->updateDocuments($documents);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
}

public static function tearDownAfterClass(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/Settings/AcceptNewFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class AcceptNewFieldsTest extends TestCase
{
Expand Down Expand Up @@ -36,7 +35,7 @@ public function testUpdateAcceptNewFields()
$res = static::$index->updateAcceptNewFields(false);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$this->assertFalse(static::$index->getAcceptNewFields());
}
}
7 changes: 3 additions & 4 deletions tests/Settings/DisplayedAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class DisplayedAttributesTest extends TestCase
{
Expand Down Expand Up @@ -45,7 +44,7 @@ public function testUpdateDisplayedAttributes()
$res = static::$index1->updateDisplayedAttributes($new_da);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$da = static::$index1->getDisplayedAttributes();
$this->assertIsArray($da);
$this->assertEquals($new_da, $da);
Expand All @@ -56,7 +55,7 @@ public function testResetDisplayedAttributes()
$res = static::$index1->resetDisplayedAttributes();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$da = static::$index1->getDisplayedAttributes();
$this->assertIsArray($da);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Settings/DistinctAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class DistinctAttributeTest extends TestCase
{
Expand Down Expand Up @@ -37,7 +36,7 @@ public function testUpdateDistinctAttribute()
$res = static::$index->updateDistinctAttribute($new_da);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$da = static::$index->getDistinctAttribute();
$this->assertEquals($new_da, $da);
}
Expand All @@ -47,7 +46,7 @@ public function testResetDistinctAttribute()
$res = static::$index->resetDistinctAttribute();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$this->assertNull(static::$index->getDistinctAttribute());
}
}
7 changes: 3 additions & 4 deletions tests/Settings/RankingRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class RankingRulesTest extends TestCase
{
Expand Down Expand Up @@ -51,7 +50,7 @@ public function testUpdateRankingRules()
$res = static::$index->updateRankingRules($new_rr);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$rr = static::$index->getRankingRules();
$this->assertIsArray($rr);
$this->assertEquals($new_rr, $rr);
Expand All @@ -62,7 +61,7 @@ public function testResetRankingRules()
$res = static::$index->resetRankingRules();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$rr = static::$index->getRankingRules();
$this->assertIsArray($rr);
$this->assertEquals(static::$default_ranking_rules, $rr);
Expand Down
7 changes: 3 additions & 4 deletions tests/Settings/SearchableAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class SearchableAttributesTest extends TestCase
{
Expand Down Expand Up @@ -48,7 +47,7 @@ public function testUpdateSearchableAttributes()
$res = static::$index1->updateSearchableAttributes($new_sa);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$sa = static::$index1->getSearchableAttributes();
$this->assertIsArray($sa);
$this->assertEquals($new_sa, $sa);
Expand All @@ -59,7 +58,7 @@ public function testResetSearchableAttributes()
$res = static::$index1->resetSearchableAttributes();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$sa = static::$index1->getSearchableAttributes();
$this->assertIsArray($sa);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/Settings/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class SettingsTest extends TestCase
{
Expand Down Expand Up @@ -76,7 +75,7 @@ public function testUpdateSettings()
]);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$settings = static::$index1->getSettings();
$this->assertEquals(['asc(title)', 'typo'], $settings['rankingRules']);
$this->assertEquals('title', $settings['distinctAttribute']);
Expand All @@ -97,7 +96,7 @@ public function testUpdateSettingsWithoutOverwritingThem()
]);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$settings = static::$index1->getSettings();
$this->assertEquals(['asc(title)', 'typo'], $settings['rankingRules']);
$this->assertEquals('title', $settings['distinctAttribute']);
Expand All @@ -115,7 +114,7 @@ public function testResetSettings()
$res = static::$index1->resetSettings();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index1->waitForUpdateStatus($res['updateId']);
static::$index1->waitForPendingUpdate($res['updateId']);
$settings = static::$index1->getSettings();
$this->assertEquals(static::$default_ranking_rules, $settings['rankingRules']);
$this->assertNull($settings['distinctAttribute']);
Expand Down
7 changes: 3 additions & 4 deletions tests/Settings/StopWordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use MeiliSearch\Client;
use PHPUnit\Framework\TestCase;

define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__.'/utils.php';
require_once dirname(dirname(__FILE__)).'/utils.php';

class StopWordsTest extends TestCase
{
Expand Down Expand Up @@ -38,7 +37,7 @@ public function testUpdateStopWords()
$res = static::$index->updateStopWords($new_sw);
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$sw = static::$index->getStopWords();
$this->assertIsArray($sw);
$this->assertEquals($new_sw, $sw);
Expand All @@ -49,7 +48,7 @@ public function testResetStopWords()
$res = static::$index->resetStopWords();
$this->assertIsArray($res);
$this->assertArrayHasKey('updateId', $res);
static::$index->waitForUpdateStatus($res['updateId']);
static::$index->waitForPendingUpdate($res['updateId']);
$sw = static::$index->getStopWords();
$this->assertIsArray($sw);
$this->assertEmpty($sw);
Expand Down
Loading