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

Move connection probing to CosmosTestStore #24845

Merged
merged 1 commit into from
May 5, 2021
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 @@ -2,10 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;

Expand All @@ -17,62 +13,6 @@ public class CosmosDbConfiguredConditionAttribute : Attribute, ITestCondition
public string SkipReason
=> "Unable to connect to Cosmos DB. Please install/start the emulator service or configure a valid endpoint.";

private static bool? _connectionAvailable;

public async ValueTask<bool> IsMetAsync()
{
if (_connectionAvailable == null)
{
_connectionAvailable = await TryConnectAsync();
}

return _connectionAvailable.Value;
}

private static async Task<bool> TryConnectAsync()
{
CosmosTestStore testStore = null;
try
{
testStore = CosmosTestStore.CreateInitialized("NonExistent");

return true;
}
catch (AggregateException aggregate)
{
if (aggregate.Flatten().InnerExceptions.Any(IsNotConfigured))
{
return false;
}

throw;
}
catch (Exception e)
{
if (IsNotConfigured(e))
{
return false;
}

throw;
}
finally
{
if (testStore != null)
{
await testStore.DisposeAsync();
}
}
}

private static bool IsNotConfigured(Exception exception)
=> exception switch
{
HttpRequestException re => re.InnerException is SocketException // Exception in Mac/Linux
|| (re.InnerException is IOException ioException && ioException.InnerException is SocketException), // Exception in Windows
_ => exception.Message.Contains(
"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used.",
StringComparison.Ordinal),
};
public ValueTask<bool> IsMetAsync() => CosmosTestStore.IsConnectionAvailableAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
Expand All @@ -27,6 +30,7 @@ public class CosmosTestStore : TestStore
private bool _initialized;

private static readonly Guid _runId = Guid.NewGuid();
private static bool? _connectionAvailable;

public static CosmosTestStore Create(string name, Action<CosmosDbContextOptionsBuilder> extensionConfiguration = null)
=> new(name, shared: false, extensionConfiguration: extensionConfiguration);
Expand Down Expand Up @@ -87,9 +91,71 @@ public override DbContextOptionsBuilder AddProviderOptions(DbContextOptionsBuild
Name,
_configureCosmos);

public static async ValueTask<bool> IsConnectionAvailableAsync()
{
if (_connectionAvailable == null)
{
_connectionAvailable = await TryConnectAsync();
}

return _connectionAvailable.Value;
}

private static async Task<bool> TryConnectAsync()
{
CosmosTestStore testStore = null;
try
{
testStore = CreateInitialized("NonExistent");

return true;
}
catch (AggregateException aggregate)
{
if (aggregate.Flatten().InnerExceptions.Any(IsNotConfigured))
{
return false;
}

throw;
}
catch (Exception e)
{
if (IsNotConfigured(e))
{
return false;
}

throw;
}
finally
{
if (testStore != null)
{
await testStore.DisposeAsync();
}
}
}

private static bool IsNotConfigured(Exception exception)
=> exception switch
{
HttpRequestException re => re.InnerException is SocketException // Exception in Mac/Linux
|| (re.InnerException is IOException ioException && ioException.InnerException is SocketException), // Exception in Windows
_ => exception.Message.Contains(
"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used.",
StringComparison.Ordinal),
};

protected override void Initialize(Func<DbContext> createContext, Action<DbContext> seed, Action<DbContext> clean)
{
_initialized = true;

if (_connectionAvailable == false)
{
return;
}

if (_dataFilePath == null)
{
base.Initialize(createContext ?? (() => _storeContext), seed, clean);
Expand Down Expand Up @@ -235,6 +301,11 @@ public override async Task DisposeAsync()
if (_initialized
&& _dataFilePath == null)
{
if (_connectionAvailable == false)
{
return;
}

if (Shared)
{
GetTestStoreIndex(ServiceProvider).RemoveShared(GetType().Name + Name);
Expand Down