Skip to content

Commit

Permalink
configure surrogates for Orleans tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Dec 6, 2024
1 parent f276683 commit 680c3d0
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ public async ValueTask<RpcResponse> InvokeRequest(RpcRequest request, Cancellati
return response;
}

public ValueTask<RegisterAgentTypeResponse> RegisterAgentTypeAsync(RegisterAgentTypeRequest request)
public async ValueTask<RegisterAgentTypeResponse> RegisterAgentTypeAsync(RegisterAgentTypeRequest request)
{
throw new NotImplementedException();
return new RegisterAgentTypeResponse();
}

public ValueTask<RpcResponse> InvokeRequest(RpcRequest request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task Test_SaveState()
var service = new GrpcGatewayService(gateway);
var callContext = TestServerCallContext.Create();

var response = await service.SaveState(new AgentState { }, callContext);
var response = await service.SaveState(new AgentState { AgentId = new AgentId { Key = "", Type = "" } }, callContext);

response.Should().NotBeNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;

public sealed class ClusterFixture : IDisposable
{
public TestCluster Cluster { get; } = new TestClusterBuilder().Build();
public ClusterFixture()
{
var builder = new TestClusterBuilder();
builder.AddSiloBuilderConfigurator<SiloBuilderConfigurator>();
Cluster = builder.Build();
Cluster.Deploy();

public ClusterFixture() => Cluster.Deploy();
}
public TestCluster Cluster { get; }

void IDisposable.Dispose() => Cluster.StopAllSilos();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SiloBuilderConfigurator.cs

using Orleans.Serialization;
using Orleans.TestingHost;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;

public class SiloBuilderConfigurator : ISiloConfigurator
{
public void Configure(ISiloBuilder siloBuilder)
{
siloBuilder.ConfigureServices(services =>
{
services.AddSerializer(a=> a.AddProtobufSerializer());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// AgentIdSurrogate.cs
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct AgentIdSurrogate
{
[Id(0)]
public string Key;
[Id(1)]
public string Type;
}

[RegisterConverter]
public sealed class AgentIdSurrogateConverter :
IConverter<AgentId, AgentIdSurrogate>
{
public AgentId ConvertFromSurrogate(
in AgentIdSurrogate surrogate) =>
new AgentId
{
Key = surrogate.Key,
Type = surrogate.Type
};

public AgentIdSurrogate ConvertToSurrogate(
in AgentId value) =>
new AgentIdSurrogate
{
Key = value.Key,
Type = value.Type
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// AgentStateSurrogate.cs

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct AgentStateSurrogate
{
[Id(0)]
public string Id;
[Id(1)]
public string TextData;
[Id(2)]
public ByteString BinaryData;
[Id(3)]
public AgentId AgentId;
[Id(4)]
public string Etag;
[Id(5)]
public Any ProtoData;
}

[RegisterConverter]
public sealed class AgentStateSurrogateConverter :
IConverter<AgentState, AgentStateSurrogate>
{
public AgentState ConvertFromSurrogate(
in AgentStateSurrogate surrogate) =>
new AgentState
{
TextData = surrogate.TextData,
BinaryData = surrogate.BinaryData,
AgentId = surrogate.AgentId,
ProtoData = surrogate.ProtoData,
ETag = surrogate.Etag
};

public AgentStateSurrogate ConvertToSurrogate(
in AgentState value) =>
new AgentStateSurrogate
{
AgentId = value.AgentId,
BinaryData = value.BinaryData,
TextData = value.TextData,
Etag = value.ETag,
ProtoData = value.ProtoData
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// CloudEventSurrogate.cs

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

// TODO: Add the rest of the properties
[GenerateSerializer]
public struct CloudEventSurrogate
{
[Id(0)]
public string Id;
[Id(1)]
public string TextData;
[Id(2)]
public ByteString BinaryData;
[Id(3)]
public Any ProtoData;
}

[RegisterConverter]
public sealed class CloudEventSurrogateConverter :
IConverter<CloudEvent, CloudEventSurrogate>
{
public CloudEvent ConvertFromSurrogate(
in CloudEventSurrogate surrogate) =>
new CloudEvent
{
TextData = surrogate.TextData,
BinaryData = surrogate.BinaryData ,
Id = surrogate.Id
};

public CloudEventSurrogate ConvertToSurrogate(
in CloudEvent value) =>
new CloudEventSurrogate
{
TextData = value.TextData,
BinaryData = value.BinaryData,
Id = value.Id,
ProtoData = value.ProtoData
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// RegisterAgentTypeRequestSurrogate.cs

using Google.Protobuf.Collections;
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct RegisterAgentTypeRequestSurrogate
{
[Id(0)]
public string RequestId;
[Id(1)]
public string Type;
[Id(2)]
public RepeatedField<string> Events;
}

[RegisterConverter]
public sealed class RegisterAgentTypeRequestSurrogateConverter :
IConverter<RegisterAgentTypeRequest, RegisterAgentTypeRequestSurrogate>
{
public RegisterAgentTypeRequest ConvertFromSurrogate(
in RegisterAgentTypeRequestSurrogate surrogate) =>
new RegisterAgentTypeRequest()
{
RequestId = surrogate.RequestId,
Type = surrogate.Type,
// TODO : Map Events
};

public RegisterAgentTypeRequestSurrogate ConvertToSurrogate(
in RegisterAgentTypeRequest value) =>
new RegisterAgentTypeRequestSurrogate
{
RequestId = value.RequestId,
Type = value.Type,
Events = value.Events
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// RegisterAgentTypeResponseSurrogate.cs

using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct RegisterAgentTypeResponseSurrogate
{
[Id(0)]
public string RequestId;
[Id(1)]
public bool Success;
[Id(2)]
public string Error;
}

[RegisterConverter]
public sealed class RegisterAgentTypeResponseSurrogateConverter :
IConverter<RegisterAgentTypeResponse, RegisterAgentTypeResponseSurrogate>
{
public RegisterAgentTypeResponse ConvertFromSurrogate(
in RegisterAgentTypeResponseSurrogate surrogate) =>
new RegisterAgentTypeResponse
{
RequestId = surrogate.RequestId,
Success = surrogate.Success,
Error = surrogate.Error
};

public RegisterAgentTypeResponseSurrogate ConvertToSurrogate(
in RegisterAgentTypeResponse value) =>
new RegisterAgentTypeResponseSurrogate
{
RequestId = value.RequestId,
Success = value.Success,
Error = value.Error
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// RpcRequestSurrogate.cs

using Google.Protobuf.Collections;
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct RpcRequestSurrogate
{
[Id(0)]
public string RequestId;
[Id(1)]
public AgentId Source;
[Id(2)]
public AgentId Target;
[Id(3)]
public string Method;
[Id(4)]
public Payload Payload;
[Id(5)]
public MapField<string, string> Metadata;
}

[RegisterConverter]
public sealed class RpcRequestSurrogateConverter :
IConverter<RpcRequest, RpcRequestSurrogate>
{
public RpcRequest ConvertFromSurrogate(
in RpcRequestSurrogate surrogate) =>
new RpcRequest
{
RequestId = surrogate.RequestId,
Source = surrogate.Source,
Target = surrogate.Target,
Method = surrogate.Method,
Payload = surrogate.Payload,
// TODO: Add Metadata Metadata = surrogate.Metadata
};

public RpcRequestSurrogate ConvertToSurrogate(
in RpcRequest value) =>
new RpcRequestSurrogate
{
RequestId = value.RequestId,
Source = value.Source,
Target = value.Target,
Method = value.Method,
Payload = value.Payload,
Metadata = value.Metadata
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// RpcResponseSurrogate.cs

using Google.Protobuf.Collections;
using Microsoft.AutoGen.Abstractions;

namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans.Surrogates;

[GenerateSerializer]
public struct RpcResponseSurrogate
{
[Id(0)]
public string RequestId;
[Id(1)]
public Payload Payload;
[Id(2)]
public string Error;
[Id(3)]
public MapField<string, string> Metadata;
}

[RegisterConverter]
public sealed class RpcResponseurrogateConverter :
IConverter<RpcResponse, RpcResponseSurrogate>
{
public RpcResponse ConvertFromSurrogate(
in RpcResponseSurrogate surrogate) =>
new RpcResponse
{
RequestId = surrogate.RequestId,
Payload = surrogate.Payload,
Error = surrogate.Error,
// TODO: Add Metadata = value.Metadata
};

public RpcResponseSurrogate ConvertToSurrogate(
in RpcResponse value) =>
new RpcResponseSurrogate
{
RequestId = value.RequestId,
Payload = value.Payload,
Error = value.Error,
Metadata = value.Metadata
};
}

0 comments on commit 680c3d0

Please sign in to comment.