Skip to content

Commit

Permalink
Add tests with timedout request
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jan 4, 2018
1 parent b592795 commit dd98fe4
Show file tree
Hide file tree
Showing 4 changed files with 1,941 additions and 3 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,18 @@ public ExecuteResponse execute(int resourceid, String params) {
}

public void createServer() {
server = createServerBuilder().build();
// monitor client registration
setupRegistrationMonitoring();
}

protected LeshanServerBuilder createServerBuilder() {
LeshanServerBuilder builder = new LeshanServerBuilder();
builder.setObjectModelProvider(new StaticModelProvider(createObjectModels()));
builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setSecurityStore(new InMemorySecurityStore());
server = builder.build();
// monitor client registration
setupRegistrationMonitoring();
return builder;
}

protected void setupRegistrationMonitoring() {
Expand Down
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);
}
}
Loading

0 comments on commit dd98fe4

Please sign in to comment.