Skip to content

Commit

Permalink
Java support passes the TCK
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorklang committed Aug 22, 2019
1 parent 1d3c8ee commit 4d6596c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ final class CloudStateRunner private[this](_system: ActorSystem, services: Map[S
configuration.userFunctionInterface,
configuration.userFunctionPort,
HttpConnectionContext(UseHttp2.Always))
FutureConverters.toJava(serverBindingFuture).thenCompose(_ => system.getWhenTerminated).thenApply(_ => Done)
// FIXME Register an onTerminate callback to unbind the Http server
FutureConverters.
toJava(serverBindingFuture).
thenCompose(
binding => system.getWhenTerminated.thenCompose(_ => FutureConverters.toJava(binding.unbind()))
).thenApply(_ => Done)
}

def terminate(): CompletionStage[Done] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ final class EventSourcedImpl(_system: ActorSystem, _services: Map[String, EventS
} finally {
context.deactivate() // Very important!
}
val clientAction = context.createClientAction(reply, false)

val clientAction = context.createClientAction(reply, false)

if (!context.hasError) {
val endSequenceNumber = sequence + context.events.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public final static void main(String[] args) throws Exception {
new CloudState().
registerEventSourcedEntity(
ShoppingCartEntity.class,
Shoppingcart.getDescriptor().findServiceByName("ShoppingCart")
Shoppingcart.getDescriptor().findServiceByName("ShoppingCart"),
com.example.shoppingcart.persistence.Domain.getDescriptor()
).start().toCompletableFuture().get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public Shoppingcart.Cart getCart() {

@CommandHandler
public Empty addItem(Shoppingcart.AddLineItem item, CommandContext ctx) {
if (item.getQuantity() == 0) {
ctx.fail("Cannot add quantity of zero of an item");
if (item.getQuantity() <= 0) {
ctx.fail("Cannot add negative quantity of to item" + item.getProductId());
}
ctx.emit(Domain.ItemAdded.newBuilder().setItem(
Domain.LineItem.newBuilder()
.setProductId(item.getProductId())
.setName(item.getName())
.setQuantity(item.getQuantity())
.build()
));
).build());
return Empty.getDefaultInstance();
}

Expand All @@ -84,7 +84,7 @@ public Empty removeItem(Shoppingcart.RemoveLineItem item, CommandContext ctx) {
if (!cart.containsKey(item.getProductId())) {
ctx.fail("Cannot remove item " + item.getProductId() + " because it is not in the cart.");
}
ctx.emit(Domain.ItemRemoved.newBuilder().setProductId(item.getProductId()));
ctx.emit(Domain.ItemRemoved.newBuilder().setProductId(item.getProductId()).build());
return Empty.getDefaultInstance();
}

Expand Down
26 changes: 14 additions & 12 deletions tck/src/main/scala/io/cloudstate/tck/CloudStateTCK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import com.google.protobuf.empty.Empty
import io.cloudstate.protocol.event_sourced.{EventSourced, EventSourcedClient, EventSourcedHandler, EventSourcedInit, EventSourcedReply, EventSourcedStreamIn, EventSourcedStreamOut}

object CloudStateTCK {
private[this] final val PROXY = "proxy"
private[this] final val PROXY = "proxy"
private[this] final val FRONTEND = "frontend"
private[this] final val TCK = "tck"
private[this] final val HOSTNAME = "hostname"
Expand Down Expand Up @@ -291,17 +291,19 @@ class CloudStateTCK(private[this] final val config: CloudStateTCK.Configuration)
}

final def fromFrontend_expectFailure(within: FiniteDuration): Failure = {
val failure = eventSourcedFromFrontend.expectMsgType[EventSourcedStreamOut](noWait)
val failure = eventSourcedFromFrontend.expectMsgType[EventSourcedStreamOut](noWait) // FIXME Expects entity.Failure, but gets lientAction.Action.Failure(Failure(commandId, msg)))
failure must not be(null)
failure.message must be('failure)
failure.message.failure must be(defined)
failure.message.failure.get
failure.message must be('reply)
failure.message.reply must be(defined)
failure.message.reply.get.clientAction must be(defined)
val clientAction = failure.message.reply.get.clientAction.get
clientAction.action must be('failure)
clientAction.action.failure must be('defined)
clientAction.action.failure.get
}

final def correlate(cmd: Command, reply: EventSourcedReply) = cmd.id must be(reply.commandId)
final def correlate(cmd: Command, failure: Failure) = cmd.id must be(failure.commandId)
final def unrelated(cmd: Command, reply: EventSourcedReply) = cmd.id must not be reply.commandId
final def unrelated(cmd: Command, failure: Failure) = cmd.id must not be failure.commandId
final def correlate(cmd: Command, commandId: Long) = cmd.id must be(commandId)
final def unrelated(cmd: Command, commandId: Long) = cmd.id must not be commandId

("The TCK for" + config.name) must {
implicit val scheduler = system.scheduler
Expand Down Expand Up @@ -329,7 +331,7 @@ class CloudStateTCK(private[this] final val config: CloudStateTCK.Configuration)

fromBackend_expectInit(noWait)

correlate(fromBackend_expectCommand(noWait), fromFrontend_expectReply(events = 0, noWait))
correlate(fromBackend_expectCommand(noWait), fromFrontend_expectReply(events = 0, noWait).commandId)

eventSourcedFromBackend.expectNoMsg(noWait)
eventSourcedFromFrontend.expectNoMsg(noWait)
Expand Down Expand Up @@ -373,9 +375,9 @@ class CloudStateTCK(private[this] final val config: CloudStateTCK.Configuration)
foldLeft(Set.empty[Long]){ case (set, (isReply, eventCount)) =>
val cmd = fromBackend_expectCommand(noWait)
if (isReply)
correlate(cmd, fromFrontend_expectReply(events = eventCount, noWait)) // Verify correlation
correlate(cmd, fromFrontend_expectReply(events = eventCount, noWait).commandId) // Verify correlation
else
correlate(cmd, fromFrontend_expectFailure(noWait)) // Verify correlation
correlate(cmd, fromFrontend_expectFailure(noWait).commandId) // Verify correlation
init.entityId must be(cmd.entityId)
set must not contain(cmd.id)
set + cmd.id
Expand Down

0 comments on commit 4d6596c

Please sign in to comment.