-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test support for Monix task - Elasticsearch
- Loading branch information
Showing
5 changed files
with
177 additions
and
285 deletions.
There are no files selected for viewing
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
98 changes: 36 additions & 62 deletions
98
elasticsearch/src/it/scala/monix/connect/elasticsearch/ElasticsearchSinkSuite.scala
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 |
---|---|---|
@@ -1,108 +1,82 @@ | ||
package monix.connect.elasticsearch | ||
|
||
import monix.eval.Task | ||
import monix.execution.Scheduler | ||
import monix.execution.Scheduler.Implicits.global | ||
import monix.reactive.Observable | ||
import monix.testing.scalatest.MonixTaskSpec | ||
import org.scalacheck.Gen | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatest.flatspec.AnyFlatSpecLike | ||
import org.scalatest.flatspec.{AnyFlatSpecLike, AsyncFlatSpec} | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.util.Try | ||
|
||
class ElasticsearchSinkSuite extends AnyFlatSpecLike with Fixture with Matchers with BeforeAndAfterEach { | ||
class ElasticsearchSinkSuite extends AsyncFlatSpec with MonixTaskSpec with Fixture with Matchers with BeforeAndAfterEach { | ||
import com.sksamuel.elastic4s.ElasticDsl._ | ||
|
||
override implicit val scheduler: Scheduler = Scheduler.io("elasticsearch-sink-suite") | ||
"ElasticsearchSink" should "execute update requests in batches" in { | ||
// given | ||
val updateRequests = Gen.listOfN(10, genUpdateRequest).sample.get | ||
|
||
// when | ||
esResource.use { es => | ||
Observable | ||
.from(updateRequests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
}.runSyncUnsafe() | ||
|
||
// then | ||
val r = | ||
Task | ||
.parSequence(updateRequests.map { request => | ||
getById(request.index.name, request.id) | ||
.map(_.sourceAsString) | ||
}) | ||
.runSyncUnsafe() | ||
r should contain theSameElementsAs updateRequests.flatMap(_.documentSource) | ||
.consumeWith(es.bulkRequestSink()) *> | ||
Task | ||
.parSequence(updateRequests.map { request => | ||
getById(request.index.name, request.id) | ||
.map(_.sourceAsString) | ||
}) | ||
}.asserting(_ should contain theSameElementsAs updateRequests.flatMap(_.documentSource)) | ||
} | ||
|
||
it should "execute delete requests in batches" in { | ||
// given | ||
val updateRequests = Gen.listOfN(10, genUpdateRequest).sample.get | ||
val deleteRequests = updateRequests.take(5).map(r => deleteById(r.index, r.id)) | ||
|
||
// when | ||
esResource.use { es => | ||
Observable | ||
.from(updateRequests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
}.runSyncUnsafe() | ||
esResource.use { es => | ||
Observable | ||
.from(deleteRequests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
}.runSyncUnsafe() | ||
// then | ||
val r = | ||
Task | ||
.parSequence(updateRequests.map { request => | ||
getById(request.index.name, request.id) | ||
.map(_.sourceAsString) | ||
}) | ||
.runSyncUnsafe() | ||
r should contain theSameElementsAs List.fill(5)("{}") ++ updateRequests.takeRight(5).flatMap(_.documentSource) | ||
.consumeWith(es.bulkRequestSink()) *> | ||
Observable | ||
.from(deleteRequests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) *> | ||
Task | ||
.parSequence(updateRequests.map { request => | ||
getById(request.index.name, request.id) | ||
.map(_.sourceAsString) | ||
}) | ||
}.asserting(_ should contain theSameElementsAs List.fill(5)("{}") ++ updateRequests.takeRight(5).flatMap(_.documentSource)) | ||
} | ||
|
||
it should "execute index requests batches" in { | ||
// given | ||
val indexRequests = Gen.listOfN(10, genIndexRequest).sample.get | ||
|
||
// when | ||
esResource.use { es => | ||
Observable | ||
.from(indexRequests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
}.runSyncUnsafe() | ||
|
||
// then | ||
val r = | ||
Task | ||
.parSequence(indexRequests.map { request => | ||
getById(request.index.name, request.id.get) | ||
.map(_.sourceAsString) | ||
}) | ||
.runSyncUnsafe() | ||
r should contain theSameElementsAs indexRequests.flatMap(_.source) | ||
.consumeWith(es.bulkRequestSink()) *> | ||
Task | ||
.parSequence(indexRequests.map { request => | ||
getById(request.index.name, request.id.get) | ||
.map(_.sourceAsString) | ||
}) | ||
}.asserting{ _ should contain theSameElementsAs indexRequests.flatMap(_.source) } | ||
} | ||
|
||
it should "fails when the es index not exists" in { | ||
// given | ||
val requests = Seq(updateById("test_index", "test_id")) | ||
|
||
// when | ||
val ob = | ||
esResource.use { es => | ||
Observable | ||
.from(requests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
} | ||
|
||
// then | ||
val r = Try(ob.runSyncUnsafe()) | ||
r.isFailure shouldBe true | ||
esResource.use { es => | ||
Observable | ||
.from(requests) | ||
.bufferTumbling(5) | ||
.consumeWith(es.bulkRequestSink()) | ||
}.attempt.asserting(_.isLeft shouldBe true) | ||
} | ||
} |
Oops, something went wrong.