From 26150c77f19e61e85618f471e717e08a425dbbfd Mon Sep 17 00:00:00 2001 From: Szymon Sasin Date: Mon, 26 Aug 2024 11:22:11 +0300 Subject: [PATCH] Reuse coap-builder --- .../mbed/coap/server/CoapServerBuilder.java | 14 +++++------ .../coap/server/CoapServerBuilderTest.java | 24 ++++++++++++++++++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java index 40e5c0d4..539db44b 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java +++ b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -137,7 +137,7 @@ public CoapServerBuilder maxIncomingBlockTransferSize(int size) { return this; } - private PutOnlyMap getOrCreateDuplicateDetectorCache() { + private PutOnlyMap getOrCreateDuplicateDetectorCache(ScheduledExecutorService scheduledExecutorService) { if (duplicateDetectionCache == null) { duplicateDetectionCache = new DefaultDuplicateDetectorCache("Default cache", duplicationMaxSize, scheduledExecutorService); } @@ -210,11 +210,9 @@ public CoapServerBuilder requestTagSupplier(RequestTagSupplier requestTagSupplie public CoapServer build() { requireNonNull(coapTransport, "Missing transport"); final boolean stopExecutor = scheduledExecutorService == null; - if (scheduledExecutorService == null) { - scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - } + final ScheduledExecutorService effectiveExecutorService = scheduledExecutorService != null ? scheduledExecutorService : Executors.newSingleThreadScheduledExecutor(); + Timer timer = toTimer(effectiveExecutorService); - Timer timer = toTimer(scheduledExecutorService); Service sender = packet -> coapTransport.sendPacket(packet) .whenComplete((__, throwable) -> logSent(packet, throwable)); @@ -248,7 +246,7 @@ public CoapServer build() { .then(sender); // INBOUND - PutOnlyMap duplicateDetectorCache = getOrCreateDuplicateDetectorCache(); + PutOnlyMap duplicateDetectorCache = getOrCreateDuplicateDetectorCache(effectiveExecutorService); DuplicateDetector duplicateDetector = new DuplicateDetector(duplicateDetectorCache, duplicatedCoapMessageCallback); Service inboundService = duplicateDetector .andThen(new CoapRequestConverter(midSupplier)) @@ -271,7 +269,7 @@ public CoapServer build() { piggybackedExchangeFilter.stop(); duplicateDetectorCache.stop(); if (stopExecutor) { - scheduledExecutorService.shutdown(); + effectiveExecutorService.shutdown(); } }); diff --git a/coap-core/src/test/java/com/mbed/coap/server/CoapServerBuilderTest.java b/coap-core/src/test/java/com/mbed/coap/server/CoapServerBuilderTest.java index 4d97618d..b336cdf2 100644 --- a/coap-core/src/test/java/com/mbed/coap/server/CoapServerBuilderTest.java +++ b/coap-core/src/test/java/com/mbed/coap/server/CoapServerBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,8 +16,11 @@ */ package com.mbed.coap.server; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; +import com.mbed.coap.client.CoapClient; +import com.mbed.coap.transport.InMemoryCoapTransport; import java.time.Duration; import java.util.concurrent.ScheduledExecutorService; import org.junit.jupiter.api.Test; @@ -55,4 +58,23 @@ public void shouldFail_whenIllegalTimeoutValue() throws Exception { CoapServer.builder().responseTimeout(Duration.ofMillis(-1)) ); } + + @Test + public void shouldReuseBuilder() throws Exception { + CoapServer server = new CoapServerBuilder() + .transport(InMemoryCoapTransport.create(5683)) + .build().start(); + + CoapServerBuilder builder = CoapServer.builder() + .transport(InMemoryCoapTransport.create()); + + + for (int i = 0; i < 3; i++) { + CoapClient client = builder.buildClient(server.getLocalSocketAddress()); + assertNotNull(client.ping().get()); + client.close(); + } + + server.stop(); + } }