Skip to content
Open
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
19 changes: 7 additions & 12 deletions docs/includes/fundamentals/as-avs/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Illuminate\Support\Facades\DB;
use MongoDB\Builder\Query;
use MongoDB\Builder\Search;
use MongoDB\Collection;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Schema\Builder;
use MongoDB\Laravel\Tests\AtlasSearchIndexManagement;
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\Group;

Expand All @@ -18,11 +20,12 @@
use function rand;
use function range;
use function srand;
use function usleep;

#[Group('atlas-search')]
class AtlasSearchTest extends TestCase
{
use AtlasSearchIndexManagement;

private array $vectors;

protected function setUp(): void
Expand All @@ -32,6 +35,7 @@ protected function setUp(): void
parent::setUp();

$moviesCollection = DB::connection('mongodb')->getCollection('movies');
self::assertInstanceOf(Collection::class, $moviesCollection);
$moviesCollection->drop();

Movie::insert([
Expand All @@ -49,7 +53,7 @@ protected function setUp(): void
['title' => 'D', 'plot' => 'Stranded on a distant planet, astronauts must repair their ship before supplies run out.'],
]));

$moviesCollection = DB::connection('mongodb')->getCollection('movies');
$this->waitForSearchIndexesDropped($moviesCollection);

try {
$moviesCollection->createSearchIndex([
Expand Down Expand Up @@ -82,16 +86,7 @@ protected function setUp(): void
throw $e;
}

// Waits for the index to be ready
do {
$ready = true;
usleep(10_000);
foreach ($moviesCollection->listSearchIndexes() as $index) {
if ($index['status'] !== 'READY') {
$ready = false;
}
}
} while (! $ready);
$this->waitForSearchIndexesReady($moviesCollection);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/AtlasSearchIndexManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace MongoDB\Laravel\Tests;

use MongoDB\Collection;
use RuntimeException;

use function hrtime;
use function usleep;

/**
* Helpers for managing Atlas Search indexes in tests with awaiting mechanism.
*/
trait AtlasSearchIndexManagement
{
/**
* Waits for the search index created in the previous test to be deleted
*/
public function waitForSearchIndexesDropped(Collection $collection)
{
$timeout = hrtime()[0] + 30;
// Waits for the search index created in the previous test to be deleted
while ($collection->listSearchIndexes()->count()) {
if (hrtime()[0] > $timeout) {
throw new RuntimeException('Timed out waiting for search indexes to be dropped');
}

usleep(1000);
}
}

/**
* Waits for all search indexes to be ready
*/
public function waitForSearchIndexesReady(Collection $collection)
{
$timeout = hrtime()[0] + 30;
do {
if (hrtime()[0] > $timeout) {
throw new RuntimeException('Timed out waiting for search indexes to be ready');
}

usleep(1000);
$ready = true;
foreach ($collection->listSearchIndexes() as $index) {
$ready = $ready && $index['queryable'];
}
} while (! $ready);
}
}
23 changes: 9 additions & 14 deletions tests/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@
use function rand;
use function range;
use function srand;
use function usleep;
use function usort;

#[Group('atlas-search')]
class AtlasSearchTest extends TestCase
{
use AtlasSearchIndexManagement;

private array $vectors;

public function setUp(): void
{
parent::setUp();

$collection = $this->getConnection('mongodb')->getCollection('books');
assert($collection instanceof MongoDBCollection);
$collection->drop();

Book::insert($this->addVector([
['title' => 'Introduction to Algorithms'],
['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'],
Expand All @@ -54,10 +59,9 @@ public function setUp(): void
['title' => 'Pattern Recognition and Machine Learning'],
]));

$collection = $this->getConnection('mongodb')->getCollection('books');
assert($collection instanceof MongoDBCollection);

try {
$this->waitForSearchIndexesDropped($collection);

$collection->createSearchIndex([
'mappings' => [
'fields' => [
Expand Down Expand Up @@ -89,16 +93,7 @@ public function setUp(): void
throw $e;
}

// Wait for the index to be ready
do {
$ready = true;
usleep(10_000);
foreach ($collection->listSearchIndexes() as $index) {
if ($index['status'] !== 'READY') {
$ready = false;
}
}
} while (! $ready);
$this->waitForSearchIndexesReady($collection);
}

public function tearDown(): void
Expand Down
21 changes: 15 additions & 6 deletions tests/Scout/ScoutIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\LazyCollection;
use Laravel\Scout\ScoutServiceProvider;
use LogicException;
use MongoDB\Laravel\Tests\AtlasSearchIndexManagement;
use MongoDB\Laravel\Tests\Scout\Models\ScoutUser;
use MongoDB\Laravel\Tests\Scout\Models\SearchableInSameNamespace;
use MongoDB\Laravel\Tests\TestCase;
Expand All @@ -17,6 +18,7 @@
use function array_merge;
use function count;
use function env;
use function hrtime;
use function iterator_to_array;
use function Orchestra\Testbench\artisan;
use function range;
Expand All @@ -26,6 +28,8 @@
#[Group('atlas-search')]
class ScoutIntegrationTest extends TestCase
{
use AtlasSearchIndexManagement;

#[Override]
protected function getPackageProviders($app): array
{
Expand Down Expand Up @@ -96,23 +100,28 @@ public function setUp(): void
/** This test create the search index for tests performing search */
public function testItCanCreateTheCollection()
{
$this->skipIfSearchIndexManagementIsNotSupported();

$collection = DB::connection('mongodb')->getCollection('prefix_scout_users');
$collection->drop();
$this->waitForSearchIndexesDropped($collection);

// Recreate the indexes using the artisan commands
// Ensure they return a success exit code (0)
self::assertSame(0, artisan($this, 'scout:delete-index', ['name' => ScoutUser::class]));
self::assertSame(0, artisan($this, 'scout:index', ['name' => ScoutUser::class]));
self::assertSame(0, artisan($this, 'scout:import', ['model' => ScoutUser::class]));
self::assertSame(0, artisan($this, 'scout:index', ['name' => ScoutUser::class]));

self::assertSame(44, $collection->countDocuments());

$searchIndexes = $collection->listSearchIndexes(['name' => 'scout', 'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
self::assertCount(1, $searchIndexes);
self::assertSame(['mappings' => ['dynamic' => true, 'fields' => ['bool_field' => ['type' => 'boolean']]]], iterator_to_array($searchIndexes)[0]['latestDefinition']);

$this->waitForSearchIndexesReady($collection);

// Wait for all documents to be indexed asynchronously
$i = 100;
$timeout = hrtime()[0] + 30;
while (true) {
$indexedDocuments = $collection->aggregate([
['$search' => ['index' => 'scout', 'exists' => ['path' => 'name']]],
Expand All @@ -122,11 +131,11 @@ public function testItCanCreateTheCollection()
break;
}

if ($i-- === 0) {
self::fail('Documents not indexed');
if (hrtime()[0] > $timeout) {
self::fail('Timed out waiting for documents to be indexed');
}

usleep(100_000);
usleep(1000);
}

self::assertCount(44, $indexedDocuments);
Expand All @@ -135,7 +144,7 @@ public function testItCanCreateTheCollection()
#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearch()
{
// All the search queries use "sort" option to ensure the results are deterministic
// All the search queries use the "sort" option to ensure the results are deterministic
$results = ScoutUser::search('lar')->take(10)->orderBy('id')->get();

self::assertSame([
Expand Down
Loading