EventStore / EventStoreDB-Client-Java

Official Asynchronous Java 8+ Client Library for EventStoreDB 20.6+
https://eventstore.com
Apache License 2.0
63 stars 20 forks source link

Using expected revision "any" (-2) as long parameter fails with exception #241

Closed michael-schnell closed 1 year ago

michael-schnell commented 1 year ago

Writing to the stream with "any" works fine with using "ExpectedRevision.any()":

AppendToStreamOptions options = AppendToStreamOptions.get().expectedRevision(ExpectedRevision.any());        
WriteResult writeResult = client.appendToStream("test", options, event).get();

Using the same value as "long" does not work:

AppendToStreamOptions options = AppendToStreamOptions.get().expectedRevision(-2);        
WriteResult writeResult = client.appendToStream("test", options, event).get();

It raises an exception:

Exception in thread "main" java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: UNKNOWN: Exception was thrown by handler.
    at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
    at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1999)
    at org.fuin.esc.esgrpc.example.App.main(App.java:40)
Caused by: io.grpc.StatusRuntimeException: UNKNOWN: Exception was thrown by handler.
    at io.grpc.Status.asRuntimeException(Status.java:539)
    at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:487)
    at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:576)
    at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:70)
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:757)
    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:736)
    at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
    at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

Using "-1" (noStream) as long value works fine.

YoEight commented 1 year ago

Hey @michael-schnell,

This is not a bug. You are supposed to use smart-constructor like you did in your first snippet so we populate the proper grpc data structure when communicating with the server. expectedRevision methods expects a positive integer value, anything different would either end with an exception or an unexpected behavior depending on the server version you are targeting.

If you want to get the proper ExpectedRevision instance while still passing -1 or -2, I suggest you to use ExpectedRevision.fromRawlong static method that deals with that for you.