| /* |
| * 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.managers.communication; |
| |
| import java.nio.ByteBuffer; |
| import java.util.concurrent.Callable; |
| import org.apache.ignite.IgniteCheckedException; |
| import org.apache.ignite.configuration.IgniteConfiguration; |
| import org.apache.ignite.plugin.AbstractTestPluginProvider; |
| import org.apache.ignite.plugin.ExtensionRegistry; |
| import org.apache.ignite.plugin.PluginContext; |
| import org.apache.ignite.plugin.extensions.communication.IgniteMessageFactory; |
| import org.apache.ignite.plugin.extensions.communication.Message; |
| import org.apache.ignite.plugin.extensions.communication.MessageFactory; |
| import org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider; |
| import org.apache.ignite.plugin.extensions.communication.MessageReader; |
| import org.apache.ignite.plugin.extensions.communication.MessageWriter; |
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| import org.junit.Test; |
| |
| import static org.apache.ignite.testframework.GridTestUtils.assertThrows; |
| |
| /** |
| * Tests that node will not start if some component tries to register message factory with direct type |
| * for which message factory is already registered. |
| */ |
| public class MessageDirectTypeIdConflictTest extends GridCommonAbstractTest { |
| /** Message direct type. Message with this direct type will be registered by {@link GridIoMessageFactory} first. */ |
| private static final short MSG_DIRECT_TYPE = -44; |
| |
| /** {@inheritDoc} */ |
| @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); |
| |
| cfg.setPluginProviders(new TestPluginProvider()); |
| |
| return cfg; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override protected void beforeTest() throws Exception { |
| super.beforeTest(); |
| |
| stopAllGrids(); |
| } |
| |
| /** {@inheritDoc} */ |
| @Override protected void afterTest() throws Exception { |
| super.afterTest(); |
| |
| stopAllGrids(); |
| } |
| |
| /** |
| * Tests that node will not start if some component tries to register message factory with direct type |
| * for which message factory is already registered. |
| */ |
| @Test |
| @SuppressWarnings({"RedundantThrows", "ThrowableNotThrown"}) |
| public void testRegisterMessageFactoryWithConflictDirectTypeId() throws Exception { |
| assertThrows(log, (Callable<Object>)this::startGrid, IgniteCheckedException.class, |
| "Message factory is already registered for direct type: " + MSG_DIRECT_TYPE); |
| } |
| |
| /** */ |
| public static class TestPluginProvider extends AbstractTestPluginProvider { |
| /** {@inheritDoc} */ |
| @Override public String name() { |
| return "TEST_PLUGIN"; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public void initExtensions(PluginContext ctx, ExtensionRegistry registry) { |
| registry.registerExtension(MessageFactory.class, new MessageFactoryProvider() { |
| @Override public void registerAll(IgniteMessageFactory factory) { |
| factory.register(MSG_DIRECT_TYPE, TestMessage::new); |
| } |
| }); |
| } |
| } |
| |
| /** Test message with already registered direct type. */ |
| private static class TestMessage implements Message { |
| /** {@inheritDoc} */ |
| @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) { |
| return false; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) { |
| return false; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public short directType() { |
| return MSG_DIRECT_TYPE; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public byte fieldsCount() { |
| return 0; |
| } |
| |
| /** {@inheritDoc} */ |
| @Override public void onAckReceived() { |
| // No-op. |
| } |
| } |
| } |