Skip to content

Commit

Permalink
Fix memcached bucket replace with cas.
Browse files Browse the repository at this point in the history
Motivation
----------
The MemcachedBucket.Replace(key, value, cas) untested and doing an Add
instead of a Replace. This patch fixes it and adds the appropriate tests.

Modifications
-------------
MemcachedBucket.Replace(key, value, cas) was corrected and tests were added to
MemcachedBucket.Tests.

Results
-------
MemcachedBucket.Replace(key, value, cas) is fixed and has tests to
assert that it works correctly.

Change-Id: I3a948b701605f9ff03a536da5287c72b7f1ba6b5
Reviewed-on: http://review.couchbase.org/48487
Reviewed-by: Jeffry Morris <jeffrymorris@gmail.com>
Tested-by: Jeffry Morris <jeffrymorris@gmail.com>
  • Loading branch information
benbenwilde authored and jeffrymorris committed Mar 20, 2015
1 parent b9c1b46 commit 9263698
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions Src/Couchbase.Tests/Core/Buckets/MemcachedBucketTests.cs
Expand Up @@ -121,6 +121,45 @@ public void When_Key_Exists_Replace_Succeeds()
}
}

[Test]
public void When_Cas_Has_Changed_Replace_Fails()
{
const string key = "CouchbaseBucket.When_Cas_Has_Changed_Replace_Fails";
using (var bucket = _cluster.OpenBucket("memcached"))
{
bucket.Remove(key);
var set = bucket.Insert(key, "value");
Assert.IsTrue(set.Success);

var upsert = bucket.Upsert(key, "newvalue");
Assert.IsTrue(upsert.Success);

var replace = bucket.Replace(key, "should fail", set.Cas);
Assert.IsFalse(replace.Success);
}
}

[Test]
public void When_Cas_Has_Not_Changed_Replace_Succeeds()
{
const string key = "CouchbaseBucket.When_Cas_Has_Not_Changed_Replace_Succeeds";
using (var bucket = _cluster.OpenBucket("memcached"))
{
bucket.Remove(key);
var set = bucket.Insert(key, "value");
Assert.IsTrue(set.Success);

var get = bucket.Get<string>(key);
Assert.AreEqual(get.Cas, set.Cas);

var replace = bucket.Replace(key, "should succeed", get.Cas);
Assert.True(replace.Success);

get = bucket.Get<string>(key);
Assert.AreEqual("should succeed", get.Value);
}
}

[Test]
public void When_Key_Exists_Delete_Returns_Success()
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Couchbase/MemcachedBucket.cs
Expand Up @@ -369,7 +369,7 @@ public IDocumentResult<T> Replace<T>(IDocument<T> document)
/// <returns>An object implementing the <see cref="IOperationResult{T}"/>interface.</returns>
public IOperationResult<T> Replace<T>(string key, T value, ulong cas)
{
var operation = new Add<T>(key, value, null, _converter, _transcoder, _operationLifespanTimeout);
var operation = new Replace<T>(key, value, cas, null, _converter, _transcoder, _operationLifespanTimeout);
return _requestExecuter.SendWithRetry(operation);
}

Expand Down

0 comments on commit 9263698

Please sign in to comment.