From 9a5f9a2e295efbdcb143d03ceda320ae484534af Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 24 Oct 2019 10:17:07 -0400 Subject: Handle DB error codes in distributed locking The commons library wraps the SQLExceptions within its own SQLException, so changed the code to simply look for a cause that's SQLTransientException, eliminating the need to check specific error codes. Deleted the error code property now that it is no longer needed. Also updated the distributed locking properties to include examples. Issue-ID: POLICY-2113 Signed-off-by: Jim Hahn Change-Id: If46e85a81cfc952e561174fea670df81efb8309a --- .../locking/DistributedLockManagerTest.java | 24 +++++++++++++++------- .../locking/DistributedLockPropertiesTest.java | 19 ----------------- 2 files changed, 17 insertions(+), 26 deletions(-) (limited to 'feature-distributed-locking/src/test/java/org/onap') diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java index 59b56224..0ba92c51 100644 --- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java +++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java @@ -48,6 +48,7 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.SQLTransientException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -100,8 +101,8 @@ public class DistributedLockManagerTest { private static final int HOLD_SEC2 = 120; private static final int MAX_THREADS = 5; private static final int MAX_LOOPS = 100; - private static final int TRANSIENT = 500; - private static final int PERMANENT = 600; + private static final boolean TRANSIENT = true; + private static final boolean PERMANENT = false; // number of execute() calls before the first lock attempt private static final int PRE_LOCK_EXECS = 1; @@ -1673,15 +1674,15 @@ public class DistributedLockManagerTest { * Feature whose data source all throws exceptions. */ private class InvalidDbLockingFeature extends MyLockingFeature { - private int errcode; + private boolean isTransient; private boolean freeLock = false; - public InvalidDbLockingFeature(int errcode) { + public InvalidDbLockingFeature(boolean isTransient) { // pass "false" because we have to set the error code BEFORE calling // afterStart() super(false); - this.errcode = errcode; + this.isTransient = isTransient; this.beforeCreateLockManager(engine, new Properties()); this.afterStart(engine); @@ -1695,13 +1696,22 @@ public class DistributedLockManagerTest { lock.free(); } - throw new SQLException(EXPECTED_EXCEPTION, "", errcode); + throw makeEx(); }); - doThrow(new SQLException(EXPECTED_EXCEPTION, "", errcode)).when(datasrc).close(); + doThrow(makeEx()).when(datasrc).close(); return datasrc; } + + private SQLException makeEx() { + if (isTransient) { + return new SQLException(new SQLTransientException(EXPECTED_EXCEPTION)); + + } else { + return new SQLException(EXPECTED_EXCEPTION); + } + } } /** diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockPropertiesTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockPropertiesTest.java index 5f76d657..5abca889 100644 --- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockPropertiesTest.java +++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockPropertiesTest.java @@ -20,13 +20,9 @@ package org.onap.policy.distributed.locking; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import java.util.Properties; -import java.util.TreeSet; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.properties.exception.PropertyException; @@ -46,7 +42,6 @@ public class DistributedLockPropertiesTest { props.setProperty(DistributedLockProperties.DB_URL, "my url"); props.setProperty(DistributedLockProperties.DB_USER, "my user"); props.setProperty(DistributedLockProperties.DB_PASS, "my pass"); - props.setProperty(DistributedLockProperties.TRANSIENT_ERROR_CODES, "10,-20,,,30"); props.setProperty(DistributedLockProperties.EXPIRE_CHECK_SEC, "100"); props.setProperty(DistributedLockProperties.RETRY_SEC, "200"); props.setProperty(DistributedLockProperties.MAX_RETRIES, "300"); @@ -60,22 +55,8 @@ public class DistributedLockPropertiesTest { assertEquals("my url", dlp.getDbUrl()); assertEquals("my user", dlp.getDbUser()); assertEquals("my pass", dlp.getDbPwd()); - assertEquals("10,-20,,,30", dlp.getErrorCodeStrings()); - assertEquals("[-20, 10, 30]", new TreeSet<>(dlp.getTransientErrorCodes()).toString()); assertEquals(100, dlp.getExpireCheckSec()); assertEquals(200, dlp.getRetrySec()); assertEquals(300, dlp.getMaxRetries()); - - assertTrue(dlp.isTransient(10)); - assertTrue(dlp.isTransient(-20)); - assertTrue(dlp.isTransient(30)); - - assertFalse(dlp.isTransient(-10)); - - // invalid value - props.setProperty(DistributedLockProperties.TRANSIENT_ERROR_CODES, "10,abc,30"); - - assertThatThrownBy(() -> new DistributedLockProperties(props)).isInstanceOf(PropertyException.class) - .hasMessageContaining(DistributedLockProperties.TRANSIENT_ERROR_CODES); } } -- cgit 1.2.3-korg