Skip to content

Commit a04684a

Browse files
authored
Add client testing to Tester example (#1551)
1 parent a236023 commit a04684a

28 files changed

+526
-49
lines changed

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,13 @@ dotnet run --EnableOpenTelemetry=true
122122

123123
The tester shows how to test gRPC services. The unit tests create and test a gRPC service directly. The functional tests show how to use [Microsoft.AspNetCore.TestHost](https://www.nuget.org/packages/Microsoft.AspNetCore.TestHost/) (version 3.1.2 or greater required) to host a gRPC service with an in-memory test server and call it using a gRPC client. The functional tests write client and server logs to the test output.
124124

125+
The tests also show how to mock a gRPC client when testing gRPC client apps.
126+
125127
##### Scenarios:
126128

127129
* Unit testing
128130
* Functional testing
131+
* Mocking gRPC client
129132

130133
## [Progressor](./Progressor)
131134

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<UserSecretsId>dotnet-Client-33BD397C-6A11-40D0-AF85-24B9610F7517</UserSecretsId>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Common\Common.csproj" />
10+
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsPackageVersion)" />
12+
<PackageReference Include="Grpc.Net.ClientFactory" Version="$(GrpcDotNetPackageVersion)" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
namespace Client
20+
{
21+
public class GreetRepository : IGreetRepository
22+
{
23+
private readonly ILogger<GreetRepository> _logger;
24+
25+
public GreetRepository(ILogger<GreetRepository> logger)
26+
{
27+
_logger = logger;
28+
}
29+
30+
public void SaveGreeting(string message)
31+
{
32+
_logger.LogInformation("Reply: {Message}", message);
33+
}
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
namespace Client
20+
{
21+
public interface IGreetRepository
22+
{
23+
void SaveGreeting(string message);
24+
}
25+
}

examples/Tester/Client/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Test;
20+
21+
namespace Client
22+
{
23+
public class Program
24+
{
25+
public static void Main(string[] args)
26+
{
27+
CreateHostBuilder(args).Build().Run();
28+
}
29+
30+
public static IHostBuilder CreateHostBuilder(string[] args) =>
31+
Host.CreateDefaultBuilder(args)
32+
.ConfigureServices((hostContext, services) =>
33+
{
34+
services.AddHostedService<Worker>();
35+
services.AddSingleton<IGreetRepository, GreetRepository>();
36+
services.AddGrpcClient<Tester.TesterClient>(options =>
37+
{
38+
options.Address = new Uri("https://localhost:5001");
39+
});
40+
});
41+
}
42+
}

examples/Tester/Client/Worker.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Test;
20+
21+
namespace Client
22+
{
23+
public class Worker : BackgroundService
24+
{
25+
private readonly Tester.TesterClient _client;
26+
private readonly IGreetRepository _greetRepository;
27+
28+
public Worker(Tester.TesterClient client, IGreetRepository greetRepository)
29+
{
30+
_client = client;
31+
_greetRepository = greetRepository;
32+
}
33+
34+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
35+
{
36+
var count = 0;
37+
while (!stoppingToken.IsCancellationRequested)
38+
{
39+
count++;
40+
41+
var reply = await _client.SayHelloUnaryAsync(
42+
new HelloRequest { Name = $"Worker {count}" });
43+
44+
_greetRepository.SaveGreeting(reply.Message);
45+
46+
await Task.Delay(1000, stoppingToken);
47+
}
48+
}
49+
}
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Grpc": "Information",
6+
"Microsoft": "Warning",
7+
"Microsoft.Hosting.Lifetime": "Information"
8+
}
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\test.proto" Link="Protos\test.proto" />
9+
10+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
11+
<PackageReference Include="Grpc.Tools" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
12+
<PackageReference Include="Grpc.Core.Api" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
13+
</ItemGroup>
14+
15+
</Project>

examples/Tester/Server/Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<Protobuf Include="..\Proto\test.proto" GrpcServices="Both" Link="Protos\test.proto" />
8+
<ProjectReference Include="..\Common\Common.csproj" />
99

1010
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
1111
</ItemGroup>

0 commit comments

Comments
 (0)