sanpwc commented on code in PR #1655:
URL: https://github.com/apache/ignite-3/pull/1655#discussion_r1109405079


##########
modules/table/src/test/java/org/apache/ignite/internal/utils/RebalanceUtilUpdateAssignmentsTest.java:
##########
@@ -0,0 +1,437 @@
+/*
+ * 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.utils;
+
+import static java.util.stream.Collectors.toSet;
+import static 
org.apache.ignite.configuration.annotation.ConfigurationType.DISTRIBUTED;
+import static 
org.apache.ignite.internal.affinity.AffinityUtils.calculateAssignmentForPartition;
+import static org.apache.ignite.internal.util.ByteUtils.toBytes;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.pendingPartAssignmentsKey;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.plannedPartAssignmentsKey;
+import static 
org.apache.ignite.internal.utils.RebalanceUtil.stablePartAssignmentsKey;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.IntStream;
+import org.apache.ignite.internal.affinity.Assignment;
+import org.apache.ignite.internal.configuration.ConfigurationManager;
+import 
org.apache.ignite.internal.configuration.storage.TestConfigurationStorage;
+import 
org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import 
org.apache.ignite.internal.distributionzones.configuration.DistributionZonesConfiguration;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.metastorage.MetaStorageManager;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.metastorage.command.MultiInvokeCommand;
+import org.apache.ignite.internal.metastorage.dsl.Iif;
+import 
org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage;
+import org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener;
+import org.apache.ignite.internal.raft.Command;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.CommandClosure;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import 
org.apache.ignite.internal.table.distributed.replicator.TablePartitionId;
+import org.apache.ignite.internal.testframework.IgniteAbstractTest;
+import org.apache.ignite.internal.util.ByteUtils;
+import org.apache.ignite.lang.IgniteInternalException;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.MessagingService;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+/**
+ * Tests for updating assignment in the meta storage.
+ */
+@ExtendWith({MockitoExtension.class, ConfigurationExtension.class})
+@MockitoSettings(strictness = Strictness.LENIENT)
+public class RebalanceUtilUpdateAssignmentsTest extends IgniteAbstractTest {
+    private static final IgniteLogger LOG = 
Loggers.forClass(RebalanceUtilUpdateAssignmentsTest.class);
+
+    private SimpleInMemoryKeyValueStorage keyValueStorage;
+
+    private ConfigurationManager clusterCfgMgr;
+
+    private ClusterService clusterService;
+
+    private MetaStorageManager metaStorageManager;
+
+    static int partNum = 2;
+    static int replicas = 2;
+
+    static Set<String> nodes1 = IntStream.of(5).mapToObj(i -> "nodes1_" + 
i).collect(toSet());
+    static Set<String> nodes2 = IntStream.of(5).mapToObj(i -> "nodes2_" + 
i).collect(toSet());
+    static Set<String> nodes3 = IntStream.of(5).mapToObj(i -> "nodes3_" + 
i).collect(toSet());
+    static Set<String> nodes4 = IntStream.of(5).mapToObj(i -> "nodes4_" + 
i).collect(toSet());
+
+    static Set<Assignment> assignments1 = 
calculateAssignmentForPartition(nodes1, partNum, replicas);
+    static Set<Assignment> assignments2 = 
calculateAssignmentForPartition(nodes2, partNum, replicas);
+    static Set<Assignment> assignments3 = 
calculateAssignmentForPartition(nodes3, partNum, replicas);
+    static Set<Assignment> assignments4 = 
calculateAssignmentForPartition(nodes4, partNum, replicas);
+
+    @BeforeEach
+    public void setUp() {
+        clusterCfgMgr = new ConfigurationManager(
+                List.of(DistributionZonesConfiguration.KEY),
+                Set.of(),
+                new TestConfigurationStorage(DISTRIBUTED),
+                List.of(),
+                List.of()
+        );
+
+        clusterService = mock(ClusterService.class);
+
+        metaStorageManager = mock(MetaStorageManager.class);
+
+        clusterCfgMgr.start();
+
+        AtomicLong raftIndex = new AtomicLong();
+
+        keyValueStorage = spy(new SimpleInMemoryKeyValueStorage("test"));
+
+        MetaStorageListener metaStorageListener = new 
MetaStorageListener(keyValueStorage);
+
+        RaftGroupService metaStorageService = mock(RaftGroupService.class);
+
+        // Delegate directly to listener.
+        lenient().doAnswer(
+                invocationClose -> {
+                    Command cmd = invocationClose.getArgument(0);
+
+                    long commandIndex = raftIndex.incrementAndGet();
+
+                    CompletableFuture<Serializable> res = new 
CompletableFuture<>();
+
+                    CommandClosure<WriteCommand> clo = new CommandClosure<>() {
+                        /** {@inheritDoc} */
+                        @Override
+                        public long index() {
+                            return commandIndex;
+                        }
+
+                        /** {@inheritDoc} */
+                        @Override
+                        public WriteCommand command() {
+                            return (WriteCommand) cmd;
+                        }
+
+                        /** {@inheritDoc} */
+                        @Override
+                        public void result(@Nullable Serializable r) {
+                            if (r instanceof Throwable) {
+                                res.completeExceptionally((Throwable) r);
+                            } else {
+                                res.complete(r);
+                            }
+                        }
+                    };
+
+                    try {
+                        metaStorageListener.onWrite(List.of(clo).iterator());
+                    } catch (Throwable e) {
+                        res.completeExceptionally(new 
IgniteInternalException(e));
+                    }
+
+                    return res;
+                }
+        ).when(metaStorageService).run(any());
+
+        MetaStorageCommandsFactory commandsFactory = new 
MetaStorageCommandsFactory();
+
+        lenient().doAnswer(invocationClose -> {
+            Iif iif = invocationClose.getArgument(0);
+
+            MultiInvokeCommand multiInvokeCommand = 
commandsFactory.multiInvokeCommand().iif(iif).build();
+
+            return metaStorageService.run(multiInvokeCommand);
+        }).when(metaStorageManager).invoke(any());
+
+        when(clusterService.messagingService()).thenAnswer(invocation -> {
+            MessagingService ret = mock(MessagingService.class);
+
+            return ret;
+        });
+    }
+
+    @Test
+    void test1() {
+        test(
+                nodes1, assignments2,
+                null, null, null,
+                null, assignments1, null
+        );
+    }
+
+    /**
+     * Impossible invariant in real environment.
+     */
+    @Test
+    void test2() {
+        test(
+                nodes1, assignments1,
+                null, null, null,
+                null, null, null
+        );
+    }
+
+    @Test
+    void test3() {
+        test(
+                nodes1, assignments2,
+                null, assignments3, null,
+                null, assignments3, assignments1
+        );
+    }
+
+    @Test
+    void test4() {
+        test(
+                nodes1, assignments1,
+                null, assignments3, null,
+                null, assignments3, assignments1
+        );
+    }
+
+    @Test
+    void test5() {
+        test(
+                nodes1, assignments2,
+                assignments3, null, null,
+                assignments3, assignments1, null
+        );
+    }
+
+    @Test
+    void test6() {
+        test(
+                nodes1, assignments1,
+                assignments3, null, null,
+                assignments3, assignments1, null
+        );
+    }
+
+    @Test
+    void test7() {
+        test(
+                nodes1, assignments2,
+                assignments1, null, null,
+                assignments1, null, null
+        );
+    }
+
+    @Test
+    void test8() {
+        test(
+                nodes1, assignments1,
+                assignments1, null, null,
+                assignments1, null, null
+        );
+    }
+
+    @Test
+    void test9() {
+        test(
+                nodes1, assignments2,
+                assignments2, null, null,
+                assignments2, assignments1, null
+        );
+    }
+
+    @Test
+    void test10() {
+        test(
+                nodes1, assignments2,
+                assignments4, assignments3, null,
+                assignments4, assignments3, assignments1
+        );
+    }
+
+    @Test
+    void test11() {
+        test(
+                nodes1, assignments1,
+                assignments3, assignments2, null,
+                assignments3, assignments2, assignments1
+        );
+    }
+
+    @Test
+    void test12() {
+        test(
+                nodes1, assignments2,
+                assignments1, assignments3, null,
+                assignments1, assignments3, assignments1
+        );
+    }
+
+    @Test
+    void test13() {
+        test(
+                nodes1, assignments2,
+                assignments2, assignments3, null,
+                assignments2, assignments3, assignments1
+        );
+    }
+
+    /**
+     * Nodes for new assignments calculating: nodes1.
+     * The table configuration assignments: assignments1.
+     * Current assignments in the metastorage: stable=assignments1, 
pending=assignments2, planned=null.
+     * Expected assignments in the metastorage after updating: 
stable=assignments1, pending=assignments2, planned=assignments1.
+     */
+    @Test

Review Comment:
   Why we only have the javadoc for test14?



-- 
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]

Reply via email to