Skip to content

Commit

Permalink
#429: call Message.setSendError() in ErrorInjector directly.
Browse files Browse the repository at this point in the history
Signed-off-by: Achim Kraus <[email protected]>
  • Loading branch information
sbernard31 authored and Achim Kraus committed Jul 3, 2018
1 parent 3084cfa commit 3397bef
Showing 1 changed file with 39 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,110 +17,61 @@

import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.californium.core.coap.EmptyMessage;
import org.eclipse.californium.core.coap.MessageObserver;
import org.eclipse.californium.core.coap.MessageObserverAdapter;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.network.interceptors.MessageInterceptor;
import org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter;
import org.eclipse.californium.elements.EndpointContext;

public class ErrorInjector implements MessageInterceptor, MessageObserver{
public class ErrorInjector extends MessageInterceptorAdapter {

private AtomicBoolean errorOnEstablishedContext = new AtomicBoolean(false);
private AtomicBoolean errorOnSent = new AtomicBoolean(false);
private AtomicBoolean errorOnReadyToSend = new AtomicBoolean(false);



public void setErrorOnEstablishedContext() {
errorOnEstablishedContext.set(true);
}

public void setErrorOnSent() {
errorOnSent.set(true);
}

public void setErrorOnReadyToSend() {
errorOnReadyToSend.set(true);
}

//************* Message Interceptor *******************//

@Override
public void sendRequest(Request request) {
request.addMessageObserver(this);
}

@Override
public void sendResponse(Response response) {
}

@Override
public void sendEmptyMessage(EmptyMessage message) {
}

@Override
public void receiveRequest(Request request) {
}

@Override
public void receiveResponse(Response response) {
}

@Override
public void receiveEmptyMessage(EmptyMessage message) {
}

//************* Message observer *******************//
@Override
public void onRetransmission() {
}

@Override
public void onResponse(Response response) {
}

@Override
public void onAcknowledgement() {
}

@Override
public void onReject() {
}

@Override
public void onTimeout() {
}

@Override
public void onCancel() {
}

@Override
public void onReadyToSend() {
if (errorOnReadyToSend.getAndSet(false)) {
throw new IllegalStateException("Simulate error before to sent");
}
}

@Override
public void onSent() {
if (errorOnReadyToSend.getAndSet(false)) {
throw new IllegalStateException("Simulate error on sent");
}
}

@Override
public void onSendError(Throwable error) {
}

@Override
public void onContextEstablished(EndpointContext endpointContext) {
if (errorOnEstablishedContext.getAndSet(false)) {
throw new IllegalStateException("Simulate error on context established");
}
}

@Override
public void onComplete() {
// ************* Message Interceptor *******************//

@Override
public void sendRequest(final Request request) {
request.addMessageObserver(new MessageObserverAdapter() {

@Override
public void onReadyToSend() {
if (errorOnReadyToSend.getAndSet(false)) {
RuntimeException exception = new IllegalStateException("Simulate error before to sent");
request.setSendError(exception);
throw exception;
}
}

@Override
public void onSent() {
if (errorOnReadyToSend.getAndSet(false)) {
RuntimeException exception = new IllegalStateException("Simulate error on sent");
request.setSendError(exception);
throw exception;
}
}

@Override
public void onContextEstablished(EndpointContext endpointContext) {
if (errorOnEstablishedContext.getAndSet(false)) {
RuntimeException exception = new IllegalStateException("Simulate error on context established");
request.setSendError(exception);
throw exception;
}
}
});
}
}

0 comments on commit 3397bef

Please sign in to comment.