-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathPolymorphicTypeContractFixture.cs
More file actions
100 lines (85 loc) · 3.46 KB
/
PolymorphicTypeContractFixture.cs
File metadata and controls
100 lines (85 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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; }
}
}