From ff8ea23a5b25677fedf7160002521abd9e832788 Mon Sep 17 00:00:00 2001 From: Torsten Krohn Date: Thu, 29 Sep 2022 17:12:26 +0200 Subject: [PATCH] perf: run live-event call asynchronously --- .../LiveEventNotificationService.java | 2 + .../config/apiclient/SpringAsyncConfig.java | 10 +++++ .../LiveEventNotificationServiceIT.java | 38 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/main/java/de/caritas/cob/videoservice/config/apiclient/SpringAsyncConfig.java create mode 100644 src/test/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationServiceIT.java diff --git a/src/main/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationService.java b/src/main/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationService.java index e372699..50102b3 100644 --- a/src/main/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationService.java +++ b/src/main/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationService.java @@ -7,6 +7,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** @@ -26,6 +27,7 @@ public class LiveEventNotificationService { * @param liveEventMessage {@link LiveEventMessage} * @param userIds list of receiver user Ids */ + @Async public void sendVideoCallRequestLiveEvent(LiveEventMessage liveEventMessage, List userIds) { await(); diff --git a/src/main/java/de/caritas/cob/videoservice/config/apiclient/SpringAsyncConfig.java b/src/main/java/de/caritas/cob/videoservice/config/apiclient/SpringAsyncConfig.java new file mode 100644 index 0000000..7523b93 --- /dev/null +++ b/src/main/java/de/caritas/cob/videoservice/config/apiclient/SpringAsyncConfig.java @@ -0,0 +1,10 @@ +package de.caritas.cob.videoservice.config.apiclient; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; + +@Configuration +@EnableAsync +public class SpringAsyncConfig { + +} diff --git a/src/test/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationServiceIT.java b/src/test/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationServiceIT.java new file mode 100644 index 0000000..791756c --- /dev/null +++ b/src/test/java/de/caritas/cob/videoservice/api/service/liveevent/LiveEventNotificationServiceIT.java @@ -0,0 +1,38 @@ +package de.caritas.cob.videoservice.api.service.liveevent; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import de.caritas.cob.videoservice.liveservice.generated.web.LiveControllerApi; +import de.caritas.cob.videoservice.liveservice.generated.web.model.LiveEventMessage; +import java.lang.management.ManagementFactory; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; + +@SpringBootTest +@ActiveProfiles("testing") +@DirtiesContext +public class LiveEventNotificationServiceIT { + + @Autowired + private LiveEventNotificationService underTest; + + @MockBean + @Qualifier("liveControllerApi") + @SuppressWarnings("unused") + private LiveControllerApi liveControllerApi; + + @Test + void sendVideoCallRequestLiveEventShouldRunInAnotherThread() { + var threadCount = ManagementFactory.getThreadMXBean().getThreadCount(); + + underTest.sendVideoCallRequestLiveEvent(new LiveEventMessage(), List.of()); + + assertEquals(threadCount + 1, ManagementFactory.getThreadMXBean().getThreadCount()); + } +}