sashapolo commented on code in PR #1807: URL: https://github.com/apache/ignite-3/pull/1807#discussion_r1146119163
########## modules/core/src/test/java/org/apache/ignite/internal/causality/IncrementalVersionedValueTest.java: ########## @@ -0,0 +1,442 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.causality; + +import static java.util.concurrent.CompletableFuture.completedFuture; +import static java.util.concurrent.CompletableFuture.failedFuture; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.assertThrowsWithCause; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.runRace; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import org.apache.ignite.lang.IgniteInternalException; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link IncrementalVersionedValue}. + */ +public class IncrementalVersionedValueTest { + /** Test value. */ + private static final int TEST_VALUE = 1; + + /** The test revision register is used to move the revision forward. */ + private final TestRevisionRegister register = new TestRevisionRegister(); + + /** Test exception is used for exceptionally completion Versioned value object. */ + private static final Exception TEST_EXCEPTION = new Exception("Test exception"); + + /** + * Checks that the update method work as expected when the previous value is known. + * + * @throws Exception If failed. + */ + @Test + public void testUpdate() throws Exception { + var versionedValue = new IncrementalVersionedValue<Integer>(register); + + versionedValue.update(0, (integer, throwable) -> completedFuture(TEST_VALUE)); + + register.moveRevision(0L).join(); + + CompletableFuture<Integer> fut = versionedValue.get(1); + + assertFalse(fut.isDone()); + + int incrementCount = 10; + + for (int i = 0; i < incrementCount; i++) { + versionedValue.update(1, (previous, e) -> completedFuture(++previous)); + + assertFalse(fut.isDone()); + } + + register.moveRevision(1L).join(); + + assertTrue(fut.isDone()); + + assertEquals(TEST_VALUE + incrementCount, fut.get()); + + assertThrows(AssertionError.class, () -> versionedValue.update(1L, (i, t) -> completedFuture(null))); + } + + /** + * Checks that the update method work as expected when there is no history to calculate previous value. + * + * @throws Exception If failed. + */ + @Test + public void testUpdatePredefined() throws Exception { + var versionedValue = new IncrementalVersionedValue<Integer>(register); + + CompletableFuture<Integer> fut = versionedValue.get(0); + + assertFalse(fut.isDone()); + + versionedValue.update(0, (previous, e) -> { + assertNull(previous); + + return completedFuture(TEST_VALUE); + }); + + assertFalse(fut.isDone()); + + register.moveRevision(0L).join(); + + assertTrue(fut.isDone()); + + assertEquals(TEST_VALUE, fut.get()); + } + + /** + * Test asynchronous update closure. + */ + @Test + public void testAsyncUpdate() { + IncrementalVersionedValue<Integer> vv = new IncrementalVersionedValue<>(register); + + CompletableFuture<Integer> fut = new CompletableFuture<>(); + + vv.update(0L, (v, e) -> fut); + + CompletableFuture<Integer> vvFut = vv.get(0L); + + CompletableFuture<?> revFut = register.moveRevision(0L); + + assertFalse(fut.isDone()); + assertFalse(vvFut.isDone()); + assertFalse(revFut.isDone()); + + fut.complete(1); + + revFut.join(); + + assertTrue(vvFut.isDone()); + } + + /** + * Test the case when exception happens in updater. + */ + @Test + public void testExceptionOnUpdate() { + IncrementalVersionedValue<Integer> vv = new IncrementalVersionedValue<>(register, () -> 0); + + final int count = 4; + final int successfulCompletionsCount = count / 2; + + AtomicInteger actualSuccessfulCompletionsCount = new AtomicInteger(); + + final String exceptionMsg = "test msg"; + + for (int i = 0; i < count; i++) { + vv.update(0L, (v, e) -> { + if (e != null) { + return failedFuture(e); + } + + if (v == successfulCompletionsCount) { + throw new IgniteInternalException(exceptionMsg); + } + + actualSuccessfulCompletionsCount.incrementAndGet(); + + return completedFuture(++v); + }); + } + + AtomicReference<Throwable> exceptionRef = new AtomicReference<>(); + + vv.whenComplete((t, v, e) -> exceptionRef.set(e)); + + vv.complete(0L); + + assertThrowsWithCause(() -> vv.get(0L).join(), IgniteInternalException.class); + + assertThat(exceptionRef.get().getMessage(), containsString(exceptionMsg)); + } + + /** + * Test with multiple versioned values and asynchronous completion. + */ + @Test + public void testAsyncMultiVv() { + final String registryName = "Registry"; + final String assignmentName = "Assignment"; + final String tableName = "T1_"; + + IncrementalVersionedValue<Map<UUID, String>> tablesVv = new IncrementalVersionedValue<>(f -> { + }, HashMap::new); + IncrementalVersionedValue<Map<UUID, String>> schemasVv = new IncrementalVersionedValue<>(register, HashMap::new); + IncrementalVersionedValue<Map<UUID, String>> assignmentsVv = new IncrementalVersionedValue<>(register, HashMap::new); + + schemasVv.whenComplete((token, value, ex) -> tablesVv.complete(token)); + + BiFunction<Long, UUID, CompletableFuture<String>> schemaRegistry = + (token, uuid) -> schemasVv.get(token).thenApply(schemas -> schemas.get(uuid)); + + // Adding table. + long token = 0L; + UUID tableId = UUID.randomUUID(); + + CompletableFuture<String> tableFut = schemaRegistry.apply(token, tableId) + .thenCombine(assignmentsVv.get(token), (registry, assignments) -> tableName + registry + assignments.get(tableId)); + + tablesVv.update(token, (old, e) -> tableFut.thenApply(table -> { + Map<UUID, String> val = new HashMap<>(old); + + val.put(tableId, table); + + return val; + })); + + CompletableFuture<String> userFut = tablesVv.get(token).thenApply(map -> map.get(tableId)); + + schemasVv.update(token, (old, e) -> { + old.put(tableId, registryName); + + return completedFuture(old); + }); + + assignmentsVv.update(token, (old, e) -> { + old.put(tableId, assignmentName); + + return completedFuture(old); + }); + + assertFalse(tableFut.isDone()); + assertFalse(userFut.isDone()); + + register.moveRevision(token).join(); + + tableFut.join(); + + assertEquals(tableName + registryName + assignmentName, userFut.join()); + } + + /** + * Tests a default value supplier. + */ + @Test + public void testDefaultValueSupplier() { + IncrementalVersionedValue<Integer> vv = new IncrementalVersionedValue<>(register, () -> TEST_VALUE); + + checkDefaultValue(vv, TEST_VALUE); + } + + /** + * Tests a case when there is no default value supplier. + */ + @Test + public void testWithoutDefaultValue() { + IncrementalVersionedValue<Integer> vv = new IncrementalVersionedValue<>(register); + + checkDefaultValue(vv, null); + } + + @RepeatedTest(100) + void testConcurrentGetAndComplete() { + var versionedValue = new IncrementalVersionedValue<>(register, () -> 1); + + // Set initial value. + versionedValue.complete(1); + + runRace( + () -> versionedValue.complete(3), + () -> { + CompletableFuture<Integer> readerFuture = versionedValue.get(2); + + assertThat(readerFuture, willBe(1)); + } + ); + } + + @RepeatedTest(100) + void testConcurrentGetAndCompleteWithHistoryTrimming() { + var versionedValue = new IncrementalVersionedValue<>(register, () -> 1); + + // Set initial value (history size 1). + versionedValue.complete(2); + // Set history size to 2. + versionedValue.complete(3); + + runRace( + () -> { + versionedValue.update(4, (i, t) -> completedFuture(i + 1)); + // Trigger history trimming + versionedValue.complete(4); + }, + () -> { + try { + CompletableFuture<Integer> readerFuture = versionedValue.get(2); + + assertThat(readerFuture, willBe(1)); + } catch (OutdatedTokenException ignored) { + // This is considered as a valid outcome. + } + } + ); + + assertThat(versionedValue.get(4), willBe(2)); Review Comment: Nice catch, thank you! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
