Skip to content

Commit

Permalink
chore: add eventual consistency test (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
nand4011 authored Dec 2, 2024
1 parent acdf319 commit 7a5e44a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
45 changes: 45 additions & 0 deletions tests/Integration/Momento.Sdk.Tests/Cache/ReplicaReadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using System.Threading.Tasks;

namespace Momento.Sdk.Tests.Integration.Cache;

[Collection("CacheClient")]
public class ReplicaReadTest : TestBase
{
private new readonly ICacheClient client;
public ReplicaReadTest(CacheClientFixture fixture) : base(fixture)
{
client = fixture.ClientWithBalancedReads;
}

[Fact]
public async Task LatestValueAfterReplicationDelay()
{
const int numTrials = 10;
const int delayBetweenTrials = 100;
const int replicationDelayMs = 1000;
var random = new Random();

var trials = Enumerable.Range(0, numTrials).Select(async trialNumber =>
{
var startDelay = (trialNumber + 1) * delayBetweenTrials + (random.NextDouble() - 0.5) * 10;
await Task.Delay((int)startDelay);

var key = Utils.NewGuidString();
var value = Utils.NewGuidString();

var setResponse = await client.SetAsync(cacheName, key, value);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");

await Task.Delay(replicationDelayMs);

var getResponse = await client.GetAsync(cacheName, key);
Assert.True(getResponse is CacheGetResponse.Hit, $"Unexpected response: {getResponse}");
var hitResponse = (CacheGetResponse.Hit)getResponse;
string setValue = hitResponse.ValueString;
Assert.Equal(value, setValue);
});

await Task.WhenAll(trials);
}
}
22 changes: 12 additions & 10 deletions tests/Integration/Momento.Sdk.Tests/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace Momento.Sdk.Tests.Integration;
/// </summary>
public class CacheClientFixture : IDisposable
{
public ICacheClient Client { get; private set; }
public ICredentialProvider AuthProvider { get; private set; }
public string CacheName { get; private set; }
public ICacheClient Client { get; }
public ICacheClient ClientWithConsistentReads { get; }
public ICacheClient ClientWithBalancedReads { get; }
public ICredentialProvider AuthProvider { get; }
public string CacheName { get; }

public TimeSpan DefaultTtl { get; private set; } = TimeSpan.FromSeconds(10);
public TimeSpan DefaultTtl { get; } = TimeSpan.FromSeconds(10);

public CacheClientFixture()
{
Expand All @@ -35,14 +37,14 @@ public CacheClientFixture()
builder.AddFilter("Grpc.Net.Client", LogLevel.Error);
builder.SetMinimumLevel(LogLevel.Information);
}));

if (consistentReads)
{
config = config.WithReadConcern(ReadConcern.Consistent);
}
var configWithConsistentReads = config.WithReadConcern(ReadConcern.Consistent);

ClientWithBalancedReads = new CacheClient(config, AuthProvider, defaultTtl: DefaultTtl);
ClientWithConsistentReads = new CacheClient(configWithConsistentReads, AuthProvider, defaultTtl: DefaultTtl);
Client = consistentReads ? ClientWithConsistentReads : ClientWithBalancedReads;

CacheName = $"dotnet-integration-{Utils.NewGuidString()}";
Client = new CacheClient(config, AuthProvider, defaultTtl: DefaultTtl);

Utils.CreateCacheForTest(Client, CacheName);
}

Expand Down

0 comments on commit 7a5e44a

Please sign in to comment.