Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review test containers #1554

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class DefaultDebugGremlinQuerySerializationTest : QueryExecutionTest
public sealed class DefaultDebugGremlinQuerySerializationTest : QueryExecutionTest, IClassFixture<GremlinqFixture>
{
public DefaultDebugGremlinQuerySerializationTest(ITestOutputHelper testOutputHelper) : base(
GremlinqFixture.Empty,
public DefaultDebugGremlinQuerySerializationTest(GremlinqFixture fixture, ITestOutputHelper testOutputHelper) : base(
fixture,
new DebugGremlinQueryVerifier(),
testOutputHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class BytecodeQuerySerializationTest : QueryExecutionTest
public sealed class BytecodeQuerySerializationTest : QueryExecutionTest, IClassFixture<GremlinqFixture>
{
public BytecodeQuerySerializationTest(ITestOutputHelper testOutputHelper) : base(GremlinqFixture.Empty, new SerializingVerifier<Bytecode>(), testOutputHelper)
public BytecodeQuerySerializationTest(GremlinqFixture fixture, ITestOutputHelper testOutputHelper) : base(fixture, new SerializingVerifier<Bytecode>(), testOutputHelper)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class Graphson2GremlinQuerySerializationTest : QueryExecutionTest
public sealed class Graphson2GremlinQuerySerializationTest : QueryExecutionTest, IClassFixture<GremlinqFixture>
{
private static readonly GraphSON2Writer GraphSon2Writer = new ();

public Graphson2GremlinQuerySerializationTest(ITestOutputHelper testOutputHelper) : base(
GremlinqFixture.Empty,
public Graphson2GremlinQuerySerializationTest(GremlinqFixture fixture, ITestOutputHelper testOutputHelper) : base(
fixture,
new GraphSonStringSerializingVerifier(GraphSon2Writer),
testOutputHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class Graphson3GremlinQuerySerializationTest : QueryExecutionTest
public sealed class Graphson3GremlinQuerySerializationTest : QueryExecutionTest, IClassFixture<GremlinqFixture>
{
private static readonly GraphSON3Writer GraphSon3Writer = new ();

public Graphson3GremlinQuerySerializationTest(ITestOutputHelper testOutputHelper) : base(
GremlinqFixture.Empty,
public Graphson3GremlinQuerySerializationTest(GremlinqFixture fixture, ITestOutputHelper testOutputHelper) : base(
fixture,
new GraphSonStringSerializingVerifier(GraphSon3Writer),
testOutputHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class Graphson3WithGroovyGremlinQuerySerializationTest : QueryExecutionTest
public sealed class Graphson3WithGroovyGremlinQuerySerializationTest : QueryExecutionTest, IClassFixture<GroovyGremlinQuerySerializationFixture>
{
private static readonly GraphSON3Writer GraphSon3Writer = new();

public Graphson3WithGroovyGremlinQuerySerializationTest(ITestOutputHelper testOutputHelper) : base(
new GroovyGremlinQuerySerializationFixture(),
public Graphson3WithGroovyGremlinQuerySerializationTest(GroovyGremlinQuerySerializationFixture fixture, ITestOutputHelper testOutputHelper) : base(
fixture,
new GraphSonStringSerializingVerifier(GraphSon3Writer),
testOutputHelper)
{
Expand Down
4 changes: 2 additions & 2 deletions test/Core.Tests/Serialization/OuterProjectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ExRam.Gremlinq.Core.Tests
{
public sealed class OuterProjectionTest : QueryExecutionTest
public sealed class OuterProjectionTest : QueryExecutionTest, IClassFixture<GremlinqFixture>
{
private sealed class ProjectionVerifier : GremlinQueryVerifier
{
Expand All @@ -16,7 +16,7 @@ public ProjectionVerifier(Func<SettingsTask, SettingsTask>? settingsTaskModifier
public override SettingsTask Verify<TElement>(IGremlinQueryBase<TElement> query) => InnerVerify(query.ToTraversal().Projection.ToTraversal(query.AsAdmin().Environment).Steps.ToArray());
}

public OuterProjectionTest(ITestOutputHelper testOutputHelper) : base(GremlinqFixture.Empty, new ProjectionVerifier(), testOutputHelper)
public OuterProjectionTest(GremlinqFixture fixture, ITestOutputHelper testOutputHelper) : base(fixture, new ProjectionVerifier(), testOutputHelper)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override async Task<IGremlinQuerySource> TransformQuerySource(IGremlin

public NoPasswordIntegrationTests(NoPasswordFixture fixture) : base(new JTokenExecutingVerifier())
{
_g = fixture.GremlinQuerySource.Result;
_g = fixture.G;
}

[Fact(Skip = "CosmosDb often seems to close the connection right after an Authenticate status.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override async Task<IGremlinQuerySource> TransformQuerySource(IContain

public WrongPasswordIntegrationTests(WrongPasswordGremlinServerContainerFixture fixture) : base(new JTokenExecutingVerifier())
{
_g = fixture.GremlinQuerySource.Result;
_g = fixture.G;
}

[Fact]
Expand Down
79 changes: 19 additions & 60 deletions test/Tests.Fixtures/GremlinqFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,33 @@

namespace ExRam.Gremlinq.Tests.Fixtures
{
public abstract class GremlinqFixture : IAsyncLifetime
public class GremlinqFixture : IAsyncLifetime
{
private sealed class EmptyGremlinqTestFixture : GremlinqFixture
{
protected override async Task<IGremlinQuerySource> TransformQuerySource(IGremlinQuerySource g) => g;
}
private IGremlinQuerySource? _g;

public static readonly GremlinqFixture Empty = new EmptyGremlinqTestFixture();
protected virtual async Task<IGremlinQuerySource> TransformQuerySource(IGremlinQuerySource g) => g;

private static readonly TaskCompletionSource<IGremlinQuerySource> Disposed = new ();
private TaskCompletionSource<IGremlinQuerySource>? _lazyQuerySource;

static GremlinqFixture()
public virtual async Task InitializeAsync()
{
Disposed.TrySetException(new ObjectDisposedException(nameof(GremlinqFixture)));
_g = await TransformQuerySource(g
.ConfigureEnvironment(env => env
.ConfigureOptions(options => options
.SetValue(GremlinqOption.StringComparisonTranslationStrictness,
StringComparisonTranslationStrictness.Lenient))));

_g = _g
.ConfigureEnvironment(env => env
.ConfigureModel(model => model == GraphModel.Invalid
? GraphModel.FromBaseTypes<Vertex, Edge>()
: model));
}

protected abstract Task<IGremlinQuerySource> TransformQuerySource(IGremlinQuerySource g);

public Task<IGremlinQuerySource> GremlinQuerySource => GetGremlinQuerySource();

private async Task<IGremlinQuerySource> GetGremlinQuerySource()
public virtual async Task DisposeAsync()
{
if (Volatile.Read(ref _lazyQuerySource) is { } tcs)
return await tcs.Task;

var newTcs = new TaskCompletionSource<IGremlinQuerySource>();

if (Interlocked.CompareExchange(ref _lazyQuerySource, newTcs, null) == null)
{
try
{
var g1 = await TransformQuerySource(g
.ConfigureEnvironment(env => env
.ConfigureOptions(options => options
.SetValue(GremlinqOption.StringComparisonTranslationStrictness, StringComparisonTranslationStrictness.Lenient))));

newTcs.TrySetResult(g1
.ConfigureEnvironment(env => env
.ConfigureModel(model => model == GraphModel.Invalid
? GraphModel.FromBaseTypes<Vertex, Edge>()
: model)));
}
catch (Exception ex)
{
newTcs.TrySetException(ex);

Interlocked.CompareExchange(ref _lazyQuerySource, null, newTcs);
}

return await newTcs.Task;
}
else
return await GetGremlinQuerySource();
if (_g is IAsyncDisposable disposable)
await disposable.DisposeAsync();
}

public async Task InitializeAsync()
{

}

public async Task DisposeAsync()
{
if (Interlocked.Exchange(ref _lazyQuerySource, Disposed) is { } tcs && tcs != Disposed)
{
if (await tcs.Task is IAsyncDisposable disposable)
await disposable.DisposeAsync();
}
}
public IGremlinQuerySource G => _g ?? throw new InvalidOperationException();
}
}
8 changes: 2 additions & 6 deletions test/Tests.Fixtures/QueryExecutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ public abstract class QueryExecutionTest : GremlinqTestBase
{
private static readonly string Id = "id";

private readonly Lazy<IGremlinQuerySource> _lazyGremlinQuerySource;
protected readonly IGremlinQuerySource _g;

protected QueryExecutionTest(GremlinqFixture fixture, GremlinQueryVerifier verifier, ITestOutputHelper testOutputHelper) : base(verifier)
{
_lazyGremlinQuerySource = new Lazy<IGremlinQuerySource>(
() => fixture.GremlinQuerySource.Result,
LazyThreadSafetyMode.PublicationOnly);
_g = fixture.G;
}

protected IGremlinQuerySource _g => _lazyGremlinQuerySource.Value;

[Fact]
public virtual Task AddE_from_StepLabel() => _g
.AddV(new Country { CountryCallingCode = "+49" })
Expand Down
26 changes: 22 additions & 4 deletions test/Tests.Fixtures/TestContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,22 @@ async ValueTask IAsyncDisposable.DisposeAsync()

private readonly int _port;

private IContainer? _container;

protected TestContainerFixtureBase(int port = 8182)
{
_port = port;
}

protected override sealed async Task<IGremlinQuerySource> TransformQuerySource(IGremlinQuerySource g)
protected sealed override async Task<IGremlinQuerySource> TransformQuerySource(IGremlinQuerySource g)
{
if (_container is { } container)
return await TransformQuerySource(_container, new ContainerAttachedGremlinQuerySource(container, g));

throw new InvalidOperationException();
}

public override async Task InitializeAsync()
{
var containerBuilder = new ContainerBuilder()
.WithImage(await GetImage())
Expand All @@ -119,13 +129,21 @@ protected override sealed async Task<IGremlinQuerySource> TransformQuerySource(I
.ForUnixContainer()
.UntilPortIsAvailable(_port));

var container = CustomizeContainer(containerBuilder)
_container = CustomizeContainer(containerBuilder)
.Build();

await container
await _container
.StartAsync();

return await TransformQuerySource(container, new ContainerAttachedGremlinQuerySource(container, g));
await base.InitializeAsync();
}

public override async Task DisposeAsync()
{
if (_container is { } container)
await container.StopAsync();

await base.DisposeAsync();
}

protected abstract Task<IImage> GetImage();
Expand Down
Loading