Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Vera committed Oct 24, 2022
1 parent 1279ada commit d9cb32c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Fabric;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Internal;
using Microsoft.ServiceFabric.Data;
using Microsoft.ServiceFabric.Data.Collections;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Remoting.V2;
using Microsoft.ServiceFabric.Services.Remoting.V2.FabricTransport.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;
using System;
using System.Collections.Generic;
using System.Fabric;
using System.Threading;
using System.Threading.Tasks;

namespace SoCreate.Extensions.Caching.ServiceFabric
{
Expand All @@ -28,13 +29,15 @@ public abstract class DistributedCacheStoreService : StatefulService, IServiceFa
private readonly Action<string> _log;
private readonly ISystemClock _systemClock;
private int _partitionCount = 1;
protected IServiceRemotingMessageSerializationProvider _serializationProvider;

public DistributedCacheStoreService(StatefulServiceContext context, Action<string> log = null)
public DistributedCacheStoreService(StatefulServiceContext context, Action<string> log = null, IServiceRemotingMessageSerializationProvider serializationProvider = null)
: base(context)
{
_serviceUri = context.ServiceName;
_log = log;
_systemClock = new SystemClock();
_serializationProvider = serializationProvider;

if (!StateManager.TryAddStateSerializer(new CachedItemSerializer()))
{
Expand Down Expand Up @@ -90,22 +93,22 @@ public async Task<byte[]> GetCachedItemAsync(string key)

return null;
}

public async Task SetCachedItemAsync(string key, byte[] value, TimeSpan? slidingExpiration, DateTimeOffset? absoluteExpiration)
{
if (slidingExpiration.HasValue)
{
var now = _systemClock.UtcNow;
absoluteExpiration = now.AddMilliseconds(slidingExpiration.Value.TotalMilliseconds);
absoluteExpiration = now.AddMilliseconds(slidingExpiration.Value.TotalMilliseconds);
}

var cacheStore = await StateManager.GetOrAddAsync<IReliableDictionary<string, CachedItem>>(CacheStoreName);
var cacheStoreMetadata = await StateManager.GetOrAddAsync<IReliableDictionary<string, CacheStoreMetadata>>(CacheStoreMetadataName);

await RetryHelper.ExecuteWithRetry(StateManager, async (tx, cancellationToken, state) =>
await RetryHelper.ExecuteWithRetry(StateManager, async (tx, cancellationToken, state) =>
{
_log?.Invoke($"Set cached item called with key: {key} on partition id: {Partition?.PartitionInfo.Id}");

Func<string, Task<ConditionalValue<CachedItem>>> getCacheItem = async (string cacheKey) => await cacheStore.TryGetValueAsync(tx, cacheKey, LockMode.Update);
var linkedDictionaryHelper = new LinkedDictionaryHelper(getCacheItem, ByteSizeOffset);

Expand Down Expand Up @@ -165,7 +168,7 @@ await RetryHelper.ExecuteWithRetry(StateManager, async (tx, cancellationToken, s
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
yield return new ServiceReplicaListener(context =>
new FabricTransportServiceRemotingListener(context, this), ListenerName);
new FabricTransportServiceRemotingListener(context, this, serializationProvider: _serializationProvider), ListenerName);
}

protected override async Task RunAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -193,11 +196,11 @@ protected async Task RemoveLeastRecentlyUsedCacheItemWhenOverMaxCacheSize(Cancel

await RetryHelper.ExecuteWithRetry(StateManager, async (tx, cancelToken, state) =>
{
var metadata = await cacheStoreMetadata.TryGetValueAsync(tx, CacheStoreMetadataKey, LockMode.Update);
var metadata = await cacheStoreMetadata.TryGetValueAsync(tx, CacheStoreMetadataKey, LockMode.Update);

if (metadata.HasValue && !string.IsNullOrEmpty(metadata.Value.FirstCacheKey))
{
_log?.Invoke($"Size: {metadata.Value.Size} Max Size: {GetMaxSizeInBytes()}");
if (metadata.HasValue && !string.IsNullOrEmpty(metadata.Value.FirstCacheKey))
{
_log?.Invoke($"Size: {metadata.Value.Size} Max Size: {GetMaxSizeInBytes()}");

if (metadata.Value.Size > GetMaxSizeInBytes())
{
Expand Down Expand Up @@ -252,7 +255,7 @@ private async Task ApplyChanges(ITransaction tx, IReliableDictionary<string, Cac
{
await cachedItemStore.SetAsync(tx, cacheItem.Key, cacheItem.Value);
}

await cacheStoreMetadata.SetAsync(tx, CacheStoreMetadataKey, linkedDictionaryItemsChanged.CacheStoreMetadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<PackageReference Include="AutoFixture.Xunit2" Version="4.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Microsoft.ServiceFabric" Version="9.0.*" />
<PackageReference Include="Microsoft.ServiceFabric.Actors" Version="6.0.1017" />
<PackageReference Include="Microsoft.ServiceFabric.Actors" Version="6.0.1121" />
<PackageReference Include="Microsoft.ServiceFabric.Services.Remoting" Version="6.0.1121" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="ServiceFabric.Mocks" Version="6.0.*" />
<PackageReference Include="ServiceFabric.Mocks" Version="6.1.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit d9cb32c

Please sign in to comment.