Skip to content

Commit

Permalink
Avoid NPE when processing response message in unit tests
Browse files Browse the repository at this point in the history
The CoapAsyncRequestObserver spawns a cleaning task when an outbound
message is ready to be sent. When invoking the onResponse method in a
unit test to trigger the processing of an incoming response, a NPE is
thrown if onReadyToSend has not been invoked before.

All other methods use the private cancelCleaningTask method to stop the
cleaning task which performs a non-null check before stopping the task.
The onResponse method has been changed to also use this method in order
to prevent the NPE.

Signed-off-by: Kai Hudalla <kai.hudalla@bosch.io>
  • Loading branch information
sophokles73 authored and sbernard31 committed Apr 15, 2021
1 parent d33516e commit afc43f2
Showing 1 changed file with 6 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016 Sierra Wireless and others.
* Copyright (c) 2016, 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
Expand Down Expand Up @@ -94,7 +94,7 @@ public void onResponse(Response coapResponse) {
LOG.debug("Received coap response: {} for {}", coapResponse, coapRequest);
coapRequest.removeMessageObserver(this);
if (eventRaised.compareAndSet(false, true)) {
cleaningTask.cancel(false);
cancelCleaningTask();
try {
responseCallback.onResponse(coapResponse);
} catch (RuntimeException e) {
Expand All @@ -114,8 +114,8 @@ public void onReadyToSend() {

@Override
public void onTimeout() {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
cancelCleaningTask();
errorCallback.onError(new TimeoutException(Type.COAP_TIMEOUT,
"Request %s timed out : CoAP or blockwise timeout", coapRequest.getURI()));
} else {
Expand All @@ -126,8 +126,8 @@ public void onTimeout() {

@Override
public void onCancel() {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
cancelCleaningTask();
if (responseTimedOut.get()) {
errorCallback.onError(new TimeoutException(Type.RESPONSE_TIMEOUT,
"Request %s timed out : no response received", coapRequest.getURI()));
Expand All @@ -143,8 +143,8 @@ public void onCancel() {

@Override
public void onReject() {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
cancelCleaningTask();
errorCallback.onError(new RequestRejectedException("Request %s rejected", coapRequest.getURI()));
} else {
LOG.debug("OnReject callback ignored because an event was already raised for this request {}", coapRequest);
Expand All @@ -153,8 +153,8 @@ public void onReject() {

@Override
public void onSendError(Throwable error) {
cancelCleaningTask();
if (eventRaised.compareAndSet(false, true)) {
cancelCleaningTask();
if (error instanceof DtlsHandshakeTimeoutException) {
errorCallback.onError(new TimeoutException(Type.DTLS_HANDSHAKE_TIMEOUT, error,
"Request %s timeout : dtls handshake timeout", coapRequest.getURI()));
Expand Down

0 comments on commit afc43f2

Please sign in to comment.