Skip to content

Commit f276683

Browse files
committed
grpc tests WIP
1 parent 5e53cf8 commit f276683

File tree

9 files changed

+292
-5
lines changed

9 files changed

+292
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,5 @@ samples/apps/autogen-studio/autogenstudio/models/test/
195195
notebook/coding
196196

197197
# dotnet artifacts
198-
artifacts
198+
artifacts
199+
/dotnet/.NCrunch_AutoGen/StoredText

dotnet/src/Microsoft.AutoGen/Microsoft.AutoGen.Runtime.Grpc/GrpcGatewayService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Microsoft.AutoGen.Runtime.Grpc;
88

99
// gRPC service which handles communication between the agent worker and the cluster.
10-
internal sealed class GrpcGatewayService : AgentRpc.AgentRpcBase
10+
public sealed class GrpcGatewayService : AgentRpc.AgentRpcBase
1111
{
1212
private readonly GrpcGateway Gateway;
1313
public GrpcGatewayService(GrpcGateway gateway)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// GrpcGatewayServiceTests.cs
3+
4+
using FluentAssertions;
5+
using Microsoft.AutoGen.Abstractions;
6+
using Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
7+
using Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;
8+
using Microsoft.Extensions.Logging;
9+
using Moq;
10+
11+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests;
12+
[Collection(ClusterCollection.Name)]
13+
public class GrpcGatewayServiceTests
14+
{
15+
private readonly ClusterFixture _fixture;
16+
17+
public GrpcGatewayServiceTests(ClusterFixture fixture)
18+
{
19+
_fixture = fixture;
20+
}
21+
// Test broadcast Event
22+
[Fact]
23+
public async Task Test_OpenChannel()
24+
{
25+
var logger = Mock.Of<ILogger<GrpcGateway>>();
26+
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
27+
var service = new GrpcGatewayService(gateway);
28+
var callContext = TestServerCallContext.Create();
29+
var requestStream = new TestAsyncStreamReader<Message>(callContext);
30+
var responseStream = new TestServerStreamWriter<Message>(callContext);
31+
32+
await service.OpenChannel(requestStream, responseStream, callContext);
33+
34+
requestStream.AddMessage(new Message { });
35+
36+
requestStream.Complete();
37+
38+
responseStream.Complete();
39+
40+
var responseMessage = await responseStream.ReadNextAsync();
41+
responseMessage.Should().NotBeNull();
42+
}
43+
44+
[Fact]
45+
public async Task Test_SaveState()
46+
{
47+
var logger = Mock.Of<ILogger<GrpcGateway>>();
48+
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
49+
var service = new GrpcGatewayService(gateway);
50+
var callContext = TestServerCallContext.Create();
51+
52+
var response = await service.SaveState(new AgentState { }, callContext);
53+
54+
response.Should().NotBeNull();
55+
}
56+
57+
[Fact]
58+
public async Task Test_GetState()
59+
{
60+
var logger = Mock.Of<ILogger<GrpcGateway>>();
61+
var gateway = new GrpcGateway(_fixture.Cluster.Client, logger);
62+
var service = new GrpcGatewayService(gateway);
63+
var callContext = TestServerCallContext.Create();
64+
65+
var response = await service.GetState(new AgentId { }, callContext);
66+
67+
response.Should().NotBeNull();
68+
}
69+
}

dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/GrpcGatewayTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// GrpcGatewayTests.cs
33

44
using Microsoft.AutoGen.Abstractions;
5-
using Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers;
5+
using Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;
66
using Microsoft.Extensions.Logging;
77
using Moq;
88

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma warning disable IDE0073
2+
// Copyright 2019 The gRPC Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System.Threading.Channels;
17+
using Grpc.Core;
18+
19+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
20+
21+
public class TestAsyncStreamReader<T> : IAsyncStreamReader<T> where T : class
22+
{
23+
private readonly Channel<T> _channel;
24+
private readonly ServerCallContext _serverCallContext;
25+
26+
public T Current { get; private set; } = null!;
27+
28+
public TestAsyncStreamReader(ServerCallContext serverCallContext)
29+
{
30+
_channel = Channel.CreateUnbounded<T>();
31+
_serverCallContext = serverCallContext;
32+
}
33+
34+
public void AddMessage(T message)
35+
{
36+
if (!_channel.Writer.TryWrite(message))
37+
{
38+
throw new InvalidOperationException("Unable to write message.");
39+
}
40+
}
41+
42+
public void Complete()
43+
{
44+
_channel.Writer.Complete();
45+
}
46+
47+
public async Task<bool> MoveNext(CancellationToken cancellationToken)
48+
{
49+
_serverCallContext.CancellationToken.ThrowIfCancellationRequested();
50+
51+
if (await _channel.Reader.WaitToReadAsync(cancellationToken) &&
52+
_channel.Reader.TryRead(out var message))
53+
{
54+
Current = message;
55+
return true;
56+
}
57+
else
58+
{
59+
Current = null!;
60+
return false;
61+
}
62+
}
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma warning disable IDE0073
2+
// Copyright 2019 The gRPC Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using Grpc.Core;
17+
18+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
19+
20+
public class TestServerCallContext : ServerCallContext
21+
{
22+
private readonly Metadata _requestHeaders;
23+
private readonly CancellationToken _cancellationToken;
24+
private readonly Metadata _responseTrailers;
25+
private readonly AuthContext _authContext;
26+
private readonly Dictionary<object, object> _userState;
27+
private WriteOptions? _writeOptions;
28+
29+
public Metadata? ResponseHeaders { get; private set; }
30+
31+
private TestServerCallContext(Metadata requestHeaders, CancellationToken cancellationToken)
32+
{
33+
_requestHeaders = requestHeaders;
34+
_cancellationToken = cancellationToken;
35+
_responseTrailers = new Metadata();
36+
_authContext = new AuthContext(string.Empty, new Dictionary<string, List<AuthProperty>>());
37+
_userState = new Dictionary<object, object>();
38+
}
39+
40+
protected override string MethodCore => "MethodName";
41+
protected override string HostCore => "HostName";
42+
protected override string PeerCore => "PeerName";
43+
protected override DateTime DeadlineCore { get; }
44+
protected override Metadata RequestHeadersCore => _requestHeaders;
45+
protected override CancellationToken CancellationTokenCore => _cancellationToken;
46+
protected override Metadata ResponseTrailersCore => _responseTrailers;
47+
protected override Status StatusCore { get; set; }
48+
protected override WriteOptions? WriteOptionsCore { get => _writeOptions; set { _writeOptions = value; } }
49+
protected override AuthContext AuthContextCore => _authContext;
50+
51+
protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions? options)
52+
{
53+
throw new NotImplementedException();
54+
}
55+
56+
protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders)
57+
{
58+
if (ResponseHeaders != null)
59+
{
60+
throw new InvalidOperationException("Response headers have already been written.");
61+
}
62+
63+
ResponseHeaders = responseHeaders;
64+
return Task.CompletedTask;
65+
}
66+
67+
protected override IDictionary<object, object> UserStateCore => _userState;
68+
69+
public static TestServerCallContext Create(Metadata? requestHeaders = null, CancellationToken cancellationToken = default)
70+
{
71+
return new TestServerCallContext(requestHeaders ?? new Metadata(), cancellationToken);
72+
}
73+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#pragma warning disable IDE0073
2+
// Copyright 2019 The gRPC Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
using System.Threading.Channels;
17+
using Grpc.Core;
18+
19+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Grpc;
20+
21+
public class TestServerStreamWriter<T> : IServerStreamWriter<T> where T : class
22+
{
23+
private readonly ServerCallContext _serverCallContext;
24+
private readonly Channel<T> _channel;
25+
26+
public WriteOptions? WriteOptions { get; set; }
27+
28+
public TestServerStreamWriter(ServerCallContext serverCallContext)
29+
{
30+
_channel = Channel.CreateUnbounded<T>();
31+
32+
_serverCallContext = serverCallContext;
33+
}
34+
35+
public void Complete()
36+
{
37+
_channel.Writer.Complete();
38+
}
39+
40+
public IAsyncEnumerable<T> ReadAllAsync()
41+
{
42+
return _channel.Reader.ReadAllAsync();
43+
}
44+
45+
public async Task<T?> ReadNextAsync()
46+
{
47+
if (await _channel.Reader.WaitToReadAsync())
48+
{
49+
_channel.Reader.TryRead(out var message);
50+
return message;
51+
}
52+
else
53+
{
54+
return null;
55+
}
56+
}
57+
58+
public Task WriteAsync(T message, CancellationToken cancellationToken)
59+
{
60+
if (cancellationToken.IsCancellationRequested)
61+
{
62+
return Task.FromCanceled(cancellationToken);
63+
}
64+
if (_serverCallContext.CancellationToken.IsCancellationRequested)
65+
{
66+
return Task.FromCanceled(_serverCallContext.CancellationToken);
67+
}
68+
69+
if (!_channel.Writer.TryWrite(message))
70+
{
71+
throw new InvalidOperationException("Unable to write message.");
72+
}
73+
74+
return Task.CompletedTask;
75+
}
76+
77+
public Task WriteAsync(T message)
78+
{
79+
return WriteAsync(message, CancellationToken.None);
80+
}
81+
}

dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/Helpers/ClusterCollection.cs renamed to dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/Helpers/Orleans/ClusterCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// ClusterCollection.cs
33

4-
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers;
4+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;
55

66
[CollectionDefinition(Name)]
77
public sealed class ClusterCollection : ICollectionFixture<ClusterFixture>

dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/Helpers/ClusterFixture.cs renamed to dotnet/test/Microsoft.AutoGen.Runtime.Grpc.Tests/Helpers/Orleans/ClusterFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using Orleans.TestingHost;
55

6-
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers;
6+
namespace Microsoft.AutoGen.Runtime.Grpc.Tests.Helpers.Orleans;
77

88
public sealed class ClusterFixture : IDisposable
99
{

0 commit comments

Comments
 (0)