use of org.apache.qpid.server.protocol.v0_10.transport.ConnectionTune in project qpid-broker-j by apache.
the class ConnectionTest method heartbeating.
@Test
@SpecificationTest(section = "9.connection", description = "The heartbeat control may be used to generate artificial network traffic when a connection " + "is idle.")
public void heartbeating() throws Exception {
try (FrameTransport transport = new FrameTransport(getBrokerAdmin()).connect()) {
final Interaction interaction = transport.newInteraction();
ConnectionTune response = interaction.authenticateConnection().getLatestResponse(ConnectionTune.class);
assumeThat(response.hasHeartbeatMin(), is(true));
assumeThat(response.hasHeartbeatMax(), is(true));
assumeThat(response.getHeartbeatMin(), is(greaterThanOrEqualTo(0)));
assumeThat(response.getHeartbeatMax(), is(greaterThanOrEqualTo(1)));
final int heartbeatPeriod = 1;
interaction.connection().tuneOkChannelMax(response.getChannelMax()).tuneOkMaxFrameSize(response.getMaxFrameSize()).tuneOkHeartbeat(heartbeatPeriod).tuneOk();
final long startTime = System.currentTimeMillis();
interaction.connection().open().consumeResponse(ConnectionOpenOk.class).consumeResponse().getLatestResponse(ConnectionHeartbeat.class);
final long actualHeartbeatDelay = System.currentTimeMillis() - startTime;
// Includes wiggle room to allow for slow boxes.
final int maximumExpectedHeartbeatDelay = heartbeatPeriod * 2 * 2;
assertThat("Heartbeat not received within expected time frame", ((int) actualHeartbeatDelay / 1000), is(both(greaterThanOrEqualTo(heartbeatPeriod)).and(lessThanOrEqualTo(maximumExpectedHeartbeatDelay))));
interaction.connection().heartbeat();
interaction.consumeResponse(ConnectionHeartbeat.class).connection().heartbeat();
}
}
Aggregations