1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 08:12:05 +01:00

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

View file

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