Skip to content

Commit

Permalink
Making FailureAcknowledgement be self serializable. Closes GH-768
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Mar 13, 2024
1 parent 004f916 commit 3bccc46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Wolverine.Runtime.RemoteInvocation;
using Xunit;

namespace CoreTests.Runtime.RemoteInvocation;

public class FailureAcknowledgementTests
{
[Fact]
public void serialize_FailureAcknowledgement()
{
var ack = new FailureAcknowledgement
{
Message = "Bad",
RequestId = Guid.NewGuid()
};

var bytes = ack.Write();

var ack2 = (FailureAcknowledgement)FailureAcknowledgement.Read(bytes);

ack2.Message.ShouldBe(ack.Message);
ack2.RequestId.ShouldBe(ack.RequestId);

}
}
22 changes: 21 additions & 1 deletion src/Wolverine/Runtime/RemoteInvocation/FailureAcknowledgement.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@

using System.Diagnostics;
using System.Text;

namespace Wolverine.Runtime.RemoteInvocation;

public class FailureAcknowledgement
public class FailureAcknowledgement : ISerializable
{
public Guid RequestId { get; init; }
public string Message { get; init; } = null!;

public byte[] Write()
{
var guid = RequestId.ToByteArray();
var message = Encoding.UTF8.GetBytes(Message ?? string.Empty);
return guid.Concat(message).ToArray();
}

public static object Read(byte[] bytes)
{
var requestId = new Guid(bytes.Take(16).ToArray());
var message = Encoding.UTF8.GetString(bytes.Skip(16).ToArray());

return new FailureAcknowledgement
{
Message = message,
RequestId = requestId
};
}

protected bool Equals(FailureAcknowledgement other)
{
return Equals(RequestId, other.RequestId) && string.Equals(Message, other.Message);
Expand Down

0 comments on commit 3bccc46

Please sign in to comment.