EventStore / EventStore-Client-Dotnet

Dotnet Client SDK for the Event Store gRPC Client API written in C#
Other
147 stars 38 forks source link

Exception calling AppendToStreamAsync with duplicate event #247

Open StevenBlair123 opened 1 year ago

StevenBlair123 commented 1 year ago

Describe the bug After writing an event to the database, we truncate the stream, then attempt to write the same event to the stream. An exception is thrown on the second AppendToStreamAsync call.

To Reproduce Steps to reproduce the behavior:

  1. Write Event to Stream1
  2. Delete the Stream
  3. Write Event to Stream1

Expected behavior No Error should be thrown.

Actual behavior Exception is thrown:

Grpc.Core.RpcException : Status(StatusCode="Unknown", Detail="Exception was thrown by handler.")

Config/Logs/Screenshots If applicable, please attach your node configuration, logs or any screenshots.

EventStore details

Additional context

I have included my unit test incase it's something in the test that wrong:

 [Test]
    public async Task truncated_stream_can_be_written_to(){
        String eventStoreConnectionString = ConfigurationReader.GetValue(Constants.KeyNameEventStoreConnectionString);
        EventStoreClientSettings eventStoreClientSettings = EventStoreClientSettings.Create(eventStoreConnectionString);

        eventStoreClientSettings.OperationOptions = new EventStoreClientOperationOptions(){
                                                                                              ThrowOnAppendFailure = false
                                                                                          };

        EventStoreClient eventStoreClient = new(eventStoreClientSettings);
        String streamName = $"TestStream{Guid.NewGuid():N}";

        EventData GetEvent(){
            var @event1 = new{
                                 EventId = Guid.Parse("da3a18fe-74ae-4751-b97e-fd711fef260c"),
                                 Id = 1,
                                 Type = "TestEvent"
                             };

            Byte[] bytes = Encoding.Default.GetBytes(JsonConvert.SerializeObject(@event1));
            return   new(Uuid.FromGuid(@event1.EventId), @event1.Type, (ReadOnlyMemory<Byte>)bytes);
        }

        await eventStoreClient.AppendToStreamAsync(streamName, StreamState.NoStream, new []{ GetEvent() });

        //Truncate the stream
        DeleteResult deleteResult = await eventStoreClient.DeleteAsync(streamName, StreamState.StreamExists);

        //Get the events
        EventStoreClient.ReadStreamResult result = eventStoreClient.ReadStreamAsync(Direction.Backwards, streamName, StreamPosition.Start);

        ReadState readState = await result.ReadState;

        readState.ShouldBe(ReadState.StreamNotFound);

        IWriteResult r = await eventStoreClient.AppendToStreamAsync(streamName, 
                                                                    StreamState.Any, 
                                                                    new[] { GetEvent() },
                                                                    ConfigureOperationOptions);
    }
alexeyzimarev commented 1 year ago

Have you seen anything on the server side? From the error message I'd expect an errors in the server logs.

StevenBlair123 commented 1 year ago

[ 1,12,15:45:20.256,DBG] IDEMPOTENT WRITE TO STREAM ClientCorrelationID c1321167-af19-4c91-a983-e2e7c5181757, "EventStreamId: TestStream1617ff842b064cbc98023c9a4e17f2a5, CorrelationId: c6d3dea6-9765-4d38-9576-f2fd38ea81ea, FirstEventNumber: 0, LastEventNumber: 0".