Skip to content

Commit

Permalink
feat: wrap and unwrap logging transport
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Sep 9, 2024
1 parent 6fcf6f3 commit 0860a5d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public CoapServerBuilder transportLogging(boolean isTransportLoggingEnabled) {

public CoapServer build() {
CoapTransport realTransport = requireNonNull(this.coapTransport.get(), "Missing transport");
CoapTransport coapTransport = isTransportLoggingEnabled ? new LoggingCoapTransport(realTransport) : realTransport;
CoapTransport coapTransport = isTransportLoggingEnabled ? LoggingCoapTransport.wrap(realTransport) : realTransport;
final boolean stopExecutor = scheduledExecutorService == null;
final ScheduledExecutorService effectiveExecutorService = scheduledExecutorService != null ? scheduledExecutorService : Executors.newSingleThreadScheduledExecutor();
Timer timer = toTimer(effectiveExecutorService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public class LoggingCoapTransport implements CoapTransport {

private final CoapTransport transport;

public LoggingCoapTransport(CoapTransport transport) {
this.transport = transport;
public static LoggingCoapTransport wrap(CoapTransport transport) {
if (transport instanceof LoggingCoapTransport) {
return ((LoggingCoapTransport) transport);
}
return new LoggingCoapTransport(transport);
}

public static CoapTransport unwrap(CoapTransport transport) {
Expand All @@ -38,6 +41,10 @@ public static CoapTransport unwrap(CoapTransport transport) {
return transport;
}

private LoggingCoapTransport(CoapTransport transport) {
this.transport = transport;
}

public CoapTransport getTransport() {
return transport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ class LoggingCoapTransportTest {
@Test
void shouldUnwrapTransport() {
CoapTransport transport = Mockito.mock(CoapTransport.class);
LoggingCoapTransport loggingCoapTransport = new LoggingCoapTransport(transport);
LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport);

assertEquals(transport, LoggingCoapTransport.unwrap(loggingCoapTransport));
assertEquals(transport, LoggingCoapTransport.unwrap(transport));
}

@Test
void shouldNotWrapTransportWhenAlreadyLoggingCoapTransport() {
CoapTransport transport = Mockito.mock(CoapTransport.class);
LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport);

// when
LoggingCoapTransport wrapped = LoggingCoapTransport.wrap(loggingCoapTransport);

// then
assertEquals(loggingCoapTransport, wrapped);
assertEquals(transport, loggingCoapTransport.getTransport());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public CoapClient buildClient(InetSocketAddress target) throws IOException {
}

public CoapServer build() {
Service<CoapPacket, Boolean> sender = (isTransportLoggingEnabled ? new LoggingCoapTransport(coapTransport) : coapTransport)::sendPacket;
Service<CoapPacket, Boolean> sender = (isTransportLoggingEnabled ? LoggingCoapTransport.wrap(coapTransport) : coapTransport)::sendPacket;

// NOTIFICATION
Service<SeparateResponse, Boolean> sendNotification = new NotificationValidator()
Expand Down

0 comments on commit 0860a5d

Please sign in to comment.