-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor cosmos emulator class and add xunit fixtures
- Loading branch information
1 parent
14a5950
commit dd95641
Showing
10 changed files
with
195 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.Azure.Cosmos; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net.Sockets; | ||
using Xunit; | ||
|
||
namespace Ephemerally.Azure.Cosmos.Xunit; | ||
|
||
[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")] | ||
public class CosmosClientFixture : | ||
IAsyncDisposable, | ||
IAsyncLifetime | ||
{ | ||
private readonly Lazy<Task<CosmosClient>> _client; | ||
|
||
public CosmosClient Client => _client.Value.Result; | ||
|
||
protected Task<CosmosClient> GetClient() => _client.Value; | ||
|
||
public CosmosClientFixture() | ||
{ | ||
_client = new(CreateClientAsync); | ||
} | ||
|
||
protected virtual Task<CosmosClient> CreateClientAsync() => Task.FromResult(CosmosEmulator.GetClient()); | ||
|
||
public virtual Task InitializeAsync() => _client.Value; | ||
|
||
public virtual async Task DisposeAsync() | ||
{ | ||
if (!_client.IsValueCreated) return; | ||
|
||
await IgnoreSocketException(async () => | ||
{ | ||
var client = await GetClient(); | ||
client.Dispose(); | ||
}); | ||
} | ||
|
||
async ValueTask IAsyncDisposable.DisposeAsync() => | ||
await ((IAsyncLifetime)this).DisposeAsync(); | ||
|
||
protected static async Task IgnoreSocketException(Func<Task> action) | ||
{ | ||
try | ||
{ | ||
await action(); | ||
} | ||
catch (HttpRequestException ex) when (ex.InnerException is SocketException { SocketErrorCode: SocketError.ConnectionRefused }) | ||
{ | ||
// If the emulator or instance is not accessible, we don't need to do anything | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.Azure.Cosmos; | ||
|
||
namespace Ephemerally.Azure.Cosmos.Xunit; | ||
|
||
public static class CosmosEmulator | ||
{ | ||
public static CosmosClient GetClient(Action<CosmosClientOptions> configureOptions = null) | ||
{ | ||
var options = new CosmosClientOptions | ||
{ | ||
RequestTimeout = TimeSpan.FromSeconds(30), | ||
ServerCertificateCustomValidationCallback = (_, _, _) => true, | ||
ConnectionMode = ConnectionMode.Gateway, | ||
LimitToEndpoint = true | ||
}; | ||
configureOptions?.Invoke(options); | ||
return new( | ||
AccountEndpoint, | ||
AuthKey, | ||
options); | ||
} | ||
|
||
public const string | ||
AccountEndpoint = "https://localhost:8081", | ||
AuthKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", | ||
ConnectionString = $"AccountEndpoint={AccountEndpoint};AccountKey={AuthKey};"; | ||
} |
50 changes: 33 additions & 17 deletions
50
src/Ephemerally.Azure.Cosmos.Xunit/EphemeralCosmosContainerFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,43 @@ | ||
using Microsoft.Azure.Cosmos; | ||
using Xunit; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Ephemerally.Azure.Cosmos.Xunit; | ||
|
||
internal abstract class EphemeralCosmosContainerFixture : | ||
IAsyncDisposable, | ||
IAsyncLifetime | ||
[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")] | ||
public class EphemeralCosmosContainerFixture : EphemeralCosmosDatabaseFixture | ||
{ | ||
private Lazy<ValueTask<EphemeralCosmosContainer>> _container; | ||
private readonly Lazy<Task<EphemeralCosmosContainer>> _container; | ||
|
||
protected EphemeralCosmosContainerFixture( | ||
Database database, | ||
EphemeralCreationOptions options) | ||
public EphemeralCosmosContainer Container => _container.Value.Result; | ||
|
||
protected Task<EphemeralCosmosContainer> GetContainer() => _container.Value; | ||
|
||
public EphemeralCosmosContainerFixture() | ||
{ | ||
_container = new(CreateContainerAsync); | ||
} | ||
|
||
protected virtual async Task<EphemeralCosmosContainer> CreateContainerAsync() | ||
{ | ||
_container = new(async () => await database.CreateEphemeralContainerAsync(options).ConfigureAwait(false)); | ||
var db = await GetDatabase(); | ||
return await db.CreateEphemeralContainerAsync(); | ||
} | ||
|
||
async Task IAsyncLifetime.InitializeAsync() => | ||
await _container.Value.ConfigureAwait(false); | ||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
await _container.Value; | ||
} | ||
|
||
async Task IAsyncLifetime.DisposeAsync() => | ||
await ((IAsyncDisposable)this).DisposeAsync().ConfigureAwait(false); | ||
public override async Task DisposeAsync() | ||
{ | ||
if (!_container.IsValueCreated) return; | ||
|
||
ValueTask IAsyncDisposable.DisposeAsync() => | ||
_container.Value.Result.DisposeAsync(); | ||
} | ||
await IgnoreSocketException(async () => | ||
{ | ||
var container = await GetContainer(); | ||
await container.DisposeAsync(); | ||
}); | ||
|
||
await base.DisposeAsync(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Ephemerally.Azure.Cosmos.Xunit/EphemeralCosmosDatabaseFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Ephemerally.Azure.Cosmos.Xunit; | ||
|
||
[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")] | ||
public class EphemeralCosmosDatabaseFixture : CosmosClientFixture | ||
{ | ||
private readonly Lazy<Task<EphemeralCosmosDatabase>> _database; | ||
|
||
public EphemeralCosmosDatabase Database => _database.Value.Result; | ||
|
||
protected Task<EphemeralCosmosDatabase> GetDatabase() => _database.Value; | ||
|
||
public EphemeralCosmosDatabaseFixture() | ||
{ | ||
_database = new(CreateDatabaseAsync); | ||
} | ||
|
||
protected virtual async Task<EphemeralCosmosDatabase> CreateDatabaseAsync() | ||
{ | ||
var client = await GetClient(); | ||
return await client.CreateEphemeralDatabaseAsync(); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
await _database.Value; | ||
} | ||
|
||
public override async Task DisposeAsync() | ||
{ | ||
if (!_database.IsValueCreated) return; | ||
|
||
await IgnoreSocketException(async () => | ||
{ | ||
var db = await GetDatabase(); | ||
await db.DisposeAsync(); | ||
}); | ||
|
||
await base.DisposeAsync(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.