Make EntityRef & EntityTypeKey contravariant

This commit is contained in:
Dale Wijnand 2019-06-04 12:51:05 +01:00
parent d50674ab49
commit 6cec50f474
No known key found for this signature in database
GPG key ID: 4F256E3D151DF5EF
2 changed files with 14 additions and 3 deletions

View file

@ -312,7 +312,7 @@ object StartEntity {
*
* Not for user extension.
*/
@DoNotInherit trait EntityTypeKey[T] {
@DoNotInherit trait EntityTypeKey[-T] {
/**
* Name of the entity type.
@ -362,7 +362,7 @@ object EntityTypeKey {
* [[ActorRef]] and watch it in case such notification is desired.
* Not for user extension.
*/
@DoNotInherit trait EntityRef[M] extends RecipientRef[M] { this: InternalRecipientRef[M] =>
@DoNotInherit trait EntityRef[-M] extends RecipientRef[M] { this: InternalRecipientRef[M] =>
/**
* Send a message to the entity referenced by this EntityRef using *at-most-once*

View file

@ -65,17 +65,28 @@ class AccountExampleSpec extends ScalaTestWithActorTestKit(AccountExampleSpec.co
probe.expectMessage(Confirmed)
ref ! Withdraw(10, probe.ref)
probe.expectMessage(Confirmed)
// The same probe can be used with other commands too:
ref ! GetBalance(probe.ref)
probe.expectMessage(CurrentBalance(90))
}
"reject Withdraw overdraft" in {
// AccountCommand[_] is the command type, but it should also be possible to narrow it to
// AccountCommand[OperationResult]
val probe = createTestProbe[OperationResult]()
val ref = ClusterSharding(system).entityRefFor(AccountEntity.TypeKey, "3")
val ref = ClusterSharding(system).entityRefFor[AccountCommand[OperationResult]](AccountEntity.TypeKey, "3")
ref ! CreateAccount(probe.ref)
probe.expectMessage(Confirmed)
ref ! Deposit(100, probe.ref)
probe.expectMessage(Confirmed)
ref ! Withdraw(110, probe.ref)
probe.expectMessageType[Rejected]
// ... thus restricting the entity ref from being sent other commands, e.g.:
// val probe2 = createTestProbe[CurrentBalance]()
// val msg = GetBalance(probe2.ref)
// ref ! msg // type mismatch: GetBalance NOT =:= AccountCommand[OperationResult]
}
"handle GetBalance" in {