Skip to content

Commit

Permalink
Merge pull request #10 from golemfactory/facade-job-creation-tests
Browse files Browse the repository at this point in the history
Facade job creation tests
  • Loading branch information
pwalski authored Nov 27, 2023
2 parents ffe4065 + 41533c5 commit a0a1f44
Show file tree
Hide file tree
Showing 21 changed files with 956 additions and 72 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ jobs:
- name: Build
run: dotnet build
- name: Test with the dotnet CLI
run: dotnet test
run: dotnet test -v diag
- name: Archive JobTests artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: JobTests_${{ matrix.os }}
path: |
Golem.Tests/tests/JobTests/modules/golem-data/yagna/*.log
Golem.Tests/tests/JobTests/modules/golem-data/provider/*.log
- name: Archive JobTests requestor artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: JobTests_requestor_${{ matrix.os }}
path: |
Golem.Tests/tests/JobTests_requestor/modules/golem-data/yagna/*.log
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Mock/obj/
/Golem.Tests/bin/
/Golem.Tests/obj/
/Golem.Tests/tests/
/Golem.Tests/TestResults
/Golem.Tests/Tools/App/.venv
/Golem.Tests/Tools/App/build
/Golem.Tests/Tools/App/dist
/Golem.Tests/Tools/App/app.spec
/MockGUI/bin/
/MockGUI/obj/
/modules/
27 changes: 25 additions & 2 deletions Golem.Tests/Golem.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="MedallionShell" Version="1.6.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -23,11 +25,32 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="XunitContext" Version="3.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GolemLib\GolemLib.csproj" />
<ProjectReference Include="..\Golem\Golem.csproj" />
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<Target Name="AfterClean">
<RemoveDir Directories="$(ProjectDir)\bin\$(Configuration)\net7.0\app\" ContinueOnError="true" />
<RemoveDir Directories="$(ProjectDir)\Tools\App\dist" ContinueOnError="true" />
<RemoveDir Directories="$(ProjectDir)\Tools\App\build" ContinueOnError="true" />
</Target>

<Target Name="BuildApp" BeforeTargets="Test" AfterTargets="AfterBuild">
<Exec Command="python -m venv .venv" WorkingDirectory="$(ProjectDir)\Tools\App" ConsoleToMsBuild="true" />
<Exec Command=".\.venv\Scripts\activate | pip install -r requirements.txt" Condition="'$(OS)' == 'WINDOWS_NT'" WorkingDirectory="$(ProjectDir)\Tools\App" ConsoleToMsBuild="true" />
<Exec Command=".\.venv\Scripts\activate | pyinstaller --onefile app.py -y" Condition="'$(OS)' == 'WINDOWS_NT'" WorkingDirectory="$(ProjectDir)\Tools\App" ConsoleToMsBuild="true" />
<Exec Command=". .venv/bin/activate; pip install -r requirements.txt" Condition="'$(OS)' != 'WINDOWS_NT'" WorkingDirectory="$(ProjectDir)\Tools\App" ConsoleToMsBuild="true" />
<Exec Command=". .venv/bin/activate; pyinstaller --onefile app.py -y" Condition="'$(OS)' != 'WINDOWS_NT'" WorkingDirectory="$(ProjectDir)\Tools\App" ConsoleToMsBuild="true" />
<RemoveDir Directories="$(ProjectDir)\bin\$(Configuration)\net7.0\app" ContinueOnError="true" />
<ItemGroup>
<AppBinary Include="$(ProjectDir)Tools\App\dist\*.*" />
</ItemGroup>
<Copy SourceFiles="@(AppBinary)" DestinationFolder="$(ProjectDir)bin\$(Configuration)\net7.0\app\" />
</Target>

</Project>
28 changes: 22 additions & 6 deletions Golem.Tests/GolemTests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
using App;

using Golem;
using Golem.IntegrationTests.Tools;
using Golem.Yagna.Types;

using GolemLib;
using GolemLib.Types;

using Microsoft.Extensions.Logging;

namespace Golem.Tests
{
[Collection("Sequential")]
public class GolemTests
public class GolemTests : IDisposable
{
readonly ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options => options.SingleLine = true)
private readonly ILoggerFactory _loggerFactory;

public GolemTests(ITestOutputHelper outputHelper)
{
XunitContext.Register(outputHelper);
_loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole(options => options.SingleLine = true)
);
}

[Fact]
public async Task StartStop_VerifyStatusAsync()
{
string golemPath = await PackageBuilder.BuildTestDirectory("StartStop_VerifyStatusAsync");
Console.WriteLine("Path: " + golemPath);

var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), loggerFactory);
var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), _loggerFactory);
GolemStatus status = GolemStatus.Off;

Action<GolemStatus> updateStatus = (v) =>
Expand Down Expand Up @@ -54,7 +65,7 @@ public async Task Start_ChangeWallet_VerifyStatusAsync()
string golemPath = await PackageBuilder.BuildTestDirectory("Start_ChangeWallet_VerifyStatusAsync");
Console.WriteLine("Path: " + golemPath);

var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), loggerFactory);
var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), _loggerFactory);
GolemStatus status = GolemStatus.Off;

Action<GolemStatus> updateStatus = (v) =>
Expand All @@ -79,7 +90,7 @@ public async Task Start_ChangePrices_VerifyPriceAsync()
string golemPath = await PackageBuilder.BuildTestDirectory("Start_ChangePrices_VerifyPriceAsync");
Console.WriteLine("Path: " + golemPath);

var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), loggerFactory);
var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), _loggerFactory);

decimal price = 0;

Expand Down Expand Up @@ -110,5 +121,10 @@ public async Task Start_ChangePrices_VerifyPriceAsync()
Assert.Equal(0.007m, golem.Price.EnvPerHour);
Assert.Equal(0.008m, golem.Price.NumRequests);
}

public void Dispose()
{
XunitContext.Flush();
}
}
}
159 changes: 159 additions & 0 deletions Golem.Tests/JobTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using System.Threading.Channels;

using App;

using Golem;
using Golem.IntegrationTests.Tools;
using Golem.Yagna;
using Golem.Yagna.Types;

using GolemLib;
using GolemLib.Types;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;

using Xunit.Abstractions;
using Xunit.Sdk;

namespace Golem.Tests
{

public class GolemFixture : IDisposable
{
public GolemFixture(IMessageSink sink)
{
Sink = sink;
}

public IMessageSink Sink { get; }

public void Dispose()
{
}
}

[Collection("Sequential")]
public class JobTests : IDisposable, IAsyncLifetime, IClassFixture<GolemFixture>
{
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _logger;
private GolemRelay? _relay;
private GolemRequestor? _requestor;

public JobTests(ITestOutputHelper outputHelper, GolemFixture golemFixture)
{
XunitContext.Register(outputHelper);
// var logfile = Path.Combine(PackageBuilder.TestDir(nameof(JobTests)), "gh_facade-{Date}.log");
var loggerProvider = new TestLoggerProvider(golemFixture.Sink);
_logger = loggerProvider.CreateLogger(nameof(JobTests));
_loggerFactory = LoggerFactory.Create(builder => builder
.AddSimpleConsole(options => options.SingleLine = true)
// .AddFile(logfile)
.AddProvider(loggerProvider)
);
}

public async Task InitializeAsync()
{
_relay = await GolemRelay.Build(nameof(JobTests), _loggerFactory.CreateLogger("Relay"));
Assert.True(_relay.Start());
System.Environment.SetEnvironmentVariable("YA_NET_RELAY_HOST", "127.0.0.1:17464");
System.Environment.SetEnvironmentVariable("RUST_LOG", "debug");

_requestor = await GolemRequestor.Build(nameof(JobTests), _loggerFactory.CreateLogger("Requestor"), false);
Assert.True(_requestor.Start());
_requestor.InitAccount();
}

[Fact]
public async Task StartStop_Job()
{
string golemPath = await PackageBuilder.BuildTestDirectory(nameof(JobTests));
_logger.LogInformation($"Path: {golemPath}");
var golem = new Golem(PackageBuilder.BinariesDir(golemPath), PackageBuilder.DataDir(golemPath), _loggerFactory);

var statusChannel = Channel.CreateUnbounded<GolemStatus>();
Action<GolemStatus> golemStatus = async (v) =>
{
_logger.LogInformation($"Golem status update. {v}");
await statusChannel.Writer.WriteAsync(v);
};
golem.PropertyChanged += new PropertyChangedHandler<Golem, GolemStatus>(nameof(IGolem.Status), golemStatus).Subscribe();

var jobChannel = Channel.CreateUnbounded<IJob?>();
Action<IJob?> currentJobHook = async (v) =>
{
_logger.LogInformation($"Current Job update. {v}");
await jobChannel.Writer.WriteAsync(v);
};
golem.PropertyChanged += new PropertyChangedHandler<Golem, IJob?>(nameof(IGolem.CurrentJob), currentJobHook).Subscribe();

_logger.LogInformation("Starting Golem");
await golem.Start();
Assert.Equal(GolemStatus.Starting, await statusChannel.Reader.ReadAsync());
GolemStatus? status = GolemStatus.Starting;
while ((status = await statusChannel.Reader.ReadAsync()) == GolemStatus.Starting)
{
_logger.LogInformation("Still starting");
}
Assert.Equal(status, GolemStatus.Ready);

Assert.Null(golem.CurrentJob);

_logger.LogInformation("Starting App");
var app = _requestor?.CreateSampleApp() ?? throw new Exception("Requestor not started yet");
Assert.True(app.Start());

var jobStartCounter = 0;
IJob? job = null;
while ((job = await jobChannel.Reader.ReadAsync()) == null && jobStartCounter++ < 100)
{
_logger.LogInformation("Still no job");
}
_logger.LogInformation($"Got a job. Status {golem.CurrentJob?.Status}, Id: {golem.CurrentJob?.Id}, RequestorId: {golem.CurrentJob?.RequestorId}");

Assert.NotNull(golem.CurrentJob);
Assert.Equal(golem.CurrentJob.RequestorId, _requestor?.AppKey?.Id);
Assert.Equal(golem.CurrentJob?.Status, JobStatus.Idle);

_logger.LogInformation("Stopping App");
await app.Stop(StopMethod.SigInt);

var jobStopCounter = 0;
while ((job = await jobChannel.Reader.ReadAsync()) != null && jobStopCounter++ < 100)
{
_logger.LogInformation($"Still has a job. Status: {job.Status}, Id: {job.Id}, RequestorId: {job.RequestorId}");
}
_logger.LogInformation("No more jobs");
Assert.Null(golem.CurrentJob);

_logger.LogInformation("Stopping Golem");
await golem.Stop();

var golemStopCounter = 0;
while ((status = await statusChannel.Reader.ReadAsync()) == GolemStatus.Ready && golemStopCounter++ < 100)
{
_logger.LogInformation("Still stopping");
}
Assert.Equal(status, GolemStatus.Off);
}

public async Task DisposeAsync()
{
if (_requestor != null)
{
await _requestor.Stop(StopMethod.SigInt);
}
if (_relay != null)
{
await _relay.Stop(StopMethod.SigInt);
}
}

public void Dispose()
{
XunitContext.Flush();
}
}
}
Empty file.
Loading

0 comments on commit a0a1f44

Please sign in to comment.