-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract commond logic into static methods
Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
- Loading branch information
Showing
4 changed files
with
104 additions
and
32 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/utils/Shutdown.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,41 @@ | ||
package dev.knative.eventing.kafka.broker.core.utils; | ||
|
||
import dev.knative.eventing.kafka.broker.contract.DataPlaneContract.Contract; | ||
import io.vertx.core.Vertx; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Shutdown { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Shutdown.class); | ||
|
||
public static Runnable run(final Vertx vertx, final Closeable fw, final Consumer<Contract> publisher) | ||
throws IOException { | ||
return () -> { | ||
try { | ||
fw.close(); | ||
} catch (final IOException e) { | ||
logger.error("Failed to close file watcher", e); | ||
} | ||
publisher.accept(Contract.newBuilder().build()); | ||
closeSync(vertx).run(); | ||
}; | ||
} | ||
|
||
public static Runnable closeSync(final Vertx vertx) { | ||
return () -> { | ||
final var wait = new CountDownLatch(1); | ||
vertx.close(ignore -> wait.countDown()); | ||
try { | ||
wait.await(10, TimeUnit.SECONDS); | ||
} catch (InterruptedException e) { | ||
logger.error("Timeout waiting for vertx close", e); | ||
} | ||
}; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/utils/ShutdownTest.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,55 @@ | ||
package dev.knative.eventing.kafka.broker.core.utils; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import dev.knative.eventing.kafka.broker.contract.DataPlaneContract.Contract; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.function.Consumer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ShutdownTest { | ||
|
||
@Test | ||
public void run() throws IOException { | ||
final var vertx = mockVertxClose(); | ||
final var closeable = mock(Closeable.class); | ||
final Consumer<Contract> consumer = mock(Consumer.class); | ||
|
||
Shutdown.run(vertx, closeable, consumer).run(); | ||
|
||
verify(vertx, times(1)).close(any()); | ||
verify(closeable).close(); | ||
verify(consumer).accept(Contract.newBuilder().build()); | ||
} | ||
|
||
@Test | ||
public void closeSync() { | ||
final var vertx = mockVertxClose(); | ||
|
||
Shutdown.closeSync(vertx).run(); | ||
|
||
verify(vertx).close(any()); | ||
} | ||
|
||
private Vertx mockVertxClose() { | ||
final var vertx = mock(Vertx.class); | ||
|
||
doAnswer(invocation -> { | ||
final Handler<AsyncResult<Void>> callback = invocation.getArgument(0); | ||
callback.handle(Future.succeededFuture()); | ||
return null; | ||
}) | ||
.when(vertx).close(any()); | ||
|
||
return vertx; | ||
} | ||
} |
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