Fix InitationListenerTest's timeouts

Don't use Thread.sleep(), isntead we use Mockito's timeout() method.
This commit is contained in:
Florian Schmaus 2017-06-16 22:51:14 +02:00
parent 08e897113d
commit 759a7d7a70
1 changed files with 14 additions and 33 deletions

View File

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.bytestreams.socks5;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import org.jivesoftware.smack.XMPPConnection;
@ -45,6 +46,8 @@ import org.powermock.reflect.Whitebox;
*/
public class InitiationListenerTest {
private final int TIMEOUT = 10000;
static final EntityFullJid initiatorJID = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE;
static final EntityFullJid targetJID = JidTestUtil.FULL_JID_1_RESOURCE_1;
static final DomainBareJid xmppServer = JidTestUtil.DOMAIN_BARE_JID_1;
@ -94,12 +97,9 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// capture reply to the SOCKS5 Bytestream initiation
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
verify(connection).sendStanza(argument.capture());
verify(connection, timeout(TIMEOUT)).sendStanza(argument.capture());
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
@ -124,12 +124,9 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert listener is called once
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(listener).incomingBytestreamRequest(byteStreamRequest.capture());
verify(listener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert that listener is called for the correct request
assertEquals(initiatorJID, byteStreamRequest.getValue().getFrom());
@ -152,12 +149,9 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert listener is called once
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(listener).incomingBytestreamRequest(byteStreamRequest.capture());
verify(listener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert that reply is the correct error packet
assertEquals(initiatorJID, byteStreamRequest.getValue().getFrom());
@ -180,16 +174,13 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert listener is not called
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(listener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
// capture reply to the SOCKS5 Bytestream initiation
ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
verify(connection).sendStanza(argument.capture());
verify(connection, timeout(TIMEOUT)).sendStanza(argument.capture());
// assert that reply is the correct error packet
assertEquals(initiatorJID, argument.getValue().getTo());
@ -218,12 +209,9 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert user request listener is called once
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(userRequestsListener).incomingBytestreamRequest(byteStreamRequest.capture());
verify(userRequestsListener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert all requests listener is not called
byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
@ -252,16 +240,15 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert user request listener is not called
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(userRequestsListener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
// assert all requests listener is called
byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(allRequestsListener).incomingBytestreamRequest(byteStreamRequest.capture());
verify(allRequestsListener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert user request listener is not called
verify(userRequestsListener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
}
@ -287,9 +274,6 @@ public class InitiationListenerTest {
// run the listener with the initiation packet
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert user request listener is not called
ArgumentCaptor<BytestreamRequest> byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);
verify(userRequestsListener, never()).incomingBytestreamRequest(byteStreamRequest.capture());
@ -301,11 +285,8 @@ public class InitiationListenerTest {
// run the listener with the initiation packet again
initiationListener.handleIQRequest(initBytestream);
// wait because packet is processed in an extra thread
Thread.sleep(200);
// assert user request listener is called on the second request with the same session ID
verify(userRequestsListener).incomingBytestreamRequest(byteStreamRequest.capture());
verify(userRequestsListener, timeout(TIMEOUT)).incomingBytestreamRequest(byteStreamRequest.capture());
// assert all requests listener is not called
byteStreamRequest = ArgumentCaptor.forClass(BytestreamRequest.class);