Page History
...
Pekko의
AskPattern.ask메소드를 활용하고,Duration타임아웃 설정도 적용함
testActorAskByCoroutine
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testActorAskByCoroutine() = runBlocking {
// Test using Actor Ask pattern with coroutines
val actorRef = actorSystem.spawn(HelloActor.create())
var response = AskPattern.ask(
actorRef,
{ replyTo: ActorRef<HelloCommand> -> Hello("Hello", replyTo) },
Duration.ofSeconds(3),
actorSystem.scheduler()
).toCompletableFuture().await()
assertEquals(HelloResponse("Kotlin"), response)
} |
testActorAskByWebflux
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testActorAskByWebflux() {
val actorRef = actorSystem.spawn(HelloActor.create())
val result = Mono.just("Hello")
.flatMap { input ->
Mono.fromFuture(
AskPattern.ask(
actorRef,
{ replyTo: ActorRef<HelloCommand> -> Hello(input, replyTo) },
Duration.ofSeconds(3),
actorSystem.scheduler()
).toCompletableFuture()
)
}
.map { response -> (response as HelloResponse).message }
.block()
assertEquals("Kotlin", result)
} |
testActorAskByCompletableFuture
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testActorAskByCompletableFuture() {
val actorRef = actorSystem.spawn(HelloActor.create())
val future = CompletableFuture.supplyAsync { "Hello" }
.thenCompose { input ->
AskPattern.ask(
actorRef,
{ replyTo: ActorRef<HelloCommand> -> Hello(input, replyTo) },
Duration.ofSeconds(3),
actorSystem.scheduler()
).toCompletableFuture()
}
.thenApply { response -> (response as HelloResponse).message }
val result = future.get()
assertEquals("Kotlin", result)
} |
testActorAskByJavaStream
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testActorAskByJavaStream() {
val actorRef = actorSystem.spawn(HelloActor.create())
val result = listOf("Hello")
.stream()
.map { input ->
AskPattern.ask(
actorRef,
{ replyTo: ActorRef<HelloCommand> -> Hello(input, replyTo) },
Duration.ofSeconds(3),
actorSystem.scheduler()
).toCompletableFuture().join()
}
.map { response -> (response as HelloResponse).message }
.findFirst()
.orElse("")
assertEquals("Kotlin", result)
} |
testActorAskByAkkaStream
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test fun testActorAskByAkkaStream() { val actorRef = actorSystem.spawn(HelloActor.create()) val materializer = Materializer.createMaterializer(actorSystem.system()) val source = Source.single("Hello") .mapAsync(1) { input -> AskPattern.ask( actorRef, { replyTo: ActorRef<HelloCommand> -> Hello(input, replyTo) }, Duration.ofSeconds(3), actorSystem.scheduler() ).toCompletableFuture() } .map { response -> (response as HelloResponse).message } .runWith(Sink.head(), materializer) val result = source.toCompletableFuture().get() assertEquals("Kotlin", result) } |
testActorAskByAkkaStreamWithCoroutine
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testActorAskByAkkaStreamWithCoroutine() = runBlocking {
val actorRef = actorSystem.spawn(HelloActor.create())
val materializer = Materializer.createMaterializer(actorSystem.system())
val source = Source.single("Hello")
.mapAsync(1) { input ->
AskPattern.ask(
actorRef,
{ replyTo: ActorRef<HelloCommand> -> Hello(input, replyTo) },
Duration.ofSeconds(3),
actorSystem.scheduler()
).toCompletableFuture()
}
.map { response -> (response as HelloResponse).message }
.runWith(Sink.head(), materializer)
val result = source.await()
assertEquals("Kotlin", result)
} |
...
KHelloActor를 생성하고start()(코루틴 Job 시작)Channel을 통해KHello메시지를 보냄응답으로
KHelloResponse를 수신결과를 검증한 후 리소스 정리 (
close및cancel)
testKHelloActor
| Code Block | ||||
|---|---|---|---|---|
| ||||
@Test
fun testKHelloActor() = runBlocking {
// KHelloActor 생성 및 시작
val actor = KHelloActor()
val replyChannel = Channel<KHelloResponse>()
val job = actor.start() // start 메서드가 Job을 반환하도록 수정
try {
// 메시지 전송 및 응답 수신
withTimeout(3000) { // 타임아웃 설정
actor.send(KHello("Hello", replyChannel))
val response = replyChannel.receive()
// 응답 검증
assertEquals("Kotlin", response.message)
}
} finally {
// 리소스 정리
replyChannel.close()
actor.close()
job.cancel() // 액터의 코루틴 종료
}
} |
...