Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failed Future when invoking ES endpoint in Source #2739

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
.invoke(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body"))
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
} else {
log.debug("Fetching next scroll")

Expand Down Expand Up @@ -198,6 +201,9 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
.invoke(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body"))
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
}
} catch {
case ex: Exception => failureHandler.invoke(ex)
Expand Down Expand Up @@ -317,6 +323,9 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
.invoke(Failure(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body")))
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.stream.alpakka.elasticsearch.impl

import akka.actor.ActorSystem
import akka.http.scaladsl.{Http, HttpExt}
import akka.stream.Materializer
import akka.stream.alpakka.elasticsearch._
import akka.stream.alpakka.testkit.scaladsl.LogCapturing
import akka.stream.scaladsl.{Keep, Source}
import akka.stream.testkit.scaladsl.TestSink
import akka.testkit.TestKit
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.ExecutionContext.Implicits.global

class ElasticsearchSourcStageTest
extends TestKit(ActorSystem("elasticsearchSourceStagetest"))
with AnyWordSpecLike
with BeforeAndAfterAll
with LogCapturing {

implicit val mat: Materializer = Materializer(system)
implicit val http: HttpExt = Http()

"ElasticsearchSourceStage" when {
"client cannot connect to ES" should {
"stop the stream" in {
val downstream = Source
.fromGraph(
new impl.ElasticsearchSourceStage[String](
ElasticsearchParams.V7("es-simple-flow-index"),
Map("query" -> """{ "match_all":{}}"""),
ElasticsearchSourceSettings(ElasticsearchConnectionSettings("http://wololo:9202")),
(json: String) => ScrollResponse(Some(json), None)
)
)
.toMat(TestSink.probe)(Keep.right)
.run()

downstream.request(1)
downstream.expectError()
}
}
}

override def afterAll(): Unit = {
super.afterAll()
TestKit.shutdownActorSystem(system)
}
}