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

PoC for polymorphic contract #511

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
100 changes: 100 additions & 0 deletions source/Halibut.Tests/PolymorphicTypeContractFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Halibut.Tests.Support;
using Halibut.Tests.Support.TestAttributes;
using Halibut.Tests.Support.TestCases;
using Halibut.Tests.TestServices.Async;
using Halibut.Tests.Util;
using Halibut.TestUtils.Contracts;
using Halibut.Util;
using NUnit.Framework;

namespace Halibut.Tests
{
public class PolymorphicTypeContractFixture : BaseTest
{

[Test]
[LatestClientAndLatestServiceTestCases(testAsyncServicesAsWell: true, testSyncService:false, testNetworkConditions: false)]
public async Task ExecuteServiceWithPolymorphicContract(ClientAndServiceTestCase clientAndServiceTestCase)
{
var clientAndService = await clientAndServiceTestCase.CreateTestCaseBuilder()
.AsLatestClientAndLatestServiceBuilder()
.WithAsyncService<IPolymorphicService, IAsyncPolymorphicService>(() => new AsyncPolymorphicService())
.Build(CancellationToken);

var polymorphicServiceClient = clientAndService.CreateClient<IPolymorphicService, IAsyncClientPolymorphicService>();

(await polymorphicServiceClient.ExecuteScriptAsync(new ExecuteScriptCommand("NoOp", new LocalEnvironment())))
.Should()
.Be("Local:NoOp");

(await polymorphicServiceClient.ExecuteScriptAsync(new ExecuteScriptCommand("NoOp", new KubernetesJobEnvironment("Image", "https://dockerhub.com"))))
.Should()
.Be("K8s:NoOp:Imagehttps://dockerhub.com");
}
}

public interface IPolymorphicService
{
string ExecuteScript(ExecuteScriptCommand command);
}

public interface IAsyncPolymorphicService
{
Task<string> ExecuteScriptAsync(ExecuteScriptCommand command, CancellationToken cancellationToken);
}

public interface IAsyncClientPolymorphicService
{
Task<string> ExecuteScriptAsync(ExecuteScriptCommand command);
}

public class AsyncPolymorphicService : IAsyncPolymorphicService
{
public async Task<string> ExecuteScriptAsync(ExecuteScriptCommand command, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return command.ExecutionEnvironment switch
{
LocalEnvironment _ => $"Local:{command.Script}",
KubernetesJobEnvironment k8s => $"K8s:{command.Script}:{k8s.ContainerImage}{k8s.ContainerImageRepositoryUrl}",
_ => throw new ArgumentOutOfRangeException()
};
}
}

public class ExecuteScriptCommand
{
public ExecuteScriptCommand(string script, IExecutionEnvironment executionEnvironment)
{
Script = script;
ExecutionEnvironment = executionEnvironment;
}

public string Script { get; }
public IExecutionEnvironment ExecutionEnvironment { get; }
}

public interface IExecutionEnvironment
{
}

public class LocalEnvironment : IExecutionEnvironment
{
}

public class KubernetesJobEnvironment : IExecutionEnvironment
{
public KubernetesJobEnvironment(string containerImage, string containerImageRepositoryUrl)
{
ContainerImage = containerImage;
ContainerImageRepositoryUrl = containerImageRepositoryUrl;
}

public string ContainerImage { get; }
public string ContainerImageRepositoryUrl { get; }
}
}
14 changes: 14 additions & 0 deletions source/Halibut/Transport/Protocol/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ IEnumerable<Type> SubTypesFor(Type type)
yield return t;
}
}

if (type.IsInterface || type.IsAbstract)
{
var derivedTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(asm => !asm.IsDynamic)
.SelectMany(asm => asm.GetExportedTypes())
.Where(t => !t.IsAbstract)
.Where(type.IsAssignableFrom);

foreach (var derivedType in derivedTypes)
{
yield return derivedType;
}
}
}

public bool IsInAllowedTypes(Type type)
Expand Down