Repeated failure in foreman-develop-source-release tests

Hello,

There have been multiple failures in the api realms controller test recently in the foreman-develop-source-release job:

--- expected
+++ actual
@@ -1 +1 @@
-#<ActiveRecord::Relation [#<Realm id: 936160439, name: "yourrealm.net", realm_type: "FreeIPA", realm_proxy_id: 982965213, created_at: "2019-06-26 09:24:22", updated_at: "2019-06-26 09:24:22">, #<Realm id: 664044182, name: "myrealm.net", realm_type: "FreeIPA", realm_proxy_id: 982965213, created_at: "2019-06-26 09:24:22", updated_at: "2019-06-26 09:24:22">]>
+#<ActiveRecord::Associations::CollectionProxy [#<Realm id: 664044182, name: "myrealm.net", realm_type: "FreeIPA", realm_proxy_id: 982965213, created_at: "2019-06-26 09:24:22", updated_at: "2019-06-26 09:24:22">, #<Realm id: 936160439, name: "yourrealm.net", realm_type: "FreeIPA", realm_proxy_id: 982965213, created_at: "2019-06-26 09:24:22", updated_at: "2019-06-26 09:24:22">]>
 (Minitest::Assertion)

It does not consistently fail but rather about once in every 4-5 runs, which is still common enough that the 5 of the last 6 runs of the job did not pass due to this.
I tried looking at the recently merged changes but nothing seems related - in fact, as far as I can tell, no changes were made to any of the realms-related files in months.
If anyone has an idea or wants to take a look, the issue is tracked at Bug #27171: Realms controller transient failures - Foreman

I suspect this may be a side-effect of https://github.com/theforeman/foreman/commit/ae1f9130eb54677a34f5c9574e4931dac2bde286 but I’m not sure why. @shiramax any idea?