diff --git a/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java b/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java new file mode 100644 index 000000000..fba77e26a --- /dev/null +++ b/naming/src/test/java/com/alibaba/nacos/naming/remote/udp/UdpConnectorTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 1999-2021 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.naming.remote.udp; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.remote.PushCallBack; +import com.alibaba.nacos.sys.env.EnvUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.mock.env.MockEnvironment; +import org.springframework.test.util.ReflectionTestUtils; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.util.concurrent.ConcurrentMap; + +/** + * {@link UdpConnector} unit tests. + * + * @author chenglu + * @date 2021-09-15 19:52 + */ +@RunWith(MockitoJUnitRunner.class) +public class UdpConnectorTest { + + @InjectMocks + private UdpConnector udpConnector; + + @Mock + private ConcurrentMap ackMap; + + @Mock + private ConcurrentMap callbackMap; + + @Mock + private DatagramSocket udpSocket; + + @BeforeClass + public static void setEnv() { + EnvUtil.setEnvironment(new MockEnvironment()); + } + + @Before + public void setUp() { + ReflectionTestUtils.setField(udpConnector, "ackMap", ackMap); + ReflectionTestUtils.setField(udpConnector, "callbackMap", callbackMap); + ReflectionTestUtils.setField(udpConnector, "udpSocket", udpSocket); + } + + @Test + public void testContainAck() { + Mockito.when(ackMap.containsKey(Mockito.anyString())).thenReturn(true); + Assert.assertTrue(udpConnector.containAck("1111")); + } + + @Test + public void testSendData() throws NacosException, IOException { + Mockito.when(udpSocket.isClosed()).thenReturn(false); + AckEntry ackEntry = new AckEntry("A", new DatagramPacket(new byte[2], 2)); + udpConnector.sendData(ackEntry); + Mockito.verify(udpSocket).send(ackEntry.getOrigin()); + } + + @Test + public void testSendDataWithCallback() throws IOException, InterruptedException { + Mockito.when(udpSocket.isClosed()).thenReturn(false); + AckEntry ackEntry = new AckEntry("A", new DatagramPacket(new byte[2], 2)); + udpConnector.sendDataWithCallback(ackEntry, new PushCallBack() { + @Override + public long getTimeout() { + return 0; + } + + @Override + public void onSuccess() { + + } + + @Override + public void onFail(Throwable e) { + Assert.fail(e.getMessage()); + } + }); + Thread.sleep(100); + Mockito.verify(udpSocket).send(ackEntry.getOrigin()); + } +}