Skip to content
Open
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
51 changes: 51 additions & 0 deletions src/Scaler.Tests/CosmosDbFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Threading;
using Microsoft.Azure.Cosmos;
using Xunit;

namespace Keda.CosmosDb.Scaler.Tests
{
public class CosmosDbFactoryTests
{
private const string DummyConnection1 = "AccountEndpoint=https://example1.com:443/;AccountKey=ZHVtbXkx";
private const string DummyConnection2 = "AccountEndpoint=https://example2.com:443/;AccountKey=ZHVtbXky";

[Fact]
public void GetCosmosClient_OnlyConstructsOnceForSameKey()
{
var factory = new CountingCosmosDbFactory();

CosmosClient c1 = factory.GetCosmosClient(DummyConnection1, useCredentials: false, clientId: null);
CosmosClient c2 = factory.GetCosmosClient(DummyConnection1, useCredentials: false, clientId: null);
CosmosClient c3 = factory.GetCosmosClient(DummyConnection1, useCredentials: false, clientId: null);

Assert.Same(c1, c2);
Assert.Same(c2, c3);
Assert.Equal(1, factory.CreateCount);
}

[Fact]
public void GetCosmosClient_ConstructsOncePerDistinctKey()
{
var factory = new CountingCosmosDbFactory();

factory.GetCosmosClient(DummyConnection1, useCredentials: false, clientId: null);
factory.GetCosmosClient(DummyConnection2, useCredentials: false, clientId: null);
factory.GetCosmosClient(DummyConnection1, useCredentials: false, clientId: null);
factory.GetCosmosClient(DummyConnection2, useCredentials: false, clientId: null);

Assert.Equal(2, factory.CreateCount);
}

private sealed class CountingCosmosDbFactory : CosmosDbFactory
{
public int CreateCount;

protected internal override CosmosClient CreateCosmosClient(
string endpointOrConnection, bool useCredentials, string clientId)
{
Interlocked.Increment(ref CreateCount);
return base.CreateCosmosClient(endpointOrConnection, useCredentials, clientId);
}
}
}
}
8 changes: 5 additions & 3 deletions src/Scaler/Services/CosmosDbFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Keda.CosmosDb.Scaler
{
internal sealed class CosmosDbFactory
internal class CosmosDbFactory
{
private const string _applicationName = "keda-external-azure-cosmos-db";
// As per https://docs.microsoft.com/dotnet/api/microsoft.azure.cosmos.cosmosclient, it is recommended to
Expand All @@ -14,10 +14,12 @@ internal sealed class CosmosDbFactory

public CosmosClient GetCosmosClient(string endpointOrConnection, bool useCredentials, string clientId)
{
return _cosmosClientCache.GetOrAdd((endpointOrConnection, clientId), CreateCosmosClient(endpointOrConnection, useCredentials, clientId));
return _cosmosClientCache.GetOrAdd(
(endpointOrConnection, clientId),
_ => CreateCosmosClient(endpointOrConnection, useCredentials, clientId));
}

private CosmosClient CreateCosmosClient(string endpointOrConnection, bool useCredentials, string clientId)
protected internal virtual CosmosClient CreateCosmosClient(string endpointOrConnection, bool useCredentials, string clientId)
{
if (useCredentials)
{
Expand Down