Skip to content

Commit

Permalink
Add LongRunningTasksService
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Nov 10, 2023
1 parent 9fecc26 commit 448fea5
Show file tree
Hide file tree
Showing 23 changed files with 577 additions and 6 deletions.
35 changes: 35 additions & 0 deletions az-appservice-dotnet.Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("az_appservice_dotnet.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("az_appservice_dotnet.Test")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("D85D944B-8861-4C3E-979B-F7C227CF2F19")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
14 changes: 14 additions & 0 deletions az-appservice-dotnet.Test/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Xunit;

namespace az_appservice_dotnet.Test
{
public class Tests
{
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}
66 changes: 66 additions & 0 deletions az-appservice-dotnet.Test/az-appservice-dotnet.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D85D944B-8861-4C3E-979B-F7C227CF2F19}</ProjectGuid>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>az_appservice_dotnet.Test</RootNamespace>
<AssemblyName>az_appservice_dotnet.Test</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Data"/>
<Reference Include="System.Xml"/>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c">
<HintPath>..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Tests.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
9 changes: 9 additions & 0 deletions az-appservice-dotnet.Test/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
</packages>
6 changes: 6 additions & 0 deletions az-appservice-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 25.0.1706.6
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "az-appservice-dotnet", "az-appservice-dotnet\az-appservice-dotnet.csproj", "{C9F971F6-AB9C-4704-BB5E-3524C36AD9AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "az-appservice-dotnet.xUnit", "az-appservice-dotnet.xUnit\az-appservice-dotnet.xUnit.csproj", "{EC930BCF-7CE0-4E27-82BB-F7C1754EEF73}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{C9F971F6-AB9C-4704-BB5E-3524C36AD9AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9F971F6-AB9C-4704-BB5E-3524C36AD9AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9F971F6-AB9C-4704-BB5E-3524C36AD9AE}.Release|Any CPU.Build.0 = Release|Any CPU
{EC930BCF-7CE0-4E27-82BB-F7C1754EEF73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC930BCF-7CE0-4E27-82BB-F7C1754EEF73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC930BCF-7CE0-4E27-82BB-F7C1754EEF73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC930BCF-7CE0-4E27-82BB-F7C1754EEF73}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 2 additions & 0 deletions az-appservice-dotnet.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=appservice/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace az_appservice_dotnet.xUnit.models.v1.LongRunningWorkloads;

public class Progress
{
[Fact]
public void Progress_shouldReturnZeroOnInit()
{
// Arrange
var sut = new az_appservice_dotnet.models.v1.LongRunningWorkloads(10);
// Act
var actual = sut.Progress;
// Assert
Assert.Equal(0U, actual);
}

[Fact]
public void Progress_shouldReturn10OnNext()
{
// Arrange
var sut = new az_appservice_dotnet.models.v1.LongRunningWorkloads(10);
// Act
sut.Next(1);
var actual = sut.Progress;
// Assert
Assert.Equal(10U, actual);
}

[Fact]
public void Progress_shouldReturn100OnOverflow()
{
// Arrange
var sut = new az_appservice_dotnet.models.v1.LongRunningWorkloads(10);
// Act
sut.Next(11);
var actual = sut.Progress;
// Assert
Assert.Equal(100U, actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using az_appservice_dotnet.models;
using az_appservice_dotnet.services;
using Moq;

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class GetLongRunningTaskProgress
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public GetLongRunningTaskProgress()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
}

[Fact]
public void GetLongRunningTaskProgress_shouldReturnMinus1()
{
var stubFactory = new Mock<ILongRunningWorkloadFactory>();
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(stubFactory.Object);
var actual = sut.GetLongRunningTaskProgress(new TaskId());
Assert.Equal(-1, actual);
}

[Fact]
public void GetLongRunningTaskProgress_shouldReturnForStartedTask()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
var taskId = sut.StartLongRunningTasksAsync(2);
// Act
var actual = sut.GetLongRunningTaskProgress(taskId);
// Assert
Assert.True(actual >= 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using az_appservice_dotnet.models;
using Moq;

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class GetLongRunningTasks
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public GetLongRunningTasks()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
}

[Fact]
public void GetLongRunningTasks_shouldReturnEmptyI()
{
var stubFactory = new Mock<ILongRunningWorkloadFactory>();
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(stubFactory.Object);
var actual = sut.GetLongRunningTasks();
Assert.Empty(actual);
}

[Fact]
public void GetLongRunningTasks_shouldReturnStartedTasks()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
var taskId1 = sut.StartLongRunningTasksAsync(2);
var taskId2 = sut.StartLongRunningTasksAsync(2);
// Act
var actual = sut.GetLongRunningTasks();
// Assert
Assert.Equal(2, actual.Count);
Assert.Contains(actual, x => x == taskId1);
Assert.Contains(actual, x => x == taskId2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using az_appservice_dotnet.models;
using az_appservice_dotnet.services;
using Moq;

namespace az_appservice_dotnet.xUnit.services.v1.LongRunningTasksService;

public class StartLongRunningTasksAsync
{
readonly Mock<ILongRunningWorkload> _fakeWorkload = new();
readonly Mock<ILongRunningWorkloadFactory> _fakeFactory = new();

public StartLongRunningTasksAsync()
{
_fakeFactory.Setup(x =>
x.Create(It.IsAny<uint>())).Returns(_fakeWorkload.Object);
}

[Fact]
public void StartLongRunningTasksAsync_shouldReturnTaskId()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
// Act
var actual = sut.StartLongRunningTasksAsync(2);
// Assert
Assert.IsType<TaskId>(actual);
}

[Fact]
public void StartLongRunningTasksAsync_shouldReturnTaskIdWhatExists()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
// Act
var taskId = sut.StartLongRunningTasksAsync(2);
var foundTaskId = sut.GetLongRunningTaskProgress(taskId);
// Assert
Assert.True(foundTaskId >= 0);
}

[Fact]
public void StartLongRunningTasksAsync_shouldCallWorkloadNext2Times()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
// Act
sut.StartLongRunningTasksAsync(2);
Thread.Sleep(3000);
// Assert
_fakeWorkload.Verify(wl => wl.Next(It.IsAny<uint>()),
Times.Exactly(2));
}

[Fact]
public void StartLongRunningTasksAsync_shouldStartTaskAsIncomplete()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
// Act
var actual = sut.StartLongRunningTasksAsync(1);
// Assert
sut.GetTaskDictionaryTest().TryGetValue(actual, out var task);
Assert.NotNull(task);
Assert.False(task.IsCompleted);
}

[Fact]
public void StartLongRunningTasksAsync_shouldStartTaskHaveCompleted()
{
// Arrange
var sut = new az_appservice_dotnet.services.v1.LongRunningTasksService(_fakeFactory.Object);
// Act
var actual = sut.StartLongRunningTasksAsync(1);
Thread.Sleep(2000);
// Assert
sut.GetTaskDictionaryTest().TryGetValue(actual, out var task);
Assert.NotNull(task);
Assert.True(task.IsCompleted);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace az_appservice_dotnet.xUnit.services.v1.LongRunningWorkloadFactory;

public class Create
{
[Fact]
public void Create_shouldReturnLongRunningWorkload()
{
// Arrange
var sut = new az_appservice_dotnet.models.v1.LongRunningWorkloadFactory();
// Act
var actual = sut.Create(10);
// Assert
Assert.IsType<az_appservice_dotnet.models.v1.LongRunningWorkloads>(actual);
}

[Fact]
public void Create_shouldPassDurationToWorkload()
{
// Arrange
var sut = new az_appservice_dotnet.models.v1.LongRunningWorkloadFactory();
// Act
var actual = sut.Create(10);
actual.Next(9);
var actualProgress1 = actual.Progress;
actual.Next(10);
var actualProgress2 = actual.Progress;
// Assert
Assert.Equal(90U, actualProgress1);
Assert.Equal(100U, actualProgress2);
}
}
Loading

0 comments on commit 448fea5

Please sign in to comment.