Skip to content

Commit

Permalink
Introduce TestResource to catch error during handleRequest.
Browse files Browse the repository at this point in the history
Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Feb 10, 2024
1 parent c9f79ba commit 4c27153
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
********************************************************************************/
package org.eclipse.californium.core.coap;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.network.Exchange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Test resource to handles junit asserts of the scopes of other thread.
*
* Intended to use assert with {@link #handleRequest(Exchange)} execution.
*
* @since 3.11
*/
public class TestResource extends CoapResource {

private static final Logger LOGGER = LoggerFactory.getLogger(TestResource.class);

private List<Error> errors = new ArrayList<>();

public TestResource(String name) {
super(name);
}

@Override
public void handleRequest(final Exchange exchange) {
try {
super.handleRequest(exchange);
} catch (Error e) {
LOGGER.warn("{}", e.getMessage(), e);
errors.add(e);
}
}

/**
* Report errors catched during execution of
* {@link #handleRequest(Exchange)}.
*
* @throw Errors if occurred execution {@link #handleRequest(Exchange)}
*/
public void report() {
if (!errors.isEmpty()) {
throw errors.get(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.californium.core.coap.BlockOption;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.TestResource;
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
Expand Down Expand Up @@ -268,8 +269,7 @@ public String getProtocol() {
serverEndpoint = builder.build();
serverEndpoint.addInterceptor(interceptor);
result.addEndpoint(serverEndpoint);

result.add(new CoapResource(RESOURCE_TEST) {
TestResource testResource = new TestResource(RESOURCE_TEST) {

@Override
public void handleGET(final CoapExchange exchange) {
Expand All @@ -285,7 +285,9 @@ public void handlePOST(final CoapExchange exchange) {
assertEquals(payload, LONG_POST_REQUEST);
exchange.respond(LONG_POST_RESPONSE);
}
});
};
cleanup.add(testResource);
result.add(testResource);
result.add(new CoapResource(RESOURCE_BIG) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.TestResource;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.server.resources.CoapExchange;
Expand Down Expand Up @@ -398,8 +399,7 @@ private static CoapServer createSimpleServer() {
builderStrictBlock2.setConfiguration(configEndpointStrictBlock2Option);
serverEndpointStrictBlock2Option = builderStrictBlock2.build();
result.addEndpoint(serverEndpointStrictBlock2Option);

result.add(new CoapResource(RESOURCE_TEST) {
TestResource testResoure = new TestResource(RESOURCE_TEST) {

private boolean isShortRequest(final CoapExchange exchange) {
return exchange.getQueryParameter(PARAM_SHORT_REQ) != null;
Expand Down Expand Up @@ -445,7 +445,9 @@ public void handlePOST(final CoapExchange exchange) {
exchange.respond(LONG_POST_RESPONSE);
}
}
});
};
cleanup.add(testResoure);
result.add(testResoure);
result.add(new CoapResource(RESOURCE_BIG) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.californium.TestTools;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.TestResource;
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.network.UdpMatcher;
import org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter;
Expand Down Expand Up @@ -158,6 +158,7 @@ public void setup() throws Exception {
expectedToken = null;
testResource = new MyTestResource(RESOURCE_PATH);
testResource.setObservable(true);
cleanup.add(testResource);
setupServerAndClient();
}

Expand Down Expand Up @@ -1163,7 +1164,7 @@ public void testObserveWithBlockwiseResponseEarlyNegotiation() throws Exception
}

// All tests are made with this resource
private class MyTestResource extends CoapResource {
private class MyTestResource extends TestResource {

public AtomicInteger calls = new AtomicInteger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.TestResource;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.EndpointManager;
import org.eclipse.californium.core.test.lockstep.LockstepEndpoint;
Expand Down Expand Up @@ -76,6 +77,10 @@ public void add(LockstepEndpoint endpoint) {
cleanup.add(endpoint);
}

public void add(TestResource resource) {
cleanup.add(resource);
}

@Override
protected void shutdown() {
for (Object resource : cleanup) {
Expand All @@ -93,6 +98,8 @@ protected void shutdown() {
((LockstepEndpoint) resource).destroy();
} else if (resource instanceof Connector) {
((Connector) resource).destroy();
} else if (resource instanceof TestResource) {
((TestResource) resource).report();
}
} catch (RuntimeException ex) {
LOGGER.warn("shutdown failed!", ex);
Expand Down

0 comments on commit 4c27153

Please sign in to comment.