This is an automated email from the ASF dual-hosted git repository.
liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 0933a2c450 feat: add diruptor unit test (#6079)
0933a2c450 is described below
commit 0933a2c450b4ad3c5d17284417ac026d8f0d6c27
Author: shown <[email protected]>
AuthorDate: Sun Aug 3 10:45:17 2025 +0800
feat: add diruptor unit test (#6079)
---
.../disruptor/DisruptorProviderManagerTest.java | 21 ++++
.../disruptor/consumer/QueueConsumerTest.java | 112 +++++++++++++++++++++
.../disruptor/provider/DisruptorProviderTest.java | 109 ++++++++++++++++++++
.../thread/DisruptorThreadFactoryTest.java | 21 ++++
4 files changed, 263 insertions(+)
diff --git
a/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/DisruptorProviderManagerTest.java
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/DisruptorProviderManagerTest.java
new file mode 100644
index 0000000000..87c3e46c90
--- /dev/null
+++
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/DisruptorProviderManagerTest.java
@@ -0,0 +1,21 @@
+/*
+ * 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.shenyu.disruptor;
+
+class DisruptorProviderManagerTest {
+}
diff --git
a/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/consumer/QueueConsumerTest.java
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/consumer/QueueConsumerTest.java
new file mode 100644
index 0000000000..cbb2ca552d
--- /dev/null
+++
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/consumer/QueueConsumerTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.shenyu.disruptor.consumer;
+
+import org.apache.shenyu.disruptor.event.DataEvent;
+import org.apache.shenyu.disruptor.event.OrderlyDataEvent;
+import org.apache.shenyu.disruptor.thread.OrderlyExecutor;
+import org.apache.shenyu.disruptor.thread.SingletonExecutor;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+class QueueConsumerTest {
+
+ private OrderlyExecutor mockExecutor;
+
+ private QueueConsumerFactory<String> mockFactory;
+
+ private QueueConsumerExecutor<String> mockConsumerExecutor;
+
+ private QueueConsumer<String> queueConsumer;
+
+ @BeforeEach
+ void setUp() {
+
+ mockExecutor = mock(OrderlyExecutor.class);
+ mockFactory = mock(QueueConsumerFactory.class);
+ mockConsumerExecutor = mock(QueueConsumerExecutor.class);
+ queueConsumer = new QueueConsumer<>(mockExecutor, mockFactory);
+ }
+
+ @Test
+ void testOnEventWithValidDataEvent() {
+ DataEvent<String> mockEvent = mock(DataEvent.class);
+ when(mockEvent.getData()).thenReturn("testData");
+ when(mockFactory.create()).thenReturn(mockConsumerExecutor);
+
+ SingletonExecutor mockSingletonExecutor =
mock(SingletonExecutor.class);
+ when(mockExecutor.select(null)).thenReturn(mockSingletonExecutor);
+
+ queueConsumer.onEvent(mockEvent);
+
+ verify(mockFactory).create();
+ verify(mockConsumerExecutor).setData("testData");
+ verify(mockEvent).setData(null);
+ }
+
+ @Test
+ void testOnEventWithNullDataEvent() {
+ queueConsumer.onEvent(null);
+
+ verifyNoInteractions(mockFactory);
+ verifyNoInteractions(mockExecutor);
+ }
+
+ @Test
+ void testOnEventWithOrderlyDataEventWithHash() {
+ OrderlyDataEvent<String> mockEvent = mock(OrderlyDataEvent.class);
+ when(mockEvent.getData()).thenReturn("testData");
+ when(mockEvent.getHash()).thenReturn("testHash");
+ when(mockFactory.create()).thenReturn(mockConsumerExecutor);
+
+ SingletonExecutor mockSingletonExecutor =
mock(SingletonExecutor.class);
+
when(mockExecutor.select("testHash")).thenReturn(mockSingletonExecutor);
+
+ queueConsumer.onEvent(mockEvent);
+
+ verify(mockFactory).create();
+ verify(mockConsumerExecutor).setData("testData");
+ verify(mockSingletonExecutor).execute(mockConsumerExecutor);
+ verify(mockEvent).setData(null);
+ }
+
+ @Test
+ void testOnEventWithOrderlyDataEventWithoutHash() {
+ // Arrange
+ OrderlyDataEvent<String> mockEvent = mock(OrderlyDataEvent.class);
+ when(mockEvent.getData()).thenReturn("testData");
+ when(mockEvent.getHash()).thenReturn("");
+ when(mockFactory.create()).thenReturn(mockConsumerExecutor);
+
+ SingletonExecutor mockSingletonExecutor =
mock(SingletonExecutor.class);
+ when(mockExecutor.select(null)).thenReturn(mockSingletonExecutor);
+
+ // Act
+ queueConsumer.onEvent(mockEvent);
+
+ // Assert
+ verify(mockFactory).create();
+ verify(mockConsumerExecutor).setData("testData");
+ verify(mockEvent).setData(null);
+ }
+}
diff --git
a/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/provider/DisruptorProviderTest.java
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/provider/DisruptorProviderTest.java
new file mode 100644
index 0000000000..6333165f73
--- /dev/null
+++
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/provider/DisruptorProviderTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.shenyu.disruptor.provider;
+
+import com.lmax.disruptor.RingBuffer;
+import com.lmax.disruptor.dsl.Disruptor;
+import org.apache.shenyu.disruptor.event.DataEvent;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+class DisruptorProviderTest {
+
+ private RingBuffer<DataEvent<String>> mockRingBuffer;
+
+ private Disruptor<DataEvent<String>> mockDisruptor;
+
+ private DisruptorProvider<String> disruptorProvider;
+
+ @BeforeEach
+ void setUp() {
+
+ mockRingBuffer = mock(RingBuffer.class);
+ mockDisruptor = mock(Disruptor.class);
+ disruptorProvider = new DisruptorProvider<>(mockRingBuffer,
mockDisruptor, false);
+ }
+
+ @Test
+ void testOnData() {
+
+ String testData = "testData";
+
+ disruptorProvider.onData(testData);
+
+ verify(mockRingBuffer).publishEvent(any(), eq(testData));
+ }
+
+ @Test
+ void testOnDataThrowsExceptionForOrderlyProvider() {
+
+ disruptorProvider = new DisruptorProvider<>(mockRingBuffer,
mockDisruptor, true);
+
+ IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () ->
disruptorProvider.onData("testData"));
+ assertEquals("The current provider is of orderly type. Please use
onOrderlyData() method.", exception.getMessage());
+ }
+
+ @Test
+ void testOnOrderlyData() {
+
+ disruptorProvider = new DisruptorProvider<>(mockRingBuffer,
mockDisruptor, true);
+ String testData = "testData";
+ String[] hashArray = {"hash1", "hash2"};
+
+ disruptorProvider.onOrderlyData(testData, hashArray);
+
+ verify(mockRingBuffer).publishEvent(any(), eq(testData),
eq("hash1:hash2"));
+ }
+
+ @Test
+ void testOnOrderlyDataThrowsExceptionForNonOrderlyProvider() {
+
+ String testData = "testData";
+ String[] hashArray = {"hash1", "hash2"};
+
+ IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () ->
disruptorProvider.onOrderlyData(testData, hashArray));
+ assertEquals("The current provider is not of orderly type. Please use
onData() method.", exception.getMessage());
+ }
+
+ @Test
+ void testShutdown() {
+
+ disruptorProvider.shutdown();
+
+ verify(mockDisruptor).shutdown();
+ }
+
+ @Test
+ void testShutdownWithNullDisruptor() {
+
+ disruptorProvider = new DisruptorProvider<>(mockRingBuffer, null,
false);
+
+ disruptorProvider.shutdown();
+
+ verifyNoInteractions(mockDisruptor);
+ }
+
+}
diff --git
a/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/thread/DisruptorThreadFactoryTest.java
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/thread/DisruptorThreadFactoryTest.java
new file mode 100644
index 0000000000..abb6ef184e
--- /dev/null
+++
b/shenyu-disruptor/src/test/java/org/apache/shenyu/disruptor/thread/DisruptorThreadFactoryTest.java
@@ -0,0 +1,21 @@
+/*
+ * 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.shenyu.disruptor.thread;
+
+class DisruptorThreadFactoryTest {
+}