Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC client factory integration #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source/Dgraph.tests.e2e/Dgraph.tests.e2e.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Assent" Version="1.4.1" />
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="FluentResults" Version="1.4.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.27.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.2" />
Expand All @@ -38,5 +39,10 @@
<ItemGroup>
<EmbeddedResource Include="Tests/Data/*" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Dgraph.tests.e2e.Orchestration
{
public class DgraphClientFactory {
public class DgraphClientFactory : IDgraphClientFactory {

private bool printed;

Expand Down
12 changes: 12 additions & 0 deletions source/Dgraph.tests.e2e/Orchestration/IDgraphClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Dgraph.tests.e2e.Orchestration
{
public interface IDgraphClientFactory
{
Task<IDgraphClient> GetDgraphClient();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line here.
EOF

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Serilog;

namespace Dgraph.tests.e2e.Orchestration
{
class InjectedDgraphClientFactory : IDgraphClientFactory
{
private bool printed;

private readonly IServiceProvider provider;

public InjectedDgraphClientFactory(IServiceProvider provider)
{
this.provider = provider;
}

public async Task<IDgraphClient> GetDgraphClient()
{
AppContext.SetSwitch(
"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

DgraphClient client = (DgraphClient)provider.GetService(typeof(DgraphClient));

if (!printed)
{
var result = await client.CheckVersion();
if (result.IsSuccess)
{
Log.Information("Connected to Dgraph version {Version}", result.Value);
}
else
{
Log.Information("Failed to get Dgraph version {Error}", result);
}
printed = true;
}

return client;
}

}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line

4 changes: 2 additions & 2 deletions source/Dgraph.tests.e2e/Orchestration/TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class TestExecutor {
private List<Exception> _Exceptions = new List<Exception>();

private readonly TestFinder TestFinder;
private readonly DgraphClientFactory ClientFactory;
private readonly IDgraphClientFactory ClientFactory;

public TestExecutor(TestFinder testFinder, DgraphClientFactory clientFactory) {
public TestExecutor(TestFinder testFinder, IDgraphClientFactory clientFactory) {
TestFinder = testFinder;
ClientFactory = clientFactory;
}
Expand Down
77 changes: 61 additions & 16 deletions source/Dgraph.tests.e2e/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Grpc.Net.Client;

namespace Dgraph.tests.e2e
{
Expand All @@ -36,30 +37,24 @@ public static int Main(string[] args) {
.ReadFrom.Configuration(config)
.CreateLogger();

var services = new ServiceCollection();
Log.Information("Testing default way of channel creation");

// Inject in every possible test type so that DI will be able to
// mint up these for me without me having to do anything to hydrate
// the objects.
Type baseTestType = typeof(DgraphDotNetE2ETest);
var assembly = typeof(DgraphDotNetE2ETest).Assembly;
IEnumerable<Type> testTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(baseTestType));
foreach (var testType in testTypes) {
services.AddTransient(testType);
}
var app = new CommandLineApplication<Program>();
app.Conventions
.UseDefaultConventions()
.UseConstructorInjection(GetDefaultServiceProvider());

services.AddSingleton<TestFinder>();
services.AddTransient<TestExecutor>();
services.AddScoped<DgraphClientFactory>();
app.Execute(args);

var serviceProvider = services.BuildServiceProvider();
Log.Information("Testing gRPC client factory to inject gRPC client in a centralized way");

var app = new CommandLineApplication<Program>();
app = new CommandLineApplication<Program>();
app.Conventions
.UseDefaultConventions()
.UseConstructorInjection(serviceProvider);
.UseConstructorInjection(GetClientInjectionServiceProvider("http://127.0.0.1:9080"));

app.Execute(args);

return 0;

} catch (AggregateException aggEx) {
Expand All @@ -82,6 +77,56 @@ public static int Main(string[] args) {
return 1;
}

private static ServiceProvider GetDefaultServiceProvider()
{
IServiceCollection services = new ServiceCollection();

// Inject in every possible test type so that DI will be able to
// mint up these for me without me having to do anything to hydrate
// the objects.
Type baseTestType = typeof(DgraphDotNetE2ETest);
var assembly = typeof(DgraphDotNetE2ETest).Assembly;
IEnumerable<Type> testTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(baseTestType));
foreach (var testType in testTypes)
{
services.AddTransient(testType);
}

services.AddSingleton<TestFinder>();
services.AddTransient<TestExecutor>();
services.AddScoped<IDgraphClientFactory, DgraphClientFactory>();

return services.BuildServiceProvider();
}

private static ServiceProvider GetClientInjectionServiceProvider(string address)
{
IServiceCollection services = new ServiceCollection();

// Inject in every possible test type so that DI will be able to
// mint up these for me without me having to do anything to hydrate
// the objects.
Type baseTestType = typeof(DgraphDotNetE2ETest);
var assembly = typeof(DgraphDotNetE2ETest).Assembly;
IEnumerable<Type> testTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(baseTestType));
foreach (var testType in testTypes)
{
services.AddTransient(testType);
}

services.AddGrpcClient<Api.Dgraph.DgraphClient>(o =>
{
o.Address = new Uri(address);
});

services.AddSingleton<TestFinder>();
services.AddTransient<TestExecutor>();
services.AddTransient<DgraphClient>();
services.AddScoped<IDgraphClientFactory, InjectedDgraphClientFactory>();

return services.BuildServiceProvider();
}

public Program(IServiceProvider serviceProvider, TestFinder testFinder, TestExecutor testExecutor) {
ServiceProvider = serviceProvider;
TestFinder = testFinder;
Expand Down
4 changes: 2 additions & 2 deletions source/Dgraph.tests.e2e/Tests/DgraphDotNetE2ETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

namespace Dgraph.tests.e2e.Tests {
public abstract class DgraphDotNetE2ETest {
protected readonly DgraphClientFactory ClientFactory;
protected readonly IDgraphClientFactory ClientFactory;

protected readonly Assent.Configuration AssentConfiguration;

private readonly IFileProvider EmbeddedProvider;

public DgraphDotNetE2ETest(DgraphClientFactory clientFactory) {
public DgraphDotNetE2ETest(IDgraphClientFactory clientFactory) {
ClientFactory = clientFactory;

AssentConfiguration = new Assent.Configuration()
Expand Down
2 changes: 1 addition & 1 deletion source/Dgraph.tests.e2e/Tests/MutateQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MutateQueryTest : DgraphDotNetE2ETest {

private Person Person1, Person2, Person3;

public MutateQueryTest(DgraphClientFactory clientFactory) : base(clientFactory) { }
public MutateQueryTest(IDgraphClientFactory clientFactory) : base(clientFactory) { }

public async override Task Setup() {
await base.Setup();
Expand Down
2 changes: 1 addition & 1 deletion source/Dgraph.tests.e2e/Tests/SchemaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Dgraph.tests.e2e.Tests
{
public class SchemaTest : DgraphDotNetE2ETest {
public SchemaTest(DgraphClientFactory clientFactory) : base(clientFactory) { }
public SchemaTest(IDgraphClientFactory clientFactory) : base(clientFactory) { }

public async override Task Test() {
using(var client = await ClientFactory.GetDgraphClient()) {
Expand Down
2 changes: 1 addition & 1 deletion source/Dgraph.tests.e2e/Tests/TransactionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Dgraph.tests.e2e.Tests

public class TransactionTest : DgraphDotNetE2ETest {

public TransactionTest(DgraphClientFactory clientFactory) : base(clientFactory) { }
public TransactionTest(IDgraphClientFactory clientFactory) : base(clientFactory) { }

public async override Task Setup() {
await base.Setup();
Expand Down
5 changes: 5 additions & 0 deletions source/Dgraph/Client/DgraphClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public DgraphClient(params GrpcChannel[] channels) {
}
}

public DgraphClient(Api.Dgraph.DgraphClient client)
{
dgraphs.Add(client);
}

//
// ------------------------------------------------------
// Transactions
Expand Down