diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServer.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServer.java index d0dbc06937..dcedde5105 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServer.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanServer.java @@ -430,7 +430,9 @@ public void destroy() { ((Stoppable) requestSender).stop(); } - presenceService.destroy(); + if (presenceService != null) { + presenceService.destroy(); + } LOG.info("LWM2M server destroyed."); } diff --git a/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/LeshanServerTest.java b/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/LeshanServerTest.java index e816e12075..65c8deedeb 100644 --- a/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/LeshanServerTest.java +++ b/leshan-server-cf/src/test/java/org/eclipse/leshan/server/californium/LeshanServerTest.java @@ -81,11 +81,34 @@ public void testStartStopDestroy() throws InterruptedException { assertEquals("All news created threads must be destroyed", numberOfThreadbefore, Thread.activeCount()); } + @Test + public void testStartStopDestroyQueueModeDisabled() throws InterruptedException { + // look at nb active thread before. + int numberOfThreadbefore = Thread.activeCount(); + + LeshanServer server = new LeshanServerBuilder().setLocalAddress(new InetSocketAddress(0)) + .disableQueueModeSupport().build(); + server.start(); + Thread.sleep(100); + // HACK force creation thread creation. + forceThreadsCreation(server); + Thread.sleep(100); + server.stop(); + Thread.sleep(100); + server.destroy(); + + // ensure all thread are destroyed + Thread.sleep(500); + assertEquals("All news created threads must be destroyed", numberOfThreadbefore, Thread.activeCount()); + } + private void forceThreadsCreation(LeshanServer server) { Registration reg = new Registration.Builder("id", "endpoint", Identity.unsecure(new InetSocketAddress(5555))) .bindingMode(EnumSet.of(BindingMode.U, BindingMode.Q)).build(); // Force timer thread creation of preference service. - ((PresenceServiceImpl) server.getPresenceService()).setAwake(reg); + if (server.getPresenceService() != null) { + ((PresenceServiceImpl) server.getPresenceService()).setAwake(reg); + } // Force time thread creation of CoapAsyncRequestObserver server.send(reg, new ReadRequest(3), new ResponseCallback() { @Override