This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Add query bus supporting more than one handler #3
Open
GMadorell
wants to merge
1
commit into
CodelyTV:master
Choose a base branch
from
GMadorell:make-query-bus-accept-multiple-queries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
4 changes: 2 additions & 2 deletions
4
src/main/scala/tv/codely/cqrs_ddd_scala_example/bus/domain/QueryHandler.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,5 +1,5 @@ | ||
package tv.codely.cqrs_ddd_scala_example.bus.domain | ||
|
||
abstract class QueryHandler[P[_], QueryType <: Query, ResponseType <: Response] { | ||
def handle(query: QueryType): P[ResponseType] | ||
abstract class QueryHandler[P[_], QueryType <: Query] { | ||
def handle(query: QueryType): P[QueryType#QueryResponse] | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/scala/tv/codely/cqrs_ddd_scala_example/types/ThrowableTypeClasses.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package tv.codely.cqrs_ddd_scala_example.types | ||
|
||
import cats.MonadError | ||
|
||
object ThrowableTypeClasses { | ||
type MonadErrorThrowable[P[_]] = MonadError[P, Throwable] | ||
|
||
object MonadErrorThrowable { | ||
def apply[P[_]](implicit E: MonadError[P, Throwable]): MonadErrorThrowable[P] = E | ||
} | ||
|
||
type EitherThrowable[A] = Either[Throwable, A] | ||
} |
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
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
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 |
---|---|---|
|
@@ -2,15 +2,20 @@ package tv.codely.cqrs_ddd_scala_example.acceptance | |
|
||
import java.util.UUID | ||
|
||
import scala.reflect.classTag | ||
import cats.implicits._ | ||
import scala.concurrent.duration._ | ||
|
||
import org.joda.time.DateTime | ||
import org.scalatest._ | ||
import org.scalatest.Matchers._ | ||
import tv.codely.cqrs_ddd_scala_example.bus.domain.QueryBus | ||
import tv.codely.cqrs_ddd_scala_example.user_greet.application.generate.{FindUserGreetQuery, FindUserGreetQueryHandler, UserGreetFinder} | ||
import tv.codely.cqrs_ddd_scala_example.user_greet.infrastructure.InSyncDelayedMemoryUserRepository | ||
import tv.codely.cqrs_ddd_scala_example.types.ThrowableTypeClasses.EitherThrowable | ||
import tv.codely.cqrs_ddd_scala_example.user_greet.application.generate.{ | ||
FindUserGreetQuery, | ||
FindUserGreetQueryHandler, | ||
UserGreetFinder | ||
} | ||
import tv.codely.cqrs_ddd_scala_example.user_greet.infrastructure.DelayedMemoryUserRepository | ||
|
||
final class SyncUserGreetFinderTest extends WordSpec with GivenWhenThen { | ||
|
||
|
@@ -19,18 +24,15 @@ final class SyncUserGreetFinderTest extends WordSpec with GivenWhenThen { | |
|
||
Given("a UserGreetGenerator with a user repository") | ||
|
||
val notableDelay = 10.seconds | ||
val userRepository = new InSyncDelayedMemoryUserRepository(notableDelay) | ||
val notableDelay = 3.seconds | ||
val userRepository = new DelayedMemoryUserRepository[EitherThrowable](notableDelay) | ||
val userGreetGeneratorWithDelay = new UserGreetFinder(userRepository) | ||
val generateUserGreetQueryHandler = new FindUserGreetQueryHandler(userGreetGeneratorWithDelay) | ||
|
||
And("an SyncQueryBus which block the execution flow until getting a response") | ||
|
||
val queryBus = new QueryBus( | ||
Map( | ||
classTag[FindUserGreetQuery] -> generateUserGreetQueryHandler | ||
) | ||
) | ||
val queryBus = new QueryBus[EitherThrowable]() | ||
queryBus.subscribe(generateUserGreetQueryHandler) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you try to refactor this requiring to inject the subscribers while creating the QueryBus instance as we where doing? This way we would be avoiding mutable state inside the If you don't have time in order to add this improvement, we could merge this PR and do that later 🙂 |
||
|
||
When("we ask the GenerateUserGreetQuery to the SyncQueryBus") | ||
|
||
|
@@ -42,13 +44,14 @@ final class SyncUserGreetFinderTest extends WordSpec with GivenWhenThen { | |
) | ||
) | ||
|
||
val greeting = queryBus.ask(query) | ||
val queryResult = queryBus.ask(query) | ||
|
||
println("Query asked to the sync query bus") | ||
|
||
Then("it should say hello to someone") | ||
|
||
greeting.greet shouldBe "Hello Rafa" | ||
queryResult.isRight shouldBe true | ||
queryResult.map(_.greet shouldBe "Hello Rafa") | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/scala/tv/codely/cqrs_ddd_scala_example/integration/QueryBusTest.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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tv.codely.cqrs_ddd_scala_example.integration | ||
|
||
import java.util.UUID | ||
|
||
import cats.Applicative | ||
import cats.syntax.applicative._ | ||
import cats.instances.either._ | ||
import org.joda.time.DateTime | ||
import org.scalatest.{Matchers, WordSpec} | ||
import tv.codely.cqrs_ddd_scala_example.bus.domain._ | ||
import tv.codely.cqrs_ddd_scala_example.types.ThrowableTypeClasses.EitherThrowable | ||
|
||
final class QueryBusTest extends WordSpec with Matchers { | ||
|
||
val queryBus = new QueryBus[EitherThrowable] | ||
queryBus.subscribe(new PingQueryHandler) | ||
queryBus.subscribe(new EchoQueryHandler) | ||
|
||
"A QueryBus" should { | ||
"return the correct ping response" in { | ||
val pingQuery = PingQuery(UUID.randomUUID(), DateTime.now()) | ||
|
||
val result = queryBus.ask(pingQuery) | ||
result.isRight shouldBe true | ||
result.map(response => response shouldBe PingResponse(pingQuery.queryId)) | ||
} | ||
|
||
"return the correct echo response" in { | ||
val echoQuery = EchoQuery("Coming soon in CodelyTV...", UUID.randomUUID(), DateTime.now()) | ||
|
||
val result = queryBus.ask(echoQuery) | ||
result.isRight shouldBe true | ||
result.map(response => response shouldBe EchoResponse(echoQuery.message, echoQuery.queryId)) | ||
} | ||
|
||
"fail when no handler exists for the asked query" in { | ||
val responseWithoutHandlers = new Response(UUID.randomUUID()) {} | ||
val queryWithoutHandlers = new Query(UUID.randomUUID(), DateTime.now()) { | ||
override type QueryResponse = responseWithoutHandlers.type | ||
} | ||
|
||
val result = queryBus.ask(queryWithoutHandlers) | ||
result.isLeft shouldBe true | ||
result.left.map(_ shouldBe QueryHandlerNotFoundForQuery) | ||
} | ||
} | ||
} | ||
|
||
private case class PingQuery(override val queryId: UUID, override val createdAt: DateTime) | ||
extends Query(queryId, createdAt) { | ||
override type QueryResponse = PingResponse | ||
} | ||
private case class PingResponse(override val requestId: UUID) extends Response(requestId) | ||
private class PingQueryHandler[P[_]: Applicative] extends QueryHandler[P, PingQuery] { | ||
override def handle(query: PingQuery): P[PingResponse] = Applicative[P].pure(PingResponse(query.queryId)) | ||
} | ||
|
||
private case class EchoQuery(message: String, override val queryId: UUID, override val createdAt: DateTime) | ||
extends Query(queryId, createdAt) { | ||
override type QueryResponse = EchoResponse | ||
} | ||
private case class EchoResponse(message: String, override val requestId: UUID) extends Response(requestId) | ||
private class EchoQueryHandler[P[_]: Applicative] extends QueryHandler[P, EchoQuery] { | ||
override def handle(query: EchoQuery): P[EchoResponse] = | ||
EchoResponse(query.message, query.queryId).pure | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't have used the types as you've done in this PR yet. Thanks for sharing!