use of org.junit.rules.Timeout in project pravega by pravega.
the class StreamSegmentMapperTests method testCreateTransactionAlreadyExists.
/**
* Tests the ability of the StreamSegmentMapper to create a new Transaction if the Transaction already exists (partially
* or fully).
*/
@Test
public void testCreateTransactionAlreadyExists() {
final String parentSegmentName = "NewSegment";
final UUID txnId = UUID.randomUUID();
final String txnName = StreamSegmentNameUtils.getTransactionNameFromId(parentSegmentName, txnId);
testCreateAlreadyExists(txnName, (mapper, attributes) -> mapper.createNewStreamSegment(parentSegmentName, null, TIMEOUT).exceptionally(ex -> {
if (Exceptions.unwrap(ex) instanceof StreamSegmentExistsException) {
return null;
}
throw new CompletionException(ex);
}).thenCompose(v -> mapper.createNewTransactionStreamSegment(parentSegmentName, txnId, attributes, TIMEOUT)));
}
use of org.junit.rules.Timeout in project pravega by pravega.
the class StreamSegmentMapperTests method testCreateAlreadyExists.
/**
* General test for verifying behavior when a Segment/Transaction is attempted to be created but it already exists.
*
* @param segmentName The name of the segment/transaction to create.
* @param createSegment A BiFunction that is given an instance of a StreamSegmentMapper and a Collection of AttributeUpdates
* that, when invoked, will create the given segment.
*/
private void testCreateAlreadyExists(String segmentName, BiFunction<StreamSegmentMapper, Collection<AttributeUpdate>, CompletableFuture<?>> createSegment) {
final String stateSegmentName = StreamSegmentNameUtils.getStateSegmentName(segmentName);
final Map<UUID, Long> correctAttributes = Collections.singletonMap(UUID.randomUUID(), 123L);
final Collection<AttributeUpdate> correctAttributeUpdates = correctAttributes.entrySet().stream().map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())).collect(Collectors.toList());
final Map<UUID, Long> badAttributes = Collections.singletonMap(UUID.randomUUID(), 456L);
final Collection<AttributeUpdate> badAttributeUpdates = badAttributes.entrySet().stream().map(e -> new AttributeUpdate(e.getKey(), AttributeUpdateType.Replace, e.getValue())).collect(Collectors.toList());
@Cleanup TestContext context = new TestContext();
@Cleanup val storage = InMemoryStorageFactory.newStorage(executorService());
storage.initialize(1);
val store = new SegmentStateStore(storage, executorService());
val mapper = new StreamSegmentMapper(context.metadata, context.operationLog, store, context.noOpMetadataCleanup, storage, executorService());
// 1. Segment Exists, and so does State File (and it's not corrupted) -> Exception must be bubbled up.
createSegment.apply(mapper, correctAttributeUpdates).join();
AssertExtensions.assertThrows("createNewStreamSegment did not fail when Segment already exists.", () -> createSegment.apply(mapper, badAttributeUpdates), ex -> ex instanceof StreamSegmentExistsException);
val state1 = store.get(segmentName, TIMEOUT).join();
AssertExtensions.assertMapEquals("Unexpected attributes after failed attempt to recreate correctly created segment", correctAttributes, state1.getAttributes());
// 2. Segment Exists, but with empty State File: State file re-created & no exception bubbled up.
storage.openWrite(stateSegmentName).thenCompose(handle -> storage.delete(handle, TIMEOUT)).thenCompose(v -> storage.create(stateSegmentName, TIMEOUT)).join();
Assert.assertNull("Expected a null SegmentState.", store.get(segmentName, TIMEOUT).join());
createSegment.apply(mapper, correctAttributeUpdates).join();
val state2 = store.get(segmentName, TIMEOUT).join();
AssertExtensions.assertMapEquals("Unexpected attributes after successful attempt to complete segment creation (missing state file)", correctAttributes, state2.getAttributes());
// 3. Segment Exists, but with corrupted State File: State file re-created & no exception bubbled up.
storage.openWrite(stateSegmentName).thenCompose(handle -> storage.delete(handle, TIMEOUT)).thenCompose(v -> storage.create(stateSegmentName, TIMEOUT)).thenCompose(v -> storage.openWrite(stateSegmentName)).thenCompose(handle -> storage.write(handle, 0, new ByteArrayInputStream(new byte[1]), 1, TIMEOUT)).join();
AssertExtensions.assertThrows("Expected a DataCorruptionException when reading a corrupted State File.", () -> store.get(segmentName, TIMEOUT), ex -> ex instanceof DataCorruptionException);
createSegment.apply(mapper, correctAttributeUpdates).join();
val state3 = store.get(segmentName, TIMEOUT).join();
AssertExtensions.assertMapEquals("Unexpected attributes after successful attempt to complete segment creation (corrupted state file)", correctAttributes, state3.getAttributes());
// 4. Segment Exists with non-zero length, but with empty/corrupted State File: State File re-created and exception thrown.
storage.openWrite(stateSegmentName).thenCompose(handle -> storage.delete(handle, TIMEOUT)).thenCompose(v -> storage.create(stateSegmentName, TIMEOUT)).thenCompose(v -> storage.openWrite(segmentName)).thenCompose(handle -> storage.write(handle, 0, new ByteArrayInputStream(new byte[1]), 1, TIMEOUT)).join();
AssertExtensions.assertThrows("createNewStreamSegment did not fail when Segment already exists (non-zero length, missing state file).", () -> createSegment.apply(mapper, correctAttributeUpdates), ex -> ex instanceof StreamSegmentExistsException);
val state4 = store.get(segmentName, TIMEOUT).join();
AssertExtensions.assertMapEquals("Unexpected attributes after failed attempt to recreate segment with non-zero length", correctAttributes, state4.getAttributes());
}
use of org.junit.rules.Timeout in project pravega by pravega.
the class SegmentStateMapperTests method testGetSegmentInfoFromStorage.
/**
* Tests the ability to retrieve Segment Info from Storage (base info and its State).
*/
@Test
public void testGetSegmentInfoFromStorage() {
final UUID attributeId = UUID.randomUUID();
@Cleanup val s = createStorage();
val stateStore = new InMemoryStateStore();
val m = new SegmentStateMapper(stateStore, s);
// Save some state.
val info = s.create("s", TIMEOUT).thenCompose(si -> s.openWrite(si.getName())).thenCompose(handle -> s.write(handle, 0, new ByteArrayInputStream(new byte[10]), 10, TIMEOUT).thenCompose(v -> s.seal(handle, TIMEOUT)).thenCompose(v -> s.getStreamSegmentInfo(handle.getSegmentName(), TIMEOUT))).join();
val expectedInfo = StreamSegmentInformation.from(info).startOffset(info.getLength() / 2).build();
val attributes = Collections.singleton(new AttributeUpdate(attributeId, AttributeUpdateType.Replace, 100L));
val expectedAttributes = Collections.singletonMap(attributeId, 100L);
m.saveState(expectedInfo, attributes, TIMEOUT).join();
// Retrieve the state and verify it.
val actual = m.getSegmentInfoFromStorage(expectedInfo.getName(), TIMEOUT).join();
Assert.assertEquals("Unexpected Name.", expectedInfo.getName(), actual.getName());
Assert.assertEquals("Unexpected Length.", expectedInfo.getLength(), actual.getLength());
Assert.assertEquals("Unexpected Sealed status.", expectedInfo.isSealed(), actual.isSealed());
Assert.assertEquals("Unexpected Start Offset.", expectedInfo.getStartOffset(), actual.getStartOffset());
AssertExtensions.assertMapEquals("Unexpected Attributes.", expectedAttributes, actual.getAttributes());
}
use of org.junit.rules.Timeout in project pravega by pravega.
the class SequentialAsyncProcessorTests method testRunAsyncErrors.
/**
* Tests the runAsync() method with execution errors.
*/
@Test(timeout = TIMEOUT_MILLIS)
public void testRunAsyncErrors() throws Exception {
final int expectedCount = 2;
val count = new AtomicInteger();
val finished = new CompletableFuture<Void>();
val retry = Retry.withExpBackoff(1, 2, expectedCount).retryWhen(t -> {
if (count.get() >= expectedCount) {
finished.complete(null);
}
return Exceptions.unwrap(t) instanceof IntentionalException;
}).throwingOn(Exception.class);
val error = new CompletableFuture<Throwable>();
val p = new SequentialAsyncProcessor(() -> {
count.incrementAndGet();
throw new IntentionalException();
}, retry, error::complete, executorService());
// Invoke it once.
p.runAsync();
finished.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
val finalException = Exceptions.unwrap(error.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
Assert.assertEquals("Unexpected number of final invocations.", expectedCount, count.get());
Assert.assertTrue("Unexpected final error callback.", finalException instanceof RetriesExhaustedException && Exceptions.unwrap(finalException.getCause()) instanceof IntentionalException);
}
use of org.junit.rules.Timeout in project rxrabbit by meltwater.
the class RxRabbitTests method if_not_using_publish_confirms_messages_can_be_lost.
@Test
public void if_not_using_publish_confirms_messages_can_be_lost() throws IOException {
ChannelFactory proxyChannelFactory = getDroppingAndExceptionThrowingChannelFactory(1, 2);
final PublisherSettings proxyPublishSettings = new PublisherSettings().withNumChannels(1).withPublisherConfirms(false).withRetryCount(5);
DefaultPublisherFactory proxyPublishFactory = new DefaultPublisherFactory(proxyChannelFactory, proxyPublishSettings);
RabbitPublisher publisher = proxyPublishFactory.createPublisher();
final List<PublishedMessage> res = sendNMessagesAsync(3, 0, publisher).take(3).timeout(30, TimeUnit.SECONDS).toList().toBlocking().last();
for (PublishedMessage re : res) {
assertFalse(re.failed);
}
final List<Message> consumeRes = consumerFactory.createConsumer(inputQueue).doOnNext(m -> m.acknowledger.ack()).take(2).timeout(10, TimeUnit.SECONDS).toList().toBlocking().last();
assertThat(consumeRes.size(), is(2));
final List<Message> timeoutConsume = consumerFactory.createConsumer(inputQueue).doOnNext(m -> m.acknowledger.ack()).take(1).timeout(3, TimeUnit.SECONDS).onErrorResumeNext(Observable.<Message>empty()).toList().toBlocking().last();
assertTrue(timeoutConsume.isEmpty());
publisher.close();
}
Aggregations