Skip to content

Commit

Permalink
Propagate exception from the C++ core
Browse files Browse the repository at this point in the history
  • Loading branch information
rigazilla committed Aug 10, 2018
1 parent a636243 commit 52abb1c
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 100 deletions.
12 changes: 12 additions & 0 deletions src/Infinispan.HotRod/Exceptions/HotRodClientRollbackException.cs
@@ -0,0 +1,12 @@
namespace Infinispan.HotRod.Exceptions
{
/// <summary>
/// Used to indicated that a commit operation actually rolled back
/// </summary>
public class HotRodClientRollbackException : HotRodClientException
{
internal HotRodClientRollbackException(string message) : base(message)
{
}
}
}
13 changes: 10 additions & 3 deletions swig/hotrod_exception.i
Expand Up @@ -9,6 +9,7 @@
HotRod_HotRodClientException,
HotRod_CounterLowerBoundException,
HotRod_CounterUpperBoundException,
HotRod_HotRodClientRollbackException,
HotRod_Exception,

} HotRodExceptionCodes;
Expand Down Expand Up @@ -66,6 +67,9 @@
SWIGPendingException.Set(new Infinispan.HotRod.Exceptions.CounterUpperBoundException(message));
break;
case 9:
SWIGPendingException.Set(new Infinispan.HotRod.Exceptions.HotRodClientRollbackException(message));
break;
case 10:
default:
SWIGPendingException.Set(new Infinispan.HotRod.Exceptions.Exception(message));
break;
Expand Down Expand Up @@ -100,15 +104,18 @@
} catch (const infinispan::hotrod::UnsupportedOperationException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_UnsupportedOperationException, e.what());
return $null;
} catch (const infinispan::hotrod::HotRodClientException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_HotRodClientException, e.what());
return $null;
} catch (const infinispan::hotrod::CounterLowerBoundException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_CounterLowerBoundException, e.what());
return $null;
} catch (const infinispan::hotrod::CounterUpperBoundException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_CounterUpperBoundException, e.what());
return $null;
} catch (const infinispan::hotrod::HotRodClientRollbackException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_HotRodClientRollbackException, e.what());
return $null;
} catch (const infinispan::hotrod::HotRodClientException& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_HotRodClientException, e.what());
return $null;
} catch (const infinispan::hotrod::Exception& e) {
SWIG_CSharpSetPendingExceptionCustom(HotRod_Exception, e.what());
return $null;
Expand Down
242 changes: 145 additions & 97 deletions test/Infinispan.HotRod.Tests/TransactionTest.cs
Expand Up @@ -19,9 +19,6 @@ public class TransactionTest
IMarshaller marshaller;
IMarshaller nonTxMarshaller;

HotRodServer server1;
HotRodServer server2;

private void InitializeRemoteCacheManager(bool started)
{
ConfigurationBuilder conf = new ConfigurationBuilder();
Expand All @@ -44,7 +41,6 @@ private void InitializeNonTxRemoteCacheManager(bool started)
nonTxRemoteManager = new RemoteCacheManager(conf.Build(), started);
}


[Test]
public void ReadCommitted()
{
Expand All @@ -54,15 +50,24 @@ public void ReadCommitted()

string k1 = "key13";
string v1 = "boron";
string rv1;

cache.Clear();

txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Commit();
try
{
txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Commit();
}
catch (Exception ex)
{
// try to release the tx resources
txManager.Rollback();
throw ex;
}
// Check the correct value from remote cache
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
Expand All @@ -77,15 +82,20 @@ public void ReadRollbackOnNotExistent()

string k1 = "key13";
string v1 = "boron";
string rv1;

cache.Clear();

txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Rollback();
try
{
txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
}
finally {
txManager.Rollback();
}
// Check the correct value from remote cache
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
Expand All @@ -103,15 +113,25 @@ public void ChangeExistentAndCommit()
string k1 = "key13";
string v0 = "carbon";
string v1 = "boron";
string rv1;

cache.Clear();
cache.Put(k1, v0);
txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Commit();
try
{
txManager.Begin();
cache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Commit();
}
catch (Exception ex)
{
// try to release the tx resources
txManager.Rollback();
throw ex;
}
// Check the correct value from remote cache
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
Expand All @@ -129,19 +149,22 @@ public void ReadRollbackOnExistent()
string k1 = "key13";
string oldv = "oxygen";
string v1 = "boron";
string rv1;

cache.Clear();

string oldrv1 = cache.Put(k1, oldv);
Assert.IsNull(oldrv1);

txManager.Begin();
oldrv1 = cache.Put(k1, v1);
Assert.AreEqual(oldrv1, oldv);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
txManager.Rollback();
try
{
txManager.Begin();
oldrv1 = cache.Put(k1, v1);
Assert.AreEqual(oldrv1, oldv);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
} finally {
txManager.Rollback();
}
// Check the correct value from remote cache
rv1 = cache.Get(k1);
Assert.AreEqual(oldrv1, oldv);
Expand All @@ -157,19 +180,23 @@ public void ReadInTransactionContext()

string k1 = "key13";
string v1 = "boron";
string rv1;

cache.Clear();

txManager.Begin();

string oldv1 = cache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
txManager.Rollback();
try
{
txManager.Begin();

string oldv1 = cache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
} finally {
txManager.Rollback();
}
}

// TX Client must read last value during the tx (from the context)
Expand All @@ -185,19 +212,28 @@ public void ReadCommittedAndNonTxRead()

string k1 = "key13";
string v1 = "boron";
string rv1;
string nontxrv1;

cache.Clear();

txManager.Begin();

cache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);

string nontxrv1 = nonTxCache.Get(k1);
Assert.IsNull(nontxrv1);
txManager.Commit();
try
{
txManager.Begin();

cache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
nontxrv1 = nonTxCache.Get(k1);
Assert.IsNull(nontxrv1);
txManager.Commit();
}
catch (Exception ex)
{
// try to release the tx resources
txManager.Rollback();
throw ex;
}
// Check the correct value from remote cache
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
Expand All @@ -217,19 +253,22 @@ public void PutAndReadWithNonTxCache()

string k1 = "key13";
string v1 = "boron";
string rv1;

cache.Clear();

txManager.Begin();
string oldv1 = nonTxCache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = nonTxCache.Get(k1);
Assert.AreEqual(rv1, v1);
rv1 = cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
txManager.Rollback();

try
{
txManager.Begin();
string oldv1 = nonTxCache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = nonTxCache.Get(k1);
Assert.AreEqual(rv1, v1);
rv1 = cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
} finally {
txManager.Rollback();
}
rv1 = nonTxCache.Get(k1);
Assert.AreEqual(rv1, v1);
}
Expand All @@ -247,28 +286,28 @@ public void RepeatableGetForTxClient()
string k1 = "key13";
string v1 = "boron";
string v2 = "helium";
string rv1;

cache.Clear();

txManager.Begin();

string oldv1 = nonTxCache.Put(k1, v1);
// Check the correct value from the tx context
string rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);

// This goes to the server
oldv1 = nonTxCache.Put(k1, v2);

// But this values comes from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);

cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
txManager.Rollback();

try
{
txManager.Begin();

string oldv1 = nonTxCache.Put(k1, v1);
// Check the correct value from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
// This goes to the server
oldv1 = nonTxCache.Put(k1, v2);
// But this values comes from the tx context
rv1 = cache.Get(k1);
Assert.AreEqual(rv1, v1);
cache.Remove(k1);
rv1 = cache.Get(k1);
Assert.IsNull(rv1);
} finally {
txManager.Rollback();
}
rv1 = nonTxCache.Get(k1);
Assert.AreEqual(rv1, v2);
}
Expand All @@ -286,22 +325,31 @@ public void ConflictsAndFail()
string v1 = "boron";
string k2 = "key14";
string v2 = "helium";

string vx = "calcium";

cache.Clear();

txManager.Begin();

string oldv1 = cache.Put(k1, v1);
string oldv2 = cache.Put(k2, v2);
// Check the correct value from the tx context
string rv1 = nonTxCache.Put(k1, vx);
Assert.IsNull(rv1);
txManager.Commit();
try
{
txManager.Begin();

string oldv1 = cache.Put(k1, v1);
string oldv2 = cache.Put(k2, v2);
// Check the correct value from the tx context
string rv1 = nonTxCache.Put(k1, vx);
Assert.IsNull(rv1);
Assert.Throws<Infinispan.HotRod.Exceptions.HotRodClientRollbackException>(() =>
{
txManager.Commit();
});
}
catch (Exception ex)
{
// try to release the tx resources
txManager.Rollback();
throw ex;
}
Assert.AreEqual(cache.Get(k1), vx);
Assert.IsNull(cache.Get(k2));
}

}
}

0 comments on commit 52abb1c

Please sign in to comment.