### Question:
SequenceNumberCache { public void reserveSequenceNumbers(List<SerializedEvent> events) { reserveSequenceNumbers(events, false); } SequenceNumberCache(
            BiFunction<String, EventStorageEngine.SearchHint[], Optional<Long>> aggregateSequenceNumberProvider);  SequenceNumberCache(
            BiFunction<String, EventStorageEngine.SearchHint[], Optional<Long>> aggregateSequenceNumberProvider,
            Clock clock, int maxSize); void reserveSequenceNumbers(List<SerializedEvent> events); Runnable reserveSequenceNumbers(List<SerializedEvent> events, boolean force); void clear(); void clearOld(long timeout); void close();  }### Answer:
@Test public void testParallelTransactions() { testSubject = new SequenceNumberCache(SequenceNumberCacheTest::slowSequenceNumberProvider, clock, 1); boolean[] success = new boolean[2]; IntStream.range(0, 2).parallel().forEach(i -> { try { testSubject.reserveSequenceNumbers(asList( serializedEvent("NEW", "SampleAgg", 0))); success[i] = true; } catch (MessagingPlatformException ex) { Assert.assertEquals(ErrorCode.INVALID_SEQUENCE, ex.getErrorCode()); success[i] = false; } }); assertTrue("Expect one success", success[0] || success[1]); assertFalse("Expect one failure", success[0] && success[1]); testSubject.reserveSequenceNumbers(asList( serializedEvent("NEW", "SampleAgg", 1))); }
@Test public void reserveSequenceNumbers() { testSubject.reserveSequenceNumbers(asList( serializedEvent("NEW", "SampleAgg", 0))); }
@Test public void reserveSequenceNumbersInvalidSequence() { try { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 0))); fail("Should fail, as sequence number should be 11"); } catch (MessagingPlatformException mpe) { assertEquals(ErrorCode.INVALID_SEQUENCE, mpe.getErrorCode()); } }
@Test public void reserveSequenceNumbersValidAfterInvalidSequence() { try { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 12))); fail("Should fail, as sequence number should be 11"); } catch (MessagingPlatformException mpe) { assertEquals(ErrorCode.INVALID_SEQUENCE, mpe.getErrorCode()); } testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11))); }
@Test public void reserveSequenceNumbersValidSequence() { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11))); }
@Test public void reserveSequenceNumbersValidSequenceMultipleEvents() { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11), serializedEvent("OTHER", "SampleAgg", 12) )); }
@Test public void reserveSequenceNumbersValidSequenceMultipleEvents2() { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11) )); testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 12) )); }
@Test public void reserveSequenceNumbersInvalidDoubleSequence() { try { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11), serializedEvent("OTHER", "SampleAgg", 11) )); fail("Should fail, as second sequence number should be 12"); } catch (MessagingPlatformException mpe) { assertEquals(ErrorCode.INVALID_SEQUENCE, mpe.getErrorCode()); } }
@Test public void reserveSequenceNumbersInvalidGapSequence() { try { testSubject.reserveSequenceNumbers(asList( serializedEvent("OTHER", "SampleAgg", 11), serializedEvent("OTHER", "SampleAgg", 13) )); fail("Should fail, as second sequence number should be 12"); } catch (MessagingPlatformException mpe) { assertEquals(ErrorCode.INVALID_SEQUENCE, mpe.getErrorCode()); } }