Skip to content

Commit

Permalink
fix on force return value flag. Fixes infinispan#47
Browse files Browse the repository at this point in the history
  • Loading branch information
rigazilla committed Mar 8, 2024
1 parent 28fe945 commit 6a3db0a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
33 changes: 33 additions & 0 deletions Infinispan.Hotrod.Core.XUnitTest/DefaultCacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public async void PutTest()
await _cache.Put(key2, "chlorine");
Assert.Equal(initialSize + 2, await _cache.Size());
Assert.Equal("chlorine", await _cache.Get(key2));

_cache.ForceReturnValue=true;
String key3 = UniqueKey.NextKey();
String oldVal = await _cache.Put(key3, "oxygen");
Assert.Null(oldVal);
oldVal = await _cache.Put(key3, "sodium");
Assert.Equal("oxygen", oldVal);
}

[Fact]
Expand Down Expand Up @@ -417,6 +424,32 @@ public async void PutAllTest()
Assert.Equal("v3", await _cache.Get(key3));
}


[Fact]
public async void PutIfAbsentTest()
{
String key1 = UniqueKey.NextKey();
Int32 initialSize = await _cache.Size();

await _cache.PutIfAbsent(key1, "boron");
Assert.Equal(initialSize + 1, await _cache.Size());
Assert.Equal("boron", await _cache.Get(key1));

await _cache.PutIfAbsent(key1, "chlorine");
Assert.Equal(initialSize + 1, await _cache.Size());
Assert.Equal("boron", await _cache.Get(key1));

_cache.ForceReturnValue = true;
String key2 = UniqueKey.NextKey();
String oldVal = await _cache.PutIfAbsent(key2, "boron2");
Assert.Equal(initialSize + 2, await _cache.Size());
Assert.Null(oldVal);
Assert.Equal("boron2", await _cache.Get(key2));
oldVal = await _cache.PutIfAbsent(key2, "chlorine2");
Assert.Equal(initialSize + 2, await _cache.Size());
Assert.Equal(oldVal, await _cache.Get(key2));
}

// [Test]
// public void ContainsValueTest()
// {
Expand Down
2 changes: 1 addition & 1 deletion src/InfinispanCommands/PUTIFABSENT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal override void Execute(CommandContext ctx, InfinispanClient client, Pipe
}
public override Result OnReceive(InfinispanRequest request, ResponseStream stream)
{
if ((request.Command.Flags & 0x01) == 1)
if ((request.Command.Flags & 0x01) == 1 && request.ResponseStatus == 0x04)
{
PrevValue = ValueMarshaller.unmarshall(Codec.readArray(stream));
return new Result { Status = ResultStatus.Completed, ResultType = ResultType.Object };
Expand Down
4 changes: 2 additions & 2 deletions src/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public StringMarshaller(Encoding enc = null)
}
public override byte[] marshall(string t)
{
return t == null ? null : Encoding.GetBytes(t);
return ((t == null) || (t.Length==0)) ? null : Encoding.GetBytes(t);
}
public override string unmarshall(byte[] buff)
{
return buff == null ? null : Encoding.GetString(buff);
return ((buff == null) || (buff.Length==0)) ? null : Encoding.GetString(buff);
}
}
}

0 comments on commit 6a3db0a

Please sign in to comment.