/** * @group disconnected * @expectedException Predis\NotSupportedException * @expectedExceptionMessage The current profile does not support WATCH and UNWATCH */ public function testThrowsExceptionOnUnsupportedWatchUnwatchInProfile() { $connection = $this->getMock('Predis\\Connection\\SingleConnectionInterface'); $client = new Client($connection, array('profile' => '2.0')); $tx = new MultiExecContext($client, array('options' => 'cas')); $tx->watch('foo'); }
/** * @group disconnected */ public function testProperlyDiscardsTransactionAfterServerExceptionInBlock() { $connection = $this->getMockedConnection(function (CommandInterface $command) { switch ($command->getId()) { case 'MULTI': return true; case 'ECHO': return new ResponseError('ERR simulated failure on ECHO'); case 'EXEC': return new ResponseError('EXECABORT Transaction discarded because of previous errors.'); default: return new ResponseQueued(); } }); $client = new Client($connection); // First attempt $tx = new MultiExecContext($client); try { $tx->multi()->set('foo', 'bar')->echo('simulated failure')->exec(); } catch (\Exception $exception) { $this->assertInstanceOf('Predis\\Transaction\\AbortedMultiExecException', $exception); $this->assertSame('ERR simulated failure on ECHO', $exception->getMessage()); } // Second attempt $tx = new MultiExecContext($client); try { $tx->multi()->set('foo', 'bar')->echo('simulated failure')->exec(); } catch (\Exception $exception) { $this->assertInstanceOf('Predis\\Transaction\\AbortedMultiExecException', $exception); $this->assertSame('ERR simulated failure on ECHO', $exception->getMessage()); } }
/** * Transaction context initializer. * * @param array $options Options for the context. * @param mixed $callable Optional callable object used to execute the context. * @return MultiExecContext|array */ protected function initMultiExec(array $options = null, $callable = null) { $transaction = new MultiExecContext($this, $options ?: array()); return isset($callable) ? $transaction->execute($callable) : $transaction; }
/** * Adds the commands needed for the count function * * @param MultiExecContext $multi A MultiExecContext instance * @param string $subject A unique identifier, for example a session id or an IP * @param int $bucket Bucket * @param int $count Count */ private function addMultiExecCount(MultiExecContext $multi, $subject, $bucket, $count) { // Get the counts from the previous `$count` buckets $multi->hget($subject, $bucket); while ($count--) { $multi->hget($subject, (--$bucket + $this->bucketCount) % $this->bucketCount); } }