Migrate redis-dispatch to JUnit 5

This commit is contained in:
Chris Eager 2021-12-20 17:58:36 -08:00 committed by Chris Eager
parent 8559e46e4a
commit 5e0cca0702
5 changed files with 102 additions and 131 deletions

View File

@ -4,28 +4,25 @@
*/ */
package org.whispersystems.dispatch; package org.whispersystems.dispatch;
import org.junit.Rule; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.whispersystems.dispatch.io.RedisPubSubConnectionFactory;
import org.whispersystems.dispatch.redis.PubSubConnection;
import org.whispersystems.dispatch.redis.PubSubReply;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.eq; import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;
import org.whispersystems.dispatch.io.RedisPubSubConnectionFactory;
import org.whispersystems.dispatch.redis.PubSubConnection;
import org.whispersystems.dispatch.redis.PubSubReply;
public class DispatchManagerTest { public class DispatchManagerTest {
private PubSubConnection pubSubConnection; private PubSubConnection pubSubConnection;
@ -33,31 +30,23 @@ public class DispatchManagerTest {
private DispatchManager dispatchManager; private DispatchManager dispatchManager;
private PubSubReplyInputStream pubSubReplyInputStream; private PubSubReplyInputStream pubSubReplyInputStream;
@Rule @BeforeEach
public ExternalResource resource = new ExternalResource() { void setUp() throws Exception {
@Override pubSubConnection = mock(PubSubConnection.class );
protected void before() throws Throwable { socketFactory = mock(RedisPubSubConnectionFactory.class);
pubSubConnection = mock(PubSubConnection.class ); pubSubReplyInputStream = new PubSubReplyInputStream();
socketFactory = mock(RedisPubSubConnectionFactory.class);
pubSubReplyInputStream = new PubSubReplyInputStream();
when(socketFactory.connect()).thenReturn(pubSubConnection); when(socketFactory.connect()).thenReturn(pubSubConnection);
when(pubSubConnection.read()).thenAnswer(new Answer<PubSubReply>() { when(pubSubConnection.read()).thenAnswer((Answer<PubSubReply>) invocationOnMock -> pubSubReplyInputStream.read());
@Override
public PubSubReply answer(InvocationOnMock invocationOnMock) throws Throwable {
return pubSubReplyInputStream.read();
}
});
dispatchManager = new DispatchManager(socketFactory, Optional.empty()); dispatchManager = new DispatchManager(socketFactory, Optional.empty());
dispatchManager.start(); dispatchManager.start();
} }
@Override @AfterEach
protected void after() { void tearDown() {
dispatchManager.shutdown();
} }
};
@Test @Test
public void testConnect() { public void testConnect() {
@ -65,7 +54,7 @@ public class DispatchManagerTest {
} }
@Test @Test
public void testSubscribe() throws IOException { public void testSubscribe() {
DispatchChannel dispatchChannel = mock(DispatchChannel.class); DispatchChannel dispatchChannel = mock(DispatchChannel.class);
dispatchManager.subscribe("foo", dispatchChannel); dispatchManager.subscribe("foo", dispatchChannel);
pubSubReplyInputStream.write(new PubSubReply(PubSubReply.Type.SUBSCRIBE, "foo", Optional.empty())); pubSubReplyInputStream.write(new PubSubReply(PubSubReply.Type.SUBSCRIBE, "foo", Optional.empty()));
@ -74,7 +63,7 @@ public class DispatchManagerTest {
} }
@Test @Test
public void testSubscribeUnsubscribe() throws IOException { public void testSubscribeUnsubscribe() {
DispatchChannel dispatchChannel = mock(DispatchChannel.class); DispatchChannel dispatchChannel = mock(DispatchChannel.class);
dispatchManager.subscribe("foo", dispatchChannel); dispatchManager.subscribe("foo", dispatchChannel);
dispatchManager.unsubscribe("foo", dispatchChannel); dispatchManager.unsubscribe("foo", dispatchChannel);
@ -86,7 +75,7 @@ public class DispatchManagerTest {
} }
@Test @Test
public void testMessages() throws IOException { public void testMessages() {
DispatchChannel fooChannel = mock(DispatchChannel.class); DispatchChannel fooChannel = mock(DispatchChannel.class);
DispatchChannel barChannel = mock(DispatchChannel.class); DispatchChannel barChannel = mock(DispatchChannel.class);

View File

@ -4,9 +4,9 @@
*/ */
package org.whispersystems.dispatch.redis; package org.whispersystems.dispatch.redis;
import static org.junit.Assert.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -18,12 +18,12 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.security.SecureRandom; import java.security.SecureRandom;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
public class PubSubConnectionTest { class PubSubConnectionTest {
private static final String REPLY = "*3\r\n" + private static final String REPLY = "*3\r\n" +
"$9\r\n" + "$9\r\n" +
@ -60,8 +60,7 @@ public class PubSubConnectionTest {
@Test @Test
public void testSubscribe() throws IOException { void testSubscribe() throws IOException {
// ByteChannel byteChannel = mock(ByteChannel.class);
OutputStream outputStream = mock(OutputStream.class); OutputStream outputStream = mock(OutputStream.class);
Socket socket = mock(Socket.class ); Socket socket = mock(Socket.class );
when(socket.getOutputStream()).thenReturn(outputStream); when(socket.getOutputStream()).thenReturn(outputStream);
@ -76,7 +75,7 @@ public class PubSubConnectionTest {
} }
@Test @Test
public void testUnsubscribe() throws IOException { void testUnsubscribe() throws IOException {
OutputStream outputStream = mock(OutputStream.class); OutputStream outputStream = mock(OutputStream.class);
Socket socket = mock(Socket.class ); Socket socket = mock(Socket.class );
when(socket.getOutputStream()).thenReturn(outputStream); when(socket.getOutputStream()).thenReturn(outputStream);
@ -91,7 +90,7 @@ public class PubSubConnectionTest {
} }
@Test @Test
public void testTricklyResponse() throws Exception { void testTricklyResponse() throws Exception {
InputStream inputStream = mockInputStreamFor(new TrickleInputStream(REPLY.getBytes())); InputStream inputStream = mockInputStreamFor(new TrickleInputStream(REPLY.getBytes()));
OutputStream outputStream = mock(OutputStream.class); OutputStream outputStream = mock(OutputStream.class);
Socket socket = mock(Socket.class ); Socket socket = mock(Socket.class );
@ -103,7 +102,7 @@ public class PubSubConnectionTest {
} }
@Test @Test
public void testFullResponse() throws Exception { void testFullResponse() throws Exception {
InputStream inputStream = mockInputStreamFor(new FullInputStream(REPLY.getBytes())); InputStream inputStream = mockInputStreamFor(new FullInputStream(REPLY.getBytes()));
OutputStream outputStream = mock(OutputStream.class); OutputStream outputStream = mock(OutputStream.class);
Socket socket = mock(Socket.class ); Socket socket = mock(Socket.class );
@ -115,7 +114,7 @@ public class PubSubConnectionTest {
} }
@Test @Test
public void testRandomLengthResponse() throws Exception { void testRandomLengthResponse() throws Exception {
InputStream inputStream = mockInputStreamFor(new RandomInputStream(REPLY.getBytes())); InputStream inputStream = mockInputStreamFor(new RandomInputStream(REPLY.getBytes()));
OutputStream outputStream = mock(OutputStream.class); OutputStream outputStream = mock(OutputStream.class);
Socket socket = mock(Socket.class ); Socket socket = mock(Socket.class );

View File

@ -5,42 +5,41 @@
package org.whispersystems.dispatch.redis.protocol; package org.whispersystems.dispatch.redis.protocol;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException; import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; class ArrayReplyHeaderTest {
public class ArrayReplyHeaderTest { @Test
void testNull() {
assertThrows(IOException.class, () -> new ArrayReplyHeader(null));
@Test(expected = IOException.class)
public void testNull() throws IOException {
new ArrayReplyHeader(null);
}
@Test(expected = IOException.class)
public void testBadPrefix() throws IOException {
new ArrayReplyHeader(":3");
}
@Test(expected = IOException.class)
public void testEmpty() throws IOException {
new ArrayReplyHeader("");
}
@Test(expected = IOException.class)
public void testTruncated() throws IOException {
new ArrayReplyHeader("*");
}
@Test(expected = IOException.class)
public void testBadNumber() throws IOException {
new ArrayReplyHeader("*ABC");
} }
@Test @Test
public void testValid() throws IOException { void testBadPrefix() {
assertThrows(IOException.class, () -> new ArrayReplyHeader(":3"));
}
@Test
void testEmpty() {
assertThrows(IOException.class, () -> new ArrayReplyHeader(""));
}
@Test
void testTruncated() {
assertThrows(IOException.class, () -> new ArrayReplyHeader("*"));
}
@Test
void testBadNumber() {
assertThrows(IOException.class, () -> new ArrayReplyHeader("*ABC"));
}
@Test
void testValid() throws IOException {
assertEquals(4, new ArrayReplyHeader("*4").getElementCount()); assertEquals(4, new ArrayReplyHeader("*4").getElementCount());
} }

View File

@ -4,36 +4,36 @@
*/ */
package org.whispersystems.dispatch.redis.protocol; package org.whispersystems.dispatch.redis.protocol;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException; import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; class IntReplyHeaderTest {
public class IntReplyHeaderTest { @Test
void testNull() {
@Test(expected = IOException.class) assertThrows(IOException.class, () -> new IntReply(null));
public void testNull() throws IOException {
new IntReply(null);
}
@Test(expected = IOException.class)
public void testEmpty() throws IOException {
new IntReply("");
}
@Test(expected = IOException.class)
public void testBadNumber() throws IOException {
new IntReply(":A");
}
@Test(expected = IOException.class)
public void testBadFormat() throws IOException {
new IntReply("*");
} }
@Test @Test
public void testValid() throws IOException { void testEmpty() {
assertThrows(IOException.class, () -> new IntReply(""));
}
@Test
void testBadNumber() {
assertThrows(IOException.class, () -> new IntReply(":A"));
}
@Test
void testBadFormat() {
assertThrows(IOException.class, () -> new IntReply("*"));
}
@Test
void testValid() throws IOException {
assertEquals(23, new IntReply(":23").getValue()); assertEquals(23, new IntReply(":23").getValue());
} }
} }

View File

@ -4,48 +4,32 @@
*/ */
package org.whispersystems.dispatch.redis.protocol; package org.whispersystems.dispatch.redis.protocol;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException; import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; class StringReplyHeaderTest {
public class StringReplyHeaderTest {
@Test @Test
public void testNull() { void testNull() {
try { assertThrows(IOException.class, () -> new StringReplyHeader(null));
new StringReplyHeader(null);
throw new AssertionError();
} catch (IOException e) {
// good
}
} }
@Test @Test
public void testBadNumber() { void testBadNumber() {
try { assertThrows(IOException.class, () -> new StringReplyHeader("$100A"));
new StringReplyHeader("$100A");
throw new AssertionError();
} catch (IOException e) {
// good
}
} }
@Test @Test
public void testBadPrefix() { void testBadPrefix() {
try { assertThrows(IOException.class, () -> new StringReplyHeader("*"));
new StringReplyHeader("*");
throw new AssertionError();
} catch (IOException e) {
// good
}
} }
@Test @Test
public void testValid() throws IOException { void testValid() throws IOException {
assertEquals(1000, new StringReplyHeader("$1000").getStringLength()); assertEquals(1000, new StringReplyHeader("$1000").getStringLength());
} }
} }