• Jeremy Mikola's avatar
    Test that ChangeStream::next() resumes after dropped connection · 3a4e9202
    Jeremy Mikola authored
    The original intention of this test was to verify resumability in the event of a dropped connection; however, since socketTimeoutMS and maxAwaitTimeMS are fixed values, we cannot test a dropped connection followed by a successful resume. Instead, we will test that the driver attempts to resume once and only once before we expect a socket timeout after the second attempt.
    
    Note: ChangeStream::rewind() does not currently resume (see: PHPLIB-322)
    3a4e9202
CommandObserver.php 1.21 KB
<?php

namespace MongoDB\Tests;

use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use Exception;

/**
 * Observes command documents using the driver's monitoring API.
 */
class CommandObserver implements CommandSubscriber
{
    private $commands = [];

    public function observe(callable $execution, callable $commandCallback)
    {
        $this->commands = [];

        \MongoDB\Driver\Monitoring\addSubscriber($this);

        try {
            call_user_func($execution);
        } catch (Exception $executionException) {}

        \MongoDB\Driver\Monitoring\removeSubscriber($this);

        foreach ($this->commands as $command) {
            call_user_func($commandCallback, $command);
        }

        if (isset($executionException)) {
            throw $executionException;
        }
    }

    public function commandStarted(CommandStartedEvent $event)
    {
        $this->commands[] = $event->getCommand();
    }

    public function commandSucceeded(CommandSucceededEvent $event)
    {
    }

    public function commandFailed(CommandFailedEvent $event)
    {
    }
}