You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/06/30 12:40:13 UTC

[dubbo] branch 3.0 updated: Refactor ChannelBufferStreamTest.testAll to improve the test experience (#10244)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 6f50470b5a Refactor ChannelBufferStreamTest.testAll to improve the test experience (#10244)
6f50470b5a is described below

commit 6f50470b5a88a3bd09ab2a7611fbed419bbb761c
Author: cheese8 <yi...@163.com>
AuthorDate: Thu Jun 30 20:39:53 2022 +0800

    Refactor ChannelBufferStreamTest.testAll to improve the test experience (#10244)
---
 .../remoting/buffer/ChannelBufferStreamTest.java   | 134 +++++++++++----------
 1 file changed, 73 insertions(+), 61 deletions(-)

diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java
index 569059211c..0b39040837 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/buffer/ChannelBufferStreamTest.java
@@ -18,87 +18,99 @@ package org.apache.dubbo.remoting.buffer;
 
 import org.junit.jupiter.api.Test;
 
+import java.io.IOException;
+
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.mock;
 
 public class ChannelBufferStreamTest {
-
+    
+    @Test
+    public void testChannelBufferOutputStreamWithNull() {
+        assertThrows(NullPointerException.class, () -> new ChannelBufferOutputStream(null));
+    }
+    
+    @Test
+    public void testChannelBufferInputStreamWithNull() {
+        assertThrows(NullPointerException.class, () -> new ChannelBufferInputStream(null));
+    }
+    
+    @Test
+    public void testChannelBufferInputStreamWithNullAndLength() {
+        assertThrows(NullPointerException.class, () -> new ChannelBufferInputStream(null, 0));
+    }
+    
+    @Test
+    public void testChannelBufferInputStreamWithBadLength() {
+        assertThrows(IllegalArgumentException.class, () -> new ChannelBufferInputStream(mock(ChannelBuffer.class), -1));
+    }
+    
     @Test
-    public void testAll() throws Exception {
+    public void testChannelBufferInputStreamWithOutOfBounds() {
+        assertThrows(IndexOutOfBoundsException.class, () -> {
+            ChannelBuffer buf = mock(ChannelBuffer.class);
+            new ChannelBufferInputStream(buf, buf.capacity() + 1);
+        });
+    }
+    
+    @Test
+    public void testChannelBufferWriteOutAndReadIn() {
         ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
-
-        try {
-            new ChannelBufferOutputStream(null);
-            fail();
-        } catch (NullPointerException e) {
-            // Expected
+        testChannelBufferOutputStream(buf);
+        testChannelBufferInputStream(buf);
+    }
+    
+    public void testChannelBufferOutputStream(final ChannelBuffer buf) {
+        try (ChannelBufferOutputStream out = new ChannelBufferOutputStream(buf)) {
+            assertSame(buf, out.buffer());
+            write(out);
+        } catch (IOException ioe) {
+            // ignored
         }
-
-        ChannelBufferOutputStream out = new ChannelBufferOutputStream(buf);
-        assertSame(buf, out.buffer());
+    }
+    
+    private void write(final ChannelBufferOutputStream out) throws IOException {
         out.write(new byte[0]);
         out.write(new byte[]{1, 2, 3, 4});
         out.write(new byte[]{1, 3, 3, 4}, 0, 0);
-        out.close();
-
-        try {
-            new ChannelBufferInputStream(null);
-            fail();
-        } catch (NullPointerException e) {
-            // Expected
-        }
-
-        try {
-            new ChannelBufferInputStream(null, 0);
-            fail();
-        } catch (NullPointerException e) {
-            // Expected
-        }
-
-        try {
-            new ChannelBufferInputStream(buf, -1);
-        } catch (IllegalArgumentException e) {
-            // Expected
-        }
-
-        try {
-            new ChannelBufferInputStream(buf, buf.capacity() + 1);
-        } catch (IndexOutOfBoundsException e) {
-            // Expected
+    }
+    
+    public void testChannelBufferInputStream(final ChannelBuffer buf) {
+        try (ChannelBufferInputStream in = new ChannelBufferInputStream(buf)) {
+            assertTrue(in.markSupported());
+            in.mark(Integer.MAX_VALUE);
+            
+            assertEquals(buf.writerIndex(), in.skip(Long.MAX_VALUE));
+            assertFalse(buf.readable());
+            
+            in.reset();
+            assertEquals(0, buf.readerIndex());
+            assertEquals(4, in.skip(4));
+            assertEquals(4, buf.readerIndex());
+            in.reset();
+            
+            readBytes(in);
+            
+            assertEquals(buf.readerIndex(), in.readBytes());
+        } catch (IOException ioe) {
+            // ignored
         }
-
-        ChannelBufferInputStream in = new ChannelBufferInputStream(buf);
-
-        assertTrue(in.markSupported());
-        in.mark(Integer.MAX_VALUE);
-
-        assertEquals(buf.writerIndex(), in.skip(Long.MAX_VALUE));
-        assertFalse(buf.readable());
-
-        in.reset();
-        assertEquals(0, buf.readerIndex());
-
-        assertEquals(4, in.skip(4));
-        assertEquals(4, buf.readerIndex());
-        in.reset();
-
-
+    }
+    
+    private void readBytes(ChannelBufferInputStream in) throws IOException {
         byte[] tmp = new byte[13];
         in.read(tmp);
-
+        
         assertEquals(1, tmp[0]);
         assertEquals(2, tmp[1]);
         assertEquals(3, tmp[2]);
         assertEquals(4, tmp[3]);
-
+        
         assertEquals(-1, in.read());
         assertEquals(-1, in.read(tmp));
-
-        in.close();
-
-        assertEquals(buf.readerIndex(), in.readBytes());
     }
 }