* Fail a transaction whose connection was lost until it is discarded A connection error released the connection holding the transaction, so a caller that caught the error and kept using the same MultiExec sent the next commands on a new connection, outside of the transaction. In CAS mode, MULTI and EXEC then ran on a connection that watched nothing and overwrote a change made after WATCH. The lost connection now stays held, and every command fails until discard() or unwatch(). discard() sends nothing to it, since closing the connection already ended the transaction on the server. discard() now always ends the transaction: the strategy closes the connection when DISCARD fails, so that it cannot stay in MULTI, and MultiExec resets its state even when the strategy throws. Before, a failed DISCARD after a queue error reset MultiExec while the strategy still held the lost connection, and every later transaction failed. discard() also unwatches the keys when MULTI was never sent, for example when the connection was lost between WATCH and MULTI. Cluster transactions built with the fluent interface are aborted after a cross-slot error too, instead of committing the remaining commands. The StreamInitException handling is removed: a held connection that is not open is never reconnected, so it cannot be thrown there. Also drops the isAborted() check in unwatch(), the inline comments added in #1756, and merges the changelog into a single Unreleased section. * Merge overlapping transaction tests The tests for a queue error with the fluent interface (discarded, not reusable, unwatch() and watch() while aborted) become one test, and the one for a failed DISCARD is covered by the recovery test, which now also covers a connection lost between WATCH and MULTI. The strategy tests for commands not being retried after WATCH or MULTI fold into the tests for a lost connection, and the connected tests for a connection lost after WATCH share one helper. The persistence data provider is renamed, since it is not only used by cluster tests. * Report protocol errors in transactions on the node holding them With replication, Sentinel or cluster, MultiExec passed the aggregate connection to ProtocolException, which requires a node connection, so an unexpected reply inside MULTI or an EXEC reply with the wrong number of items threw a TypeError. The exception is now built with the node the transaction's commands are routed to, which is also the connection that gets closed. * Remove unreachable branches from the transaction retry callback A CommunicationException always carries a node connection, so the callback only ever disconnected it: the branches removing the node from a replication or handling timeouts never ran. * Align the comment of MultiExecState::ABORTED with the other flags * Only realign the comment of MultiExecState::ABORTED * Shorten the transaction changelog entries * Cover the remaining branches of the changed transaction methods Adds tests for discard() in CAS mode before MULTI, a non-callable argument to execute(), RESP3 replies to EXEC, and server errors from a node or Relay connection in executeBypassingTransaction(). * Retry transaction commands after a failed connect The retry callback only accepted a CommunicationException, so retrying after a StreamInitException failed with a TypeError. * Reset a transaction once Redis replies to EXEC execute() kept the transaction state after EXEC, so a transaction that Redis had already ended still looked open. discard() then sent DISCARD outside of MULTI, and since a failed DISCARD closes the connection, it dropped a healthy connection. Reusing the transaction sent the next command without MULTI, outside of any transaction. The state is now reset once EXEC replies, whether it succeeded, reported a failed command, was aborted by a watched key or was rejected. It is kept when EXEC fails on a lost connection, so that discard() still releases that connection. * Close the connection when Redis rejects EXEC but keeps MULTI open Since MultiExec resets once EXEC replies, discard() no longer sends DISCARD after a rejected EXEC. Redis 6.0.6 and later discard the transaction when they reject EXEC and reply with EXECABORT, but older versions reject it with BUSY, LOADING or MASTERDOWN and keep the connection in MULTI, so later commands on it were queued. The strategy now closes the connection when EXEC fails with any error other than EXECABORT. Relay is excluded, since it reports a transaction aborted by WATCH as an error. * Skip UNWATCH on a transaction connection that was lost unwatch() reconnected to send UNWATCH after the connection holding the WATCH was lost, although closing that connection already dropped the watched keys on the server. With the server still down, discard() of a transaction in CAS mode, or of one that lost its connection between WATCH and MULTI, then failed although there was nothing left to clean up. unwatch() now releases a lost connection without sending anything, as discard() already does. * Keep a rejected command before MULTI from aborting a transaction In CAS mode commands run on their own until multi(), so an error there does not affect the transaction, as in Redis. A fluent cluster transaction still treated a cross-slot error before multi() like a rejected queued command, discarding the transaction and failing every later command until discard(). Only errors after MULTI abort it now. * Cover the Relay, replication and server-down transaction paths No test failed when the Relay exception or the choice of connection in the rejected EXEC handling was removed, or when discard() of a CAS transaction failed while the server stayed down. New tests check that a transaction aborted by WATCH under Relay keeps its connection, that a rejected EXEC with replication closes only the node holding the transaction, and that discard() succeeds while the server is down after the connection was lost. The docblock now says that the connection is closed on any EXEC error but EXECABORT, as the code does. * Send nothing from unwatch() on an aborted transaction Aborting the transaction already discarded it and released the connection holding it, so the UNWATCH that unwatch() sent was a wasted round trip. Restores the isAborted() check from #1755.
About testing Predis
ATTENTION: Do not ever ever run this test suite against instances of Redis running in production environments or containing data you are interested in! If you still want to test this library on a production server without hitting the database, please read ahead about how to disable integration tests.
Predis ships a comprehensive test suite that uses PHPUnit to cover every aspect of the library.
The suite is organized into several unit groups with the PHPUnit @group annotation which makes it
possible to run only selected groups of tests. The main groups are:
- disconnected: generic tests verifying the correct behaviour of the library without requiring an active connection to Redis.
- connected: integration tests that require an active connection to Redis
- commands: tests for the implementation of Redis commands.
- slow: tests that might slow down the execution of the test suite (either connected or disconnected).
- ext-relay: tests that require the Relay extension or assert Relay-specific behavior.
- relay-incompatible: tests requiring connection operations or response abstractions that the Relay backend does not provide.
- relay-resp3: tests whose expected reply types or shapes differ from Relay's RESP3 replies. This group covers specific assertions, rather than every test of a command or Redis module.
A list of all the available groups in the suite can be obtained by running:
$ phpunit --list-groups
Groups of tests can be disabled or enabled via the XML configuration file or the standard command
line test runner. Please note that due to a bug in PHPUnit, older versions ignore the --group
option when the group is excluded in the XML configuration file. More details about this issue are
available on PHPUnit's bug tracker.
Certain groups of tests requiring native extensions, such as ext-relay, are
disabled by default in the configuration file. To enable these groups of tests you should remove
them from the exclusion list in phpunit.xml.
Combining groups for inclusion or exclusion with the command-line runner
$ phpunit --group disconnected --exclude-group commands,slow
Integration tests
The suite performs integration tests against a running instance of Redis (>= 2.4.0) to verify the correct behavior of the implementation of each command and certain abstractions implemented in the library that depend on them. These tests are identified by the connected group.
Integration tests for commands that are not supported by the running instance of Redis are marked as skipped automatically.
If you do not have a Redis instance up and running or available for testing, you can completely disable integration tests by excluding the connected group:
$ phpunit --exclude-group connected
Slow tests
Certain tests can slow down the execution of the suite. These tests can be disabled by excluding the slow group:
$ phpunit --exclude-group slow
Testing Redis commands
We also provide an helper script in the bin directory that can be used to automatically generate a
file with the skeleton of a test case to test a Redis command by specifying the name of the class
in the Predis\Command\Redis namespace (only classes in this namespace are considered valid).
For example to generate a test case for SET (represented by the Predis\Command\Redis\SET class):
$ ./bin/create-command-test --class=SET --realm=string
Each command must have a realm specified by the --realm command line argument using a value that
matches its group, as defined by the Redis documentation. Valid realms are:
stringlistsetsorted_sethashgeostreamhyperloglogkeysscriptingpubsubtransactionclusterserverconnection
When unsure about which value to use for --realm for a specific command, you can just infer it by
searching in commands.json for the
group attribute of the corresponding command as use its value.