-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b592795
commit dd98fe4
Showing
4 changed files
with
1,941 additions
and
3 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/FailingTest.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,161 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2017 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Sierra Wireless - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.integration.tests; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.californium.core.coap.CoAP.Type; | ||
import org.eclipse.californium.core.network.config.NetworkConfig; | ||
import org.eclipse.leshan.Link; | ||
import org.eclipse.leshan.core.request.BindingMode; | ||
import org.eclipse.leshan.core.request.ReadRequest; | ||
import org.eclipse.leshan.core.request.RegisterRequest; | ||
import org.eclipse.leshan.core.request.exception.TimeoutException; | ||
import org.eclipse.leshan.core.response.ReadResponse; | ||
import org.eclipse.leshan.server.californium.LeshanServerBuilder; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class FailingTest { | ||
|
||
public IntegrationTestHelper helper = new IntegrationTestHelper() { | ||
@Override | ||
protected LeshanServerBuilder createServerBuilder() { | ||
NetworkConfig coapConfig = LeshanServerBuilder.createDefaultNetworkConfig(); | ||
|
||
// configure retransmission, with this configuration a request without ACK should timeout in ~200*5ms | ||
coapConfig.setInt(NetworkConfig.Keys.ACK_TIMEOUT, 200).setFloat(NetworkConfig.Keys.ACK_RANDOM_FACTOR, 1f) | ||
.setFloat(NetworkConfig.Keys.ACK_TIMEOUT_SCALE, 1f).setInt(NetworkConfig.Keys.MAX_RETRANSMIT, 4); | ||
|
||
LeshanServerBuilder builder = super.createServerBuilder(); | ||
builder.setCoapConfig(coapConfig); | ||
|
||
return builder; | ||
}; | ||
}; | ||
|
||
@Before | ||
public void start() { | ||
helper.initialize(); | ||
helper.createServer(); | ||
helper.server.start(); | ||
} | ||
|
||
@After | ||
public void stop() { | ||
helper.server.destroy(); | ||
helper.dispose(); | ||
} | ||
|
||
@Test | ||
public void sync_send_without_acknowleged() throws Exception { | ||
// Register client | ||
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress()); | ||
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, | ||
Link.parse("</1>,</2>,</3>".getBytes()), null)); | ||
client.expectResponse().go(); | ||
helper.waitForRegistration(1); | ||
|
||
// Send read | ||
Future<ReadResponse> future = Executors.newSingleThreadExecutor().submit(new Callable<ReadResponse>() { | ||
@Override | ||
public ReadResponse call() throws Exception { | ||
// send a request with 3 seconds timeout | ||
return helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000); | ||
} | ||
}); | ||
// Request should timedout in ~1s we don't send ACK | ||
ReadResponse response = future.get(1500, TimeUnit.MILLISECONDS); | ||
Assert.assertNull("we should timeout", response); | ||
} | ||
|
||
@Test | ||
public void sync_send_with_acknowleged_request_without_response() throws Exception { | ||
// Register client | ||
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress()); | ||
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, | ||
Link.parse("</1>,</2>,</3>".getBytes()), null)); | ||
client.expectResponse().go(); | ||
helper.waitForRegistration(1); | ||
|
||
// Send read | ||
Future<ReadResponse> future = Executors.newSingleThreadExecutor().submit(new Callable<ReadResponse>() { | ||
@Override | ||
public ReadResponse call() throws Exception { | ||
// send a request with 3 seconds timeout | ||
return helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000); | ||
} | ||
}); | ||
|
||
// Acknowledge the response | ||
client.expectRequest().storeMID("R").go(); | ||
client.sendEmpty(Type.ACK).loadMID("R").go(); | ||
|
||
// Request should timedout in ~3s as we send the ACK | ||
Thread.sleep(1500); | ||
Assert.assertFalse("we should still wait for response", future.isDone()); | ||
ReadResponse response = future.get(2000, TimeUnit.MILLISECONDS); | ||
Assert.assertNull("we should timeout", response); | ||
} | ||
|
||
@Test | ||
public void async_send_without_acknowleged() throws Exception { | ||
// register client | ||
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress()); | ||
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, | ||
Link.parse("</1>,</2>,</3>".getBytes()), null)); | ||
client.expectResponse().go(); | ||
helper.waitForRegistration(1); | ||
|
||
// send read | ||
Callback<ReadResponse> callback = new Callback<ReadResponse>(); | ||
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000l, callback, callback); | ||
|
||
// Request should timedout in ~1s we don't send ACK | ||
callback.waitForResponse(1500); | ||
Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException); | ||
|
||
} | ||
|
||
@Test | ||
public void async_send_with_acknowleged_request_without_response() throws Exception { | ||
// register client | ||
LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress()); | ||
client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, | ||
Link.parse("</1>,</2>,</3>".getBytes()), null)); | ||
client.expectResponse().go(); | ||
helper.waitForRegistration(1); | ||
|
||
// send read | ||
Callback<ReadResponse> callback = new Callback<ReadResponse>(); | ||
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000l, callback, callback); | ||
|
||
// Acknowledge the response | ||
client.expectRequest().storeMID("R").go(); | ||
client.sendEmpty(Type.ACK).loadMID("R").go(); | ||
|
||
// Request should timedout in ~3s as we send a ack | ||
Thread.sleep(1500); | ||
Assert.assertTrue("we should still wait for response", callback.getException() == null); | ||
callback.waitForResponse(2000); | ||
Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
...gration-tests/src/test/java/org/eclipse/leshan/integration/tests/LockStepLwM2mClient.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,54 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2017 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Sierra Wireless - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.integration.tests; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.Random; | ||
|
||
import org.eclipse.californium.core.coap.Request; | ||
import org.eclipse.californium.core.network.serialization.UdpDataSerializer; | ||
import org.eclipse.californium.elements.RawData; | ||
import org.eclipse.leshan.client.californium.impl.CoapRequestBuilder; | ||
import org.eclipse.leshan.core.request.UplinkRequest; | ||
import org.eclipse.leshan.core.response.LwM2mResponse; | ||
|
||
public class LockStepLwM2mClient extends LockstepEndpoint { | ||
|
||
private static final Random r = new Random(); | ||
private InetSocketAddress destination; | ||
|
||
public LockStepLwM2mClient(final InetSocketAddress destination) { | ||
super(destination); | ||
this.destination = destination; | ||
} | ||
|
||
public void sendLwM2mRequest(UplinkRequest<? extends LwM2mResponse> lwm2mReq) { | ||
// create CoAP request | ||
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination); | ||
lwm2mReq.accept(coapRequestBuilder); | ||
Request coapReq = coapRequestBuilder.getRequest(); | ||
byte[] token = new byte[8]; | ||
r.nextBytes(token); | ||
coapReq.setToken(token); | ||
|
||
// serialize request | ||
UdpDataSerializer serializer = new UdpDataSerializer(); | ||
RawData raw = serializer.serializeRequest(coapReq); | ||
|
||
// send it | ||
super.send(raw); | ||
} | ||
} |
Oops, something went wrong.