NO-JIRA use lambdas in tests for readability

This commit is contained in:
Justin Bertram 2017-09-15 11:16:44 -05:00 committed by Clebert Suconic
parent dac625179a
commit 956c4c6493
2 changed files with 16 additions and 92 deletions

View File

@ -842,26 +842,14 @@ public class MQTTTest extends MQTTTestSupport {
// publish non-retained message // publish non-retained message
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false); connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
Wait.waitFor(new Wait.Condition() { assertTrue(Wait.waitFor(() -> publishList.size() == 2, 5000));
@Override
public boolean isSatisfied() throws Exception {
return publishList.size() == 2;
}
}, 5000);
assertEquals(2, publishList.size());
connection.disconnect(); connection.disconnect();
connection = mqtt.blockingConnection(); connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
Wait.waitFor(new Wait.Condition() { assertTrue(Wait.waitFor(() -> publishList.size() == 4, 5000));
@Override
public boolean isSatisfied() throws Exception {
return publishList.size() == 4;
}
}, 5000);
assertEquals(4, publishList.size());
// TODO Investigate if receiving the same ID for overlapping subscriptions is actually spec compliant. // TODO Investigate if receiving the same ID for overlapping subscriptions is actually spec compliant.
// In Artemis we send a new ID for every copy of the message. // In Artemis we send a new ID for every copy of the message.
@ -1018,12 +1006,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
Wait.waitFor(new Wait.Condition() { Wait.waitFor(() -> connection.isConnected());
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
});
final String TOPIC = "TopicA"; final String TOPIC = "TopicA";
final byte[] qos = connection.subscribe(new Topic[]{new Topic(TOPIC, QoS.EXACTLY_ONCE)}); final byte[] qos = connection.subscribe(new Topic[]{new Topic(TOPIC, QoS.EXACTLY_ONCE)});
@ -1037,12 +1020,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection newConnection = mqtt.blockingConnection(); final BlockingConnection newConnection = mqtt.blockingConnection();
newConnection.connect(); newConnection.connect();
Wait.waitFor(new Wait.Condition() { Wait.waitFor(() -> newConnection.isConnected());
@Override
public boolean isSatisfied() throws Exception {
return newConnection.isConnected();
}
});
assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]); assertEquals(QoS.EXACTLY_ONCE.ordinal(), qos[0]);
Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS); Message msg = newConnection.receive(1000, TimeUnit.MILLISECONDS);
@ -1064,12 +1042,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
Wait.waitFor(new Wait.Condition() { Wait.waitFor(() -> connection.isConnected());
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
});
MQTT mqtt2 = createMQTTConnection("2", false); MQTT mqtt2 = createMQTTConnection("2", false);
BlockingConnection connection2 = mqtt2.blockingConnection(); BlockingConnection connection2 = mqtt2.blockingConnection();
@ -1098,12 +1071,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
Wait.waitFor(new Wait.Condition() { Wait.waitFor(() -> connection.isConnected());
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
});
// kill transport // kill transport
connection.kill(); connection.kill();
@ -1276,13 +1244,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() { assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
}));
connection.disconnect(); connection.disconnect();
} }
@ -1299,13 +1261,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() { assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
}));
connection.disconnect(); connection.disconnect();
} }
@ -1365,19 +1321,9 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection1 = mqtt1.blockingConnection(); final BlockingConnection connection1 = mqtt1.blockingConnection();
connection1.connect(); connection1.connect();
assertTrue("Duplicate client disconnected", Wait.waitFor(new Wait.Condition() { assertTrue("Duplicate client disconnected", Wait.waitFor(() -> connection1.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return connection1.isConnected();
}
}));
assertTrue("Old client still connected", Wait.waitFor(new Wait.Condition() { assertTrue("Old client still connected", Wait.waitFor(() -> !connection.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return !connection.isConnected();
}
}));
connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true); connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
connection1.disconnect(); connection1.disconnect();
@ -1399,20 +1345,10 @@ public class MQTTTest extends MQTTTestSupport {
connection.connect(); connection.connect();
connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true); connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
assertTrue("Client connect failed for attempt: " + i, Wait.waitFor(new Wait.Condition() { assertTrue("Client connect failed for attempt: " + i, Wait.waitFor(() -> connection.isConnected(), 3000, 200));
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
}, TimeUnit.SECONDS.toMillis(3), TimeUnit.MILLISECONDS.toMillis(200)));
if (oldConnection.get() != null) { if (oldConnection.get() != null) {
assertTrue("Old client still connected on attempt: " + i, Wait.waitFor(new Wait.Condition() { assertTrue("Old client still connected on attempt: " + i, Wait.waitFor(() -> !oldConnection.get().isConnected(), 3000, 200));
@Override
public boolean isSatisfied() throws Exception {
return !oldConnection.get().isConnected();
}
}, TimeUnit.SECONDS.toMillis(3), TimeUnit.MILLISECONDS.toMillis(200)));
} }
oldConnection.set(connection); oldConnection.set(connection);
@ -1575,13 +1511,7 @@ public class MQTTTest extends MQTTTestSupport {
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() { assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
}));
} }
@Test(timeout = 60 * 1000) @Test(timeout = 60 * 1000)
@ -1773,13 +1703,7 @@ public class MQTTTest extends MQTTTestSupport {
mqtt.setKeepAlive((short) 2); mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection(); final BlockingConnection connection = mqtt.blockingConnection();
connection.connect(); connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(new Wait.Condition() { assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
@Override
public boolean isSatisfied() throws Exception {
return connection.isConnected();
}
}));
connection.disconnect(); connection.disconnect();
} }

View File

@ -40,10 +40,10 @@ public class Wait {
} }
public static boolean waitFor(final Condition condition, public static boolean waitFor(final Condition condition,
final long duration, final long durationMillis,
final long sleepMillis) throws Exception { final long sleepMillis) throws Exception {
final long expiry = System.currentTimeMillis() + duration; final long expiry = System.currentTimeMillis() + durationMillis;
boolean conditionSatisified = condition.isSatisfied(); boolean conditionSatisified = condition.isSatisfied();
while (!conditionSatisified && System.currentTimeMillis() < expiry) { while (!conditionSatisified && System.currentTimeMillis() < expiry) {
TimeUnit.MILLISECONDS.sleep(sleepMillis); TimeUnit.MILLISECONDS.sleep(sleepMillis);