When running code in a thread ActiveRecord uses a separate database connection, which can't see the results of any queries run by the main thread until they're committed. But Rails doesn't commit transactions in the test environment, making this difficult to test.
The fix for this was in two parts, either of which should be enough to solve the problem on their own.
-
Load the ActiveRecord objects before entering the thread, as the database connection that we'll be using inside the thread may not be able to access the (freshly created) database records yet.
-
Commit the transaction that wraps the creation of the database records before publishing the
:message_created
message (that triggers the thread to run). This ought to mean that code with a separate database connection will be able to load the subscribers anyway.