-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockDb.scala
42 lines (28 loc) · 1.09 KB
/
MockDb.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.svend.demo.web
import akka.actor.typed.scaladsl._
import akka.actor.typed.{ActorRef, Behavior}
import scala.concurrent.duration._
/**
* Basic Mock DB: receive async query request for <pageSize> rows and sends back a generated page 500ms later.
*
* This is the main back-end of the v1 solution.
* It is also the backend behind the buffer of the v2 solutieon
**/
object MockDb {
trait DbProtocol
case class DbQuery(fromRowId: Long, pageSize: Long, replyTo: ActorRef[Rows]) extends DbProtocol
case class ResponseFromDb(rows: List[String], replyTo: ActorRef[Rows]) extends DbProtocol
case class Rows(rows: List[String])
def apply(): Behavior[DbProtocol] = Behaviors.receive {
case (ctx, DbQuery(fromRowId, pageSize, replyTo)) =>
// simulate a DB delay of 500ms
ctx.scheduleOnce(
500.milliseconds,
ctx.self,
ResponseFromDb(List.range(fromRowId, fromRowId + pageSize).map(num => s"this is db row $num"), replyTo))
Behaviors.same
case (ctx, ResponseFromDb(rows, replyTo)) =>
replyTo ! Rows(rows)
Behaviors.same
}
}