Skip to content

Commit

Permalink
Added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icnocop committed Oct 15, 2023
1 parent 505dd5e commit ef26f35
Show file tree
Hide file tree
Showing 15 changed files with 515 additions and 58 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ jobs:
run: |
mkdir AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
copy ".\src\AzurePipelines.TestLogger\bin\${{ matrix.Configuration }}\netstandard1.5\*.*" .\AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
copy "~\.nuget\packages\semver\2.0.6\lib\netstandard1.1\*.*" .\AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
copy ".\LICENSE" .\AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
copy ".\README.md" .\AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
copy ".\ReleaseNotes.md" .\AzurePipelines.TestLogger.${{ env.SEM_VERSION }}
Expand Down
19 changes: 17 additions & 2 deletions AzurePipelines.TestLogger.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C95ECC05-F3E8-49F4-B7C5-A29CD7EACFC1}"
EndProject
Expand All @@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "misc", "misc", "{B8315F74-E
stylecop.json = stylecop.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleUnitTestProject", "tests\SampleUnitTestProject\SampleUnitTestProject.csproj", "{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -60,13 +62,26 @@ Global
{8C42EBD4-FF36-44B6-A70E-7D83CB0626F8}.Release|x64.Build.0 = Release|Any CPU
{8C42EBD4-FF36-44B6-A70E-7D83CB0626F8}.Release|x86.ActiveCfg = Release|Any CPU
{8C42EBD4-FF36-44B6-A70E-7D83CB0626F8}.Release|x86.Build.0 = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|x64.ActiveCfg = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|x64.Build.0 = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|x86.ActiveCfg = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Debug|x86.Build.0 = Debug|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|Any CPU.Build.0 = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|x64.ActiveCfg = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|x64.Build.0 = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|x86.ActiveCfg = Release|Any CPU
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{77CA5040-B4A0-4D0B-ADDD-09853A385007} = {C95ECC05-F3E8-49F4-B7C5-A29CD7EACFC1}
{8C42EBD4-FF36-44B6-A70E-7D83CB0626F8} = {FA92AD98-1291-4A90-A3AA-ED81A9BBC86E}
{AFB35FB3-F22D-436A-84F7-A5DEFD879D3D} = {FA92AD98-1291-4A90-A3AA-ED81A9BBC86E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7517899-6171-4E6B-BDD7-DBE01B34E83A}
Expand Down
Empty file added root
Empty file.
51 changes: 25 additions & 26 deletions src/AzurePipelines.TestLogger/AzurePipelines.TestLogger.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
</PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<Compile Remove="contentFiles\**" />
<EmbeddedResource Remove="contentFiles\**" />
<None Remove="contentFiles\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="15.0.0" />
<PackageReference Include="Semver" Version="2.0.6" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>AzurePipelines.TestLogger.Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="15.0.0" />
<PackageReference Include="Semver" Version="2.0.6" GeneratePathProperty="true" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>AzurePipelines.TestLogger.Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<Target Name="CopyFileFromNuGetPackage" AfterTargets="Build">
<Copy SourceFiles="$(PkgSemver)\lib\netstandard1.1\Semver.dll" DestinationFolder="$(OutDir)" />
</Target>

</Project>
28 changes: 24 additions & 4 deletions src/AzurePipelines.TestLogger/AzurePipelinesTestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class AzurePipelinesTestLogger : ITestLoggerWithParameters

public AzurePipelinesTestLogger()
{
// For debugging purposes
// System.Diagnostics.Debugger.Launch();
_environmentVariableProvider = new EnvironmentVariableProvider();
_apiClientFactory = new ApiClientFactory();
}
Expand Down Expand Up @@ -137,10 +139,28 @@ private void TestMessageHandler(object sender, TestRunMessageEventArgs e)
// Add code to handle message
}

private void TestResultHandler(object sender, TestResultEventArgs e) =>
_queue.Enqueue(new VstpTestResult(e.Result));
private void TestResultHandler(object sender, TestResultEventArgs e)
{
try
{
_queue.Enqueue(new VstpTestResult(e.Result));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e) =>
_queue.Flush(new VstpTestRunComplete(e.IsAborted || e.IsCanceled, e.AttachmentSets));
private void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
{
try
{
_queue.Flush(new VstpTestRunComplete(e.IsAborted || e.IsCanceled, e.AttachmentSets));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
48 changes: 24 additions & 24 deletions src/AzurePipelines.TestLogger/LoggerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,44 @@ public void Flush(VstpTestRunComplete testRunComplete)
// Cancel any idle consumers and let them return
_queue.Cancel();

try
{
// Any active consumer will circle back around and batch post the remaining queue
_consumeTask.Wait(TimeSpan.FromSeconds(60));
// Any active consumer will circle back around and batch post the remaining queue
_consumeTask.Wait(TimeSpan.FromSeconds(60));

// Update the run and parents to a completed state
SendTestsCompleted(testRunComplete, _consumeTaskCancellationSource.Token).Wait(TimeSpan.FromSeconds(60));
// Update the run and parents to a completed state
SendTestsCompleted(testRunComplete, _consumeTaskCancellationSource.Token).Wait(TimeSpan.FromSeconds(60));

// Cancel any active HTTP requests if still hasn't finished flushing
_consumeTaskCancellationSource.Cancel();
if (!_consumeTask.Wait(TimeSpan.FromSeconds(10)))
{
throw new TimeoutException("Cancellation didn't happen quickly");
}
}
catch (Exception ex)
// Cancel any active HTTP requests if still hasn't finished flushing
_consumeTaskCancellationSource.Cancel();
if (!_consumeTask.Wait(TimeSpan.FromSeconds(10)))
{
Console.WriteLine(ex);
throw new TimeoutException("Cancellation didn't happen quickly");
}
}

private async Task ConsumeItemsAsync(CancellationToken cancellationToken)
{
while (true)
{
ITestResult[] nextItems = await _queue.TakeAsync().ConfigureAwait(false);

if (nextItems == null || nextItems.Length == 0)
try
{
// Queue is canceling and is empty
return;
}
ITestResult[] nextItems = await _queue.TakeAsync().ConfigureAwait(false);

await SendResultsAsync(nextItems, cancellationToken).ConfigureAwait(false);
if (nextItems == null || nextItems.Length == 0)
{
// Queue is canceling and is empty
return;
}

if (cancellationToken.IsCancellationRequested)
await SendResultsAsync(nextItems, cancellationToken).ConfigureAwait(false);

if (cancellationToken.IsCancellationRequested)
{
return;
}
}
catch (Exception ex)
{
return;
Console.WriteLine(ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="5.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.38" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.3" />
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Shouldly" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AzurePipelines.TestLogger\AzurePipelines.TestLogger.csproj" />
<ProjectReference Include="..\SampleUnitTestProject\SampleUnitTestProject.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions tests/AzurePipelines.TestLogger.Tests/CaptureRequestsMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace AzurePipelines.TestLogger.Tests
{
public class CaptureRequestsMiddleware
{
private readonly RequestDelegate _next;

public CaptureRequestsMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
// Capture the incoming request and store it
IRequestStore requestStore = context.RequestServices.GetService<IRequestStore>();
requestStore.Add(context.Request);

// Call the next middleware in the pipeline
await _next(context);
}
}
}
5 changes: 5 additions & 0 deletions tests/AzurePipelines.TestLogger.Tests/ClientMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ public bool Equals(ClientMessage other) =>

private static string RemoveWhiteSpace(string str) =>
string.Concat(str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));

public override string ToString()
{
return $"Method: {Method}, Endpoint: {Endpoint}, ApiVersion: {ApiVersion}, Body: {Body}";
}
}
}
9 changes: 9 additions & 0 deletions tests/AzurePipelines.TestLogger.Tests/IRequestStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Http;

namespace AzurePipelines.TestLogger.Tests
{
internal interface IRequestStore
{
void Add(HttpRequest item);
}
}
Loading

0 comments on commit ef26f35

Please sign in to comment.