-
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
- Loading branch information
1 parent
e8a6c42
commit f9f8b91
Showing
4 changed files
with
363 additions
and
3 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; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
/******************************************************************************* | ||
* 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.EmptyMessage; | ||
import org.eclipse.californium.core.coap.MessageObserver; | ||
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.elements.EndpointContext; | ||
|
||
public class ErrorInjector implements MessageInterceptor, MessageObserver{ | ||
|
||
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() { | ||
} | ||
} |
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