-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#429: tests to check there is no leak when error occurred before sending
Signed-off-by: Simon Bernard <[email protected]> Also-by: Achim Kraus <[email protected]>
- Loading branch information
1 parent
daa3a36
commit ff39290
Showing
5 changed files
with
370 additions
and
4 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...fornium-core/src/test/java/org/eclipse/californium/core/test/CountingMessageObserver.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,78 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 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 creation | ||
******************************************************************************/ | ||
package org.eclipse.californium.core.test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.eclipse.californium.core.coap.MessageObserverAdapter; | ||
import org.eclipse.californium.core.coap.Response; | ||
|
||
public class CountingMessageObserver extends MessageObserverAdapter { | ||
|
||
public AtomicInteger loadCalls = new AtomicInteger(); | ||
public AtomicInteger errorCalls = new AtomicInteger(); | ||
|
||
@Override | ||
public void onRetransmission() { | ||
} | ||
|
||
@Override | ||
public void onResponse(Response response) { | ||
int counter; | ||
synchronized (this) { | ||
counter = loadCalls.incrementAndGet(); | ||
notifyAll(); | ||
} | ||
System.out.println("Received " + counter + ". Notification: " + response); | ||
} | ||
|
||
@Override | ||
public void onSendError(Throwable error) { | ||
int counter; | ||
synchronized (this) { | ||
counter = errorCalls.incrementAndGet(); | ||
notifyAll(); | ||
} | ||
System.out.println(counter + " Errors!"); | ||
} | ||
|
||
public boolean waitForLoadCalls(final int counter, final long timeout, final TimeUnit unit) | ||
throws InterruptedException { | ||
return waitForCalls(counter, timeout, unit, loadCalls); | ||
} | ||
|
||
public boolean waitForErrorCalls(final int counter, final long timeout, final TimeUnit unit) | ||
throws InterruptedException { | ||
return waitForCalls(counter, timeout, unit, errorCalls); | ||
} | ||
|
||
private synchronized boolean waitForCalls(final int counter, final long timeout, final TimeUnit unit, | ||
AtomicInteger calls) throws InterruptedException { | ||
if (0 < timeout) { | ||
long end = System.nanoTime() + unit.toNanos(timeout); | ||
while (calls.get() < counter) { | ||
long left = TimeUnit.NANOSECONDS.toMillis(end - System.nanoTime()); | ||
if (0 < left) { | ||
wait(left); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
return calls.get() >= counter; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
californium-core/src/test/java/org/eclipse/californium/core/test/ErrorInjector.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,90 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 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 creation | ||
******************************************************************************/ | ||
package org.eclipse.californium.core.test; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.eclipse.californium.core.coap.Message; | ||
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.MessageInterceptorAdapter; | ||
import org.eclipse.californium.elements.EndpointContext; | ||
|
||
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); | ||
} | ||
|
||
@Override | ||
public void sendRequest(final Request request) { | ||
request.addMessageObserver(new ErrorInjectorMessageObserver(request)); | ||
} | ||
|
||
@Override | ||
public void sendResponse(final Response response) { | ||
response.addMessageObserver(new ErrorInjectorMessageObserver(response)); | ||
} | ||
|
||
private class ErrorInjectorMessageObserver extends MessageObserverAdapter { | ||
|
||
private Message message; | ||
|
||
public ErrorInjectorMessageObserver(Message message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public void onReadyToSend() { | ||
if (errorOnReadyToSend.getAndSet(false)) { | ||
RuntimeException exception = new IllegalStateException("Simulate error before to sent"); | ||
message.setSendError(exception); | ||
throw exception; | ||
} | ||
} | ||
|
||
@Override | ||
public void onSent() { | ||
if (errorOnReadyToSend.getAndSet(false)) { | ||
RuntimeException exception = new IllegalStateException("Simulate error on sent"); | ||
message.setSendError(exception); | ||
throw exception; | ||
} | ||
} | ||
|
||
@Override | ||
public void onContextEstablished(EndpointContext endpointContext) { | ||
if (errorOnEstablishedContext.getAndSet(false)) { | ||
RuntimeException exception = new IllegalStateException("Simulate error on context established"); | ||
message.setSendError(exception); | ||
throw exception; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.