Skip to content

Commit

Permalink
chore: Ensure Hass is in RUNNING state before running the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vicfergar committed Nov 4, 2024
1 parent 503f7aa commit bd96ff6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"RGBWW",
"RSSI",
"Speedtestdotnet",
"Testcontainers",
"Webos",
"zigbee"
],
Expand Down
7 changes: 6 additions & 1 deletion src/HassClient.WS.Tests/BaseHassWSApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json.Serialization;
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace HassClient.WS.Tests
Expand All @@ -11,12 +12,14 @@ public abstract class BaseHassWSApiTest
public const string TestsInstanceBaseUrlVar = "TestsInstanceBaseUrl";
public const string TestsAccessTokenVar = "TestsAccessToken";

protected readonly CancellationTokenSource cts;
private readonly ConnectionParameters connectionParameters;

protected HassWSApi hassWSApi;

public BaseHassWSApiTest()
{
this.cts = new CancellationTokenSource();
var instanceBaseUrl = Environment.GetEnvironmentVariable(TestsInstanceBaseUrlVar);
var accessToken = Environment.GetEnvironmentVariable(TestsAccessTokenVar);

Expand All @@ -37,7 +40,7 @@ public BaseHassWSApiTest()
protected virtual async Task OneTimeSetUp()
{
this.hassWSApi = new HassWSApi();
await this.hassWSApi.ConnectAsync(this.connectionParameters);
await this.hassWSApi.ConnectAsync(this.connectionParameters, cancellationToken: this.cts.Token);

HassSerializer.DefaultSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Error;
HassSerializer.DefaultSettings.Error += this.HassSerializerError;
Expand All @@ -57,6 +60,8 @@ private void HassSerializerError(object sender, ErrorEventArgs args)
[OneTimeTearDown]
protected virtual Task OneTimeTearDown()
{
this.cts?.Cancel();
this.cts?.Dispose();
return Task.CompletedTask;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/HassClient.WS.Tests/ConfigurationApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void ConfigurationHasConfigSource()
}

[Test]
[Retry(3)]
public void ConfigurationHasLocation()
{
Assert.NotNull(this.configuration.LocationName);
Expand All @@ -67,7 +66,6 @@ public void ConfigurationHasLocation()
}

[Test]
[Retry(3)]
public void ConfigurationHasState()
{
Assert.AreEqual("RUNNING", this.configuration.State);
Expand Down
38 changes: 38 additions & 0 deletions src/HassClient.WS.Tests/EnvironmentSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;

namespace HassClient.WS.Tests
{
[SetUpFixture]
public class EnvironmentSetup
{
private readonly CancellationTokenSource cts = new CancellationTokenSource();

private IContainer hassContainer;

[OneTimeSetUp]
Expand Down Expand Up @@ -50,15 +53,50 @@ public async Task GlobalSetupAsync()
Environment.SetEnvironmentVariable(BaseHassWSApiTest.TestsInstanceBaseUrlVar, $"http://localhost:{mappedPort}");
Environment.SetEnvironmentVariable(BaseHassWSApiTest.TestsAccessTokenVar, accessToken);
}

await this.EnsureHassIsRunningAsync();
}

[OneTimeTearDown]
public async Task GlobalTeardown()
{
this.cts.Cancel();
this.cts.Dispose();

if (this.hassContainer != null)
{
await this.hassContainer.DisposeAsync();
}
}

private async Task EnsureHassIsRunningAsync()
{
var instanceBaseUrl = Environment.GetEnvironmentVariable(BaseHassWSApiTest.TestsInstanceBaseUrlVar);
var accessToken = Environment.GetEnvironmentVariable(BaseHassWSApiTest.TestsAccessTokenVar);
var connectionParameters = ConnectionParameters.CreateFromInstanceBaseUrl(instanceBaseUrl, accessToken);

var cancellationToken = this.cts.Token;
var hassWSApi = new HassWSApi();
await hassWSApi.ConnectAsync(connectionParameters, cancellationToken: cancellationToken);

const int maxRetries = 3;
const int delayMilliseconds = 1000;

for (int attempt = 0; attempt < maxRetries; attempt++)
{
var config = await hassWSApi.GetConfigurationAsync(cancellationToken);
if (config?.State == "RUNNING")
{
return;
}

if (attempt < maxRetries - 1)
{
await Task.Delay(delayMilliseconds, cancellationToken);
}
}

Assert.Fail("Home Assistant is not running after multiple attempts");
}
}
}

0 comments on commit bd96ff6

Please sign in to comment.