Skip to content
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
36 changes: 34 additions & 2 deletions Enyim.Caching.Tests/MemcachedClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace Enyim.Caching.Tests
{
public abstract class MemcachedClientTestsBase
{
protected MemcachedClient _client;

public MemcachedClientTestsBase()
public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
{
IServiceCollection services = new ServiceCollection();
services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
services.AddEnyimMemcached(options =>
{
options.AddServer("memcached", 11211);
if (onAddEnyimMemcached != null)
{
onAddEnyimMemcached(options);
}
});

services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());
IServiceProvider serviceProvider = services.BuildServiceProvider();
_client = serviceProvider.GetService<IMemcachedClient>() as MemcachedClient;
Expand Down Expand Up @@ -63,6 +72,20 @@ protected IStoreOperationResult Store(StoreMode mode = StoreMode.Set, string key
return _client.ExecuteStore(mode, key, value);
}

protected Task<bool> StoreAsync(StoreMode mode = StoreMode.Set, string key = null, object value = null)
{
if (string.IsNullOrEmpty(key))
{
key = GetUniqueKey("store");
}

if (value == null)
{
value = GetRandomString();
}
return _client.StoreAsync(mode, key, value, TimeSpan.MaxValue);
}

protected void StoreAssertPass(IStoreOperationResult result)
{
Assert.True(result.Success, "Success was false");
Expand Down Expand Up @@ -95,6 +118,15 @@ protected void GetAssertFail(IGetOperationResult result)
Assert.Null(result.Value);
}

protected void GetAssertFail(IGetOperationResult<object> result)
{
Assert.False(result.Success, "Success was true");
Assert.Equal((ulong)0, result.Cas);
Assert.True(result.StatusCode > 0, "StatusCode not greater than 0");
Assert.False(result.HasValue, "HasValue was true");
Assert.Null(result.Value);
}

protected void MutateAssertPass(IMutateOperationResult result, ulong expectedValue)
{
Assert.True(result.Success, "Success was false");
Expand Down
81 changes: 81 additions & 0 deletions Enyim.Caching.Tests/MemcachedClientWithKeyTransformerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Xunit;

namespace Enyim.Caching.Tests
{
public class MemcachedClientWithKeyTransformerTests : MemcachedClientTestsBase
{
public MemcachedClientWithKeyTransformerTests(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
: base(options =>
{
options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer";
if (onAddEnyimMemcached != null)
{
onAddEnyimMemcached(options);
}
})
{
}

[Fact]
public void When_Removing_A_Valid_Transformed_Key_Is_Successful()
{
var key = GetUniqueKey("remove");
var storeResult = Store(key: key);
StoreAssertPass(storeResult);

var removeResult = _client.ExecuteRemove(key);
Assert.True(removeResult.Success, "Success was false");
Assert.True((removeResult.StatusCode ?? 0) == 0, "StatusCode was neither null nor 0");

var getResult = _client.ExecuteGet(key);
GetAssertFail(getResult);
}


[Fact]
public async Task When_Removing_A_Valid_Transformed_Key_Is_Successful_Async()
{
var key = GetUniqueKey("remove");
var storeResult = await StoreAsync(key: key);
Assert.True(storeResult, "Success was false");

var removeResult = await _client.RemoveAsync(key);

Assert.True(removeResult, "Success was false");

var getResult = await _client.GetAsync<object>(key);
GetAssertFail(getResult);
}

[Fact]
public void When_Getting_Existing_Item_Value_With_Transformed_Key_Is_Not_Null_And_Result_Is_Successful()
{
var key = GetUniqueKey("get");
var value = GetRandomString();
var storeResult = Store(key: key, value: value);
StoreAssertPass(storeResult);

var getResult = _client.ExecuteGet(key);
GetAssertPass(getResult, value);
}

[Fact]
public void When_Storing_Item_With_With_Transformed_Key_And_Valid_Cas_Result_Is_Successful()
{
var key = GetUniqueKey("cas");
var value = GetRandomString();
var storeResult = Store(StoreMode.Add, key, value);
StoreAssertPass(storeResult);

var casResult = _client.ExecuteCas(StoreMode.Set, key, value, storeResult.Cas);
StoreAssertPass(casResult);
}
}
}
11 changes: 6 additions & 5 deletions Enyim.Caching/MemcachedClient.Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,13 @@ public IConcatOperationResult ExecutePrepend(string key, ulong cas, ArraySegment
/// <returns>true if the item was successfully removed from the cache; false otherwise.</returns>
public IRemoveOperationResult ExecuteRemove(string key)
{
//var hashedKey = this.keyTransformer.Transform(key);
var node = this.pool.Locate(key);
var hashedKey = this.keyTransformer.Transform(key);
var node = this.pool.Locate(hashedKey);
var result = RemoveOperationResultFactory.Create();

if (node != null)
{
var command = this.pool.OperationFactory.Delete(key, 0);
var command = this.pool.OperationFactory.Delete(hashedKey, 0);
var commandResult = node.Execute(command);

if (commandResult.Success)
Expand All @@ -472,12 +472,13 @@ public IRemoveOperationResult ExecuteRemove(string key)

public async Task<IRemoveOperationResult> ExecuteRemoveAsync(string key)
{
var node = this.pool.Locate(key);
var hashedKey = this.keyTransformer.Transform(key);
var node = this.pool.Locate(hashedKey);
var result = RemoveOperationResultFactory.Create();

if (node != null)
{
var command = this.pool.OperationFactory.Delete(key, 0);
var command = this.pool.OperationFactory.Delete(hashedKey, 0);
var commandResult = await node.ExecuteAsync(command);

if (commandResult.Success)
Expand Down
9 changes: 7 additions & 2 deletions Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
result.Value = transcoder.Deserialize<T>(command.Result);
return result;
}
else
{
commandResult.Combine(result);

return result;
}
}
catch (Exception ex)
{
Expand All @@ -208,8 +214,7 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
_logger.LogError($"Unable to locate memcached node");
}

result.Success = false;
result.Value = default(T);
result.Fail("Unable to locate node");
return result;
}

Expand Down