-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #486 from MAIF/fix-cacheablequeue
Improve cacheable queue (#472)
- Loading branch information
Showing
2 changed files
with
115 additions
and
22 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
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,86 @@ | ||
package libs.streams | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.{Materializer, OverflowStrategy} | ||
import akka.stream.scaladsl.{Sink, Source} | ||
import org.scalatest.{MustMatchers, OptionValues, WordSpec} | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
import scala.util.{Failure, Try} | ||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.{Await, Future} | ||
|
||
class CacheableQueueTest extends WordSpec with MustMatchers with OptionValues { | ||
|
||
implicit val system = ActorSystem() | ||
implicit val mat = Materializer(system) | ||
import system.dispatcher | ||
|
||
"Cacheable queue" must { | ||
|
||
"handle heavy concurent publish fail with backpressure strategy" in { | ||
val queue = CacheableQueue[String](500, queueBufferSize = 500) | ||
|
||
val hasFailed = new AtomicBoolean(false) | ||
|
||
val allOffer = Future.sequence((0 to 700).map { i => | ||
val result = queue.offer(s"message-$i") | ||
result.onComplete { | ||
case Failure(e) => hasFailed.set(true) | ||
case _ => | ||
} | ||
result | ||
}) | ||
|
||
Try { | ||
Await.result(allOffer, 20.seconds) | ||
} | ||
|
||
// Fail because too many concurrent offer | ||
hasFailed.get() mustBe true | ||
} | ||
|
||
"handle heavy publish with consumer" in { | ||
val queue = CacheableQueue[String](500, queueBufferSize = 500) | ||
|
||
queue.rawSource.runWith(Sink.ignore) | ||
queue.sourceWithCache.runWith(Sink.ignore) | ||
|
||
val hasFailed = new AtomicBoolean(false) | ||
|
||
val allOffer = Source((0 to 1000).toList).mapAsync(1) { i => | ||
val result = queue.offer(s"message-$i") | ||
result.onComplete { | ||
case Failure(e) => hasFailed.set(true) | ||
case _ => | ||
} | ||
result | ||
} | ||
|
||
Await.result(allOffer.runWith(Sink.ignore), 20.seconds) | ||
|
||
hasFailed.get() mustBe false | ||
} | ||
|
||
"handle normmal publish without consumer" in { | ||
val queue = CacheableQueue[String](500, queueBufferSize = 500) | ||
|
||
val hasFailed = new AtomicBoolean(false) | ||
|
||
val allOffer = Source((0 to 1000).toList).mapAsync(1) { i => | ||
val result = queue.offer(s"message-$i") | ||
result.onComplete { | ||
case Failure(e) => hasFailed.set(true) | ||
case _ => | ||
} | ||
result | ||
} | ||
|
||
Await.result(allOffer.runWith(Sink.ignore), 20.seconds) | ||
|
||
hasFailed.get() mustBe false | ||
} | ||
|
||
} | ||
|
||
} |