nizhikov commented on code in PR #11616: URL: https://github.com/apache/ignite/pull/11616#discussion_r1816590358
########## modules/core/src/test/java/org/apache/ignite/internal/processors/configuration/distributed/DistributedConfigurationDefaultValuesTest.java: ########## @@ -0,0 +1,299 @@ +/* + * 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.processors.configuration.distributed; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.plugin.AbstractTestPluginProvider; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.testframework.ListeningTestLogger; +import org.apache.ignite.testframework.LogListener; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; + +/** + * Test default values configuration for distributed properties. + */ +public class DistributedConfigurationDefaultValuesTest extends GridCommonAbstractTest { + /** */ + private Consumer<DistributedPropertyDispatcher> onReadyToRegister; + + /** */ + private Runnable onReadyToWrite; + + /** */ + Map<String, String> dfltPropVals; + + /** */ + private boolean pds; + + /** */ + private final ListeningTestLogger listeningLog = new ListeningTestLogger(log); + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + stopAllGrids(); + + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + cleanPersistenceDir(); + + super.afterTest(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + cfg.setGridLogger(listeningLog); + + cfg.setConsistentId(igniteInstanceName); + + cfg.setDataStorageConfiguration(new DataStorageConfiguration().setDefaultDataRegionConfiguration( + new DataRegionConfiguration().setPersistenceEnabled(pds) + )); + + cfg.setDistributedPropertiesDefaultValues(dfltPropVals); + + cfg.setPluginProviders(new AbstractTestPluginProvider() { + @Override public String name() { + return "Distributed property register"; + } + + @Override public void start(PluginContext ctx) { + ((IgniteEx)ctx.grid()).context().internalSubscriptionProcessor().getDistributedConfigurationListeners().add( + new DistributedConfigurationLifecycleListener() { + @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) { + if (onReadyToRegister != null) + onReadyToRegister.accept(dispatcher); + } + + @Override public void onReadyToWrite() { + if (onReadyToWrite != null) + onReadyToWrite.run(); + } + } + ); + } + }); + + return cfg; + } + + /** */ + @Test + public void testDifferentPropertyTypes() throws Exception { + DistributedLongProperty longProp = DistributedLongProperty.detachedLongProperty("longProp", ""); + DistributedBooleanProperty boolProp = DistributedBooleanProperty.detachedBooleanProperty("boolProp", ""); + SimpleDistributedProperty<String> strProp = new SimpleDistributedProperty<>("stringProp", Function.identity(), ""); + DistributedEnumProperty<ClusterState> enumProp = new DistributedEnumProperty<>( + "enumProp", "", + ordinal -> ordinal == null ? null : ClusterState.fromOrdinal(ordinal), + state -> state == null ? null : state.ordinal(), + ClusterState.class + ); + + dfltPropVals = Map.of( + "longProp", "1", + "boolProp", "true", + "stringProp", "val", + "enumProp", "ACTIVE" + ); + + onReadyToRegister = dispatcher -> dispatcher.registerProperties(longProp, boolProp, strProp, enumProp); + + Map<String, String> lsnrProps = new HashMap<>(); + + DistributePropertyListener<Object> lsnr = (name, oldVal, newVal) -> { + if (newVal != null) + lsnrProps.put(name, newVal.toString()); + }; + + longProp.addListener(lsnr); + boolProp.addListener(lsnr); + strProp.addListener(lsnr); + enumProp.addListener(lsnr); + + // Properties values when distributed configuration is ready to write. + Map<String, String> lsnrProps0 = new HashMap<>(); + + onReadyToWrite = () -> lsnrProps0.putAll(lsnrProps); + + startGrid(0); + + assertEquals(dfltPropVals, lsnrProps0); + + assertEquals((Long)1L, longProp.get()); + assertEquals(Boolean.TRUE, boolProp.get()); + assertEquals("val", strProp.get()); + assertEquals(ClusterState.ACTIVE, enumProp.get()); + } + + /** */ + @Test + public void testInMemoryCluster() throws Exception { + checkDistributedPropertiesClusterPropagation(false); + } + + /** */ + @Test + public void testPersistentCluster() throws Exception { + checkDistributedPropertiesClusterPropagation(true); + } + + /** */ + private void checkDistributedPropertiesClusterPropagation(boolean pds) throws Exception { + this.pds = pds; + + String propName = "testProp"; + + // Properties with the same name, but on different nodes. + DistributedLongProperty prop0 = DistributedLongProperty.detachedLongProperty(propName, ""); + DistributedLongProperty prop1 = DistributedLongProperty.detachedLongProperty(propName, ""); + DistributedLongProperty prop2 = DistributedLongProperty.detachedLongProperty(propName, ""); + + dfltPropVals = F.asMap(propName, "1"); + + onReadyToRegister = dispatcher -> dispatcher.registerProperties(prop0); + + startGrid(0).cluster().state(ClusterState.ACTIVE); + + // Value from default configuration. + assertEquals((Long)1L, prop0.get()); Review Comment: Let's move comment to the assert call here and below: ``` assertEquals("Expecting value from configuration", 1L, prop0.get()); ``` -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org