Skip to content

Commit

Permalink
feat: adopt http\resiliency\srv-driven
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerangel-msft committed Nov 19, 2024
1 parent 13d2064 commit eb57b4f
Show file tree
Hide file tree
Showing 19 changed files with 1,166 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function IsSpecDir {

$failingSpecs = @(
Join-Path 'http' 'payload' 'pageable'
Join-Path 'http' 'resiliency' 'srv-driven'
Join-Path 'http' 'special-headers' 'conditional-request'
Join-Path 'http' 'type' 'model' 'flatten'
Join-Path 'http' 'type' 'model' 'templated'
Expand Down Expand Up @@ -94,7 +93,7 @@ foreach ($directory in $directories) {
$generationDir = Join-Path $generationDir $folder
}

#create the directory if it doesn't exist
# create the directory if it doesn't exist
if (-not (Test-Path $generationDir)) {
New-Item -ItemType Directory -Path $generationDir | Out-Null
}
Expand All @@ -111,6 +110,24 @@ foreach ($directory in $directories) {
exit $LASTEXITCODE
}

# srv-driven contains two separate specs, for two separate clients. We need to generate both.
if ($folders.Contains("srv-driven")) {
$specFile = Join-Path $directory.FullName "old.tsp"
$generationDir = Join-Path $generationDir "v1"
if (-not (Test-Path $generationDir)) {
New-Item -ItemType Directory -Path $generationDir | Out-Null
}

Write-Host "Generating $subPath v1" -ForegroundColor Cyan
# override namespace for "resiliency/srv-driven/old.tsp" (make it different to that from "main.tsp")
Invoke (Get-TspCommand $specFile $generationDir $stubbed " --option @typespec/http-client-csharp.namespace=Resiliency.ServiceDriven.V1")

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}

# TODO need to build but depends on https://github.com/Azure/autorest.csharp/issues/4463
}

Expand Down
7 changes: 6 additions & 1 deletion packages/http-client-csharp/eng/scripts/Generation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function Get-TspCommand {
param (
[string]$specFile,
[string]$generationDir,
[bool]$generateStub = $false
[bool]$generateStub = $false,
[string[]]$additionalOptions = @()
)
$command = "npx tsp compile $specFile"
$command += " --trace @typespec/http-client-csharp"
Expand All @@ -38,6 +39,10 @@ function Get-TspCommand {
if ($generateStub) {
$command += " --option @typespec/http-client-csharp.plugin-name=StubLibraryPlugin"
}

foreach ($option in $additionalOptions) {
$command += " $option"
}
return $command
}

Expand Down
19 changes: 19 additions & 0 deletions packages/http-client-csharp/eng/scripts/Get-CadlRanch-Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ foreach ($directory in $directories) {
$outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1)
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)

if ($subPath.Contains($(Join-Path 'srv-driven' 'v1'))) {
continue
}

Write-Host "Regenerating $subPath" -ForegroundColor Cyan

$specFile = Join-Path $specsDirectory $subPath "client.tsp"
Expand All @@ -40,6 +44,21 @@ foreach ($directory in $directories) {
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

# srv-driven contains two separate specs, for two separate clients. We need to generate both.
if ($subPath.Contains('srv-driven')) {
Write-Host "Regenerating $subPath v1" -ForegroundColor Cyan
$specFile = Join-Path $specsDirectory $subPath "old.tsp"
$outputDir = Join-Path $outputDir "v1"
# override namespace for "resiliency/srv-driven/old.tsp" (make it different to that from "main.tsp")
$command = Get-TspCommand $specFile $outputDir -additionalOptions " --option @typespec/http-client-csharp.namespace=Resiliency.ServiceDriven.V1"

Invoke $command
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
}

# test all
Expand Down
20 changes: 20 additions & 0 deletions packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ foreach ($directory in $directories) {
if (-not (Compare-Paths $subPath $filter)) {
continue
}

if ($subPath.Contains($(Join-Path 'srv-driven' 'v1'))) {
continue
}

$testPath = "$cadlRanchRoot.Tests"
$testFilter = "TestProjects.CadlRanch.Tests"
Expand Down Expand Up @@ -64,6 +68,22 @@ foreach ($directory in $directories) {
exit $LASTEXITCODE
}

# srv-driven contains two separate specs, for two separate clients. We need to generate both.
if ($subPath.Contains("srv-driven")) {
Write-Host "Regenerating $subPath v1" -ForegroundColor Cyan

$specFile = Join-Path $specsDirectory $subPath "old.tsp"
$srvDrivenOutputDir = Join-Path $outputDir "v1"
# override namespace for "resiliency/srv-driven/old.tsp" (make it different to that from "main.tsp")
$command = Get-TspCommand $specFile $srvDrivenOutputDir -additionalOptions " --option @typespec/http-client-csharp.namespace=Resiliency.ServiceDriven.V1"
Invoke $command

if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}


Write-Host "Testing $subPath" -ForegroundColor Cyan
$command = "dotnet test $cadlRanchCsproj --filter `"FullyQualifiedName~$testFilter`""
Invoke $command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-resiliency-srv-driven": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/resiliency/srv-driven -p StubLibraryPlugin",
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-routes": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/routes -p StubLibraryPlugin",
"commandName": "Executable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NUnit.Framework;
using Resiliency.ServiceDriven.V1;
using System.Threading.Tasks;

namespace TestProjects.CadlRanch.Tests.Http.Resiliency.SrvDriven
{
/// <summary>
/// Contains tests for the service-driven resiliency V1 client.
/// </summary>
public partial class SrvDrivenTests : CadlRanchTestBase
{
// This test validates the v1 client behavior when both the service deployment and api version are set to V1.
[CadlRanchTest]
public Task AddOptionalParamFromNone_V1Client_V1Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV1, options);
var response = await client.FromNoneAsync();

Assert.AreEqual(204, response.GetRawResponse().Status);
});

// This test validates the v1 client behavior when the service deployment is set to V2 and the api version is set to V1.
[CadlRanchTest]
public Task AddOptionalParamFromNone_V1Client_V2Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromNoneAsync();

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneOptional_V1Client_V1Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV1, options);
var response = await client.FromOneOptionalAsync("optional");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneOptional_V1Client_V2Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneOptionalAsync("optional");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneRequired_V1Client_V1Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV1, options);
var response = await client.FromOneRequiredAsync("required");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneRequired_V1Client_V2Service_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneRequiredAsync("required");

Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NUnit.Framework;
using System.Threading.Tasks;
using Resiliency.ServiceDriven;

namespace TestProjects.CadlRanch.Tests.Http.Resiliency.SrvDriven
{
public partial class SrvDrivenTests : CadlRanchTestBase
{
private const string ServiceDeploymentV1 = "v1";
private const string ServiceDeploymentV2 = "v2";

[CadlRanchTest]
public Task AddOperation() => Test(async (host) =>
{
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2);
var response = await client.AddOperationAsync();

Assert.AreEqual(204, response.GetRawResponse().Status);
});

// This test validates the "new" client behavior when the api version is set to V1.
[CadlRanchTest]
public Task AddOptionalParamFromNone_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromNoneAsync();

Assert.AreEqual(204, response.GetRawResponse().Status);
});

// This test validates the "new" client behavior when the api version is set to V2.
[CadlRanchTest]
public Task AddOptionalParamFromNone_WithApiVersionV2() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V2);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromNoneAsync("new");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneOptional_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneOptionalAsync("optional");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneOptional_WithApiVersionV2() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V2);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneOptionalAsync("optional", "new");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneRequired_WithApiVersionV1() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V1);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneRequiredAsync("required");

Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task AddOptionalParamFromOneRequired_WithApiVersionV2() => Test(async (host) =>
{
var options = new ResiliencyServiceDrivenClientOptions(ResiliencyServiceDrivenClientOptions.ServiceVersion.V2);
var client = new ResiliencyServiceDrivenClient(host, ServiceDeploymentV2, options);
var response = await client.FromOneRequiredAsync("required", "new");

Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output-folder": ".",
"namespace": "Resiliency.ServiceDriven",
"library-name": "Resiliency.ServiceDriven",
"use-model-reader-writer": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Resiliency.ServiceDriven", "src\Resiliency.ServiceDriven.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal
Loading

0 comments on commit eb57b4f

Please sign in to comment.