This test occasionally fails because it assumes that as messages 7, 6 and 5 are sent in individual transactions (SortedQueueTest.java,295-297) they will be delivered immediately to the client and not be subject to sorting by the queue. This assumption is unsafe.
When the test fails (SortedQueueTest.java, 300) messages 7 and 6 have resided momentarily together in the queue and therefore have been sorted. The test sees message 6 when it expects message 7.
The test needs to be changed to avoid assuming that the assertion on line 300 will always see message 7 and could in fact see message 6 or even 5.