Recently I was working on a project where, in order to deal with recursion and governor limits, the previous developer decided to send Platform Events, with the expectation that the very same Salesforce org would catch and handle the event.
This was evidently working in production (at least sometimes), but the test classes -- even though they contained Test.stopTest() -- suffered intermittent failures.
This was out of scope for my activities, so I didn't dig too deep into it (and I can't share any code, sorry), but I am wondering:
Since Platform Event were not designed as an asynchronous solution, is it reliable within a test context to assume that both their being published and handled should be triggered and completed by Test.stopTest()?
The idea, as I understand it, of the publisher/subscriber model is that the published shouldn't have to know or care whether there are any subscribers, whether they catch the message, or what (if anything) they do with those messages. This being the case, is it reasonable to expect or to code as if a Salesforce org will catch all the events it generates?
If there are no subscribers outside the Salesforce org, are there any good use cases where Platform Events should (or must) be used instead of Batchables, Queueables, and/or Future methods?