-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from vert-x3/konradmichael-issue-123
Konradmichael issue 123
- Loading branch information
Showing
9 changed files
with
237 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/io/vertx/mqtt/test/client/MqttClientKeepAliveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2016 Red Hat Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.vertx.mqtt.test.client; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.unit.Async; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import io.vertx.mqtt.MqttClient; | ||
import io.vertx.mqtt.MqttClientOptions; | ||
import io.vertx.mqtt.MqttServer; | ||
import io.vertx.mqtt.impl.MqttServerImpl; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* MQTT client keep alive tests using a Vert.x MQTT server to accommodate testing. | ||
*/ | ||
@RunWith(VertxUnitRunner.class) | ||
public class MqttClientKeepAliveTest { | ||
|
||
private Vertx vertx; | ||
private MqttServer server; | ||
|
||
private void startServer(TestContext ctx) { | ||
Async async = ctx.async(); | ||
server.listen(ctx.asyncAssertSuccess(server -> async.complete())); | ||
async.awaitSuccess(10000); | ||
} | ||
|
||
@Before | ||
public void before(TestContext ctx) { | ||
vertx = Vertx.vertx(); | ||
server = MqttServer.create(vertx); | ||
} | ||
|
||
@After | ||
public void after(TestContext ctx) { | ||
server.close(ctx.asyncAssertSuccess(v -> { | ||
vertx.close(ctx.asyncAssertSuccess()); | ||
})); | ||
} | ||
|
||
@Test | ||
public void clientWillDisconnectOnMissingPingResponse(TestContext ctx) { | ||
AtomicInteger pings = new AtomicInteger(); | ||
server.endpointHandler(endpoint -> { | ||
endpoint.autoKeepAlive(false); // Tell the server not to respond to PINGREQ | ||
endpoint.accept(false); | ||
endpoint.pingHandler(v -> pings.incrementAndGet()); | ||
}); | ||
startServer(ctx); | ||
MqttClientOptions options = new MqttClientOptions(); | ||
options.setKeepAliveTimeSeconds(2); | ||
options.setKeepAliveTimeout(1); | ||
MqttClient client = MqttClient.create(vertx, options); | ||
client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, ctx.asyncAssertSuccess(ack -> { | ||
Async async = ctx.async(); | ||
client.closeHandler(v -> { | ||
assertEquals(1, pings.get()); | ||
async.complete(); | ||
}); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/io/vertx/mqtt/test/server/MqttServerKeepAliveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2016 Red Hat Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.vertx.mqtt.test.server; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.unit.Async; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import io.vertx.mqtt.MqttClient; | ||
import io.vertx.mqtt.MqttClientOptions; | ||
import io.vertx.mqtt.MqttServer; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* MQTT server keep alive tests using a Vert.x MQTT server to accommodate testing. | ||
*/ | ||
@RunWith(VertxUnitRunner.class) | ||
public class MqttServerKeepAliveTest { | ||
|
||
private Vertx vertx; | ||
private MqttServer server; | ||
|
||
private void startServer(TestContext ctx) { | ||
Async async = ctx.async(); | ||
server.listen(ctx.asyncAssertSuccess(server -> async.complete())); | ||
async.awaitSuccess(10000); | ||
} | ||
|
||
@Before | ||
public void before(TestContext ctx) { | ||
vertx = Vertx.vertx(); | ||
server = MqttServer.create(vertx); | ||
} | ||
|
||
@After | ||
public void after(TestContext ctx) { | ||
server.close(ctx.asyncAssertSuccess(v -> { | ||
vertx.close(ctx.asyncAssertSuccess()); | ||
})); | ||
} | ||
|
||
@Test | ||
public void serverWillDisconnectOnTimeout(TestContext ctx) { | ||
server.endpointHandler(endpoint -> { | ||
endpoint.accept(false); | ||
endpoint.pingHandler(v -> ctx.fail()); | ||
}); | ||
startServer(ctx); | ||
MqttClientOptions options = new MqttClientOptions(); | ||
options.setAutoKeepAlive(false); // The client will manage pings manually | ||
options.setKeepAliveTimeSeconds(2); // Tell the server to disconnects the client after 3 seconds of inactivity | ||
MqttClient client = MqttClient.create(vertx, options); | ||
client.connect(MqttClientOptions.DEFAULT_PORT, MqttClientOptions.DEFAULT_HOST, ctx.asyncAssertSuccess(ack -> { | ||
Async async = ctx.async(); | ||
client.closeHandler(v -> { | ||
async.complete(); | ||
}); | ||
})); | ||
} | ||
} |