summaryrefslogtreecommitdiffstats
path: root/models-provider/src/test/java/org
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-05-02 01:35:07 +0100
committerliamfallon <liam.fallon@est.tech>2020-05-04 21:20:38 +0100
commit275a3c5707c721d6fb020c9632f7a545cbc33b81 (patch)
treeb4d45d330c51356fc0eb5c494b14e7645a7bc7d7 /models-provider/src/test/java/org
parentc86d6a6645af95d76a8b2b59f24433c616bf49fe (diff)
Fix simultaneous write access to policy provider
This change serializes write access to the policy database via the AUthorative TOSCA provider by making sure that only one request is executed at a time. His change should be repolaced by proper session handling in the next release of the Policy Framework project. Issue-ID: POLICY-2533 Change-Id: I5fe4c0f2846981a66eb2f4e1da936fe3c9490ae5 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-provider/src/test/java/org')
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java18
-rw-r--r--models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java23
2 files changed, 35 insertions, 6 deletions
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
index b7145d00e..cbc57fe75 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java
@@ -20,6 +20,7 @@
package org.onap.policy.models.provider.impl;
+import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -29,6 +30,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import lombok.NonNull;
@@ -238,8 +241,19 @@ public class PolicyToscaPersistenceTest {
public void testPolicyPersistence(@NonNull final ToscaServiceTemplate serviceTemplate) throws Exception {
assertNotNull(serviceTemplate);
- databaseProvider.createPolicies(serviceTemplate);
- databaseProvider.updatePolicies(serviceTemplate);
+ CountDownLatch threadCountDownLatch = new CountDownLatch(10);
+
+ for (int i = 0; i < 10; i++) {
+ new Thread() {
+ public void run() {
+ assertThatCode(() -> databaseProvider.createPolicies(serviceTemplate)).doesNotThrowAnyException();
+ assertThatCode(() -> databaseProvider.updatePolicies(serviceTemplate)).doesNotThrowAnyException();
+ threadCountDownLatch.countDown();
+ }
+ }.start();
+ }
+
+ threadCountDownLatch.await(10, TimeUnit.SECONDS);
for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
for (ToscaPolicy policy : policyMap.values()) {
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
index 22722186c..6cda57b60 100644
--- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
+++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java
@@ -21,11 +21,14 @@
package org.onap.policy.models.provider.impl;
import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Base64;
import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
@@ -82,7 +85,7 @@ public class PolicyTypePersistenceTest {
String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
ToscaServiceTemplate foundPolicyTypeSt =
- yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
+ yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
serviceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
serviceTemplate.setDescription(foundPolicyTypeSt.getDescription());
@@ -107,13 +110,25 @@ public class PolicyTypePersistenceTest {
}
}
- assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate)).doesNotThrowAnyException();
+ CountDownLatch threadCountDownLatch = new CountDownLatch(10);
+
+ for (int i = 0; i < 10; i++) {
+ new Thread() {
+ public void run() {
+ assertThatCode(() -> databaseProvider.createPolicyTypes(serviceTemplate))
+ .doesNotThrowAnyException();
+ threadCountDownLatch.countDown();
+ }
+ }.start();
+ }
+
+ threadCountDownLatch.await(9, TimeUnit.SECONDS);
ToscaEntityKey resourceOptimizationPtKey =
- new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0");
+ new ToscaEntityKey("onap.policies.optimization.resource.OptimizationPolicy", "1.0.0");
ToscaServiceTemplate resOptPolicyTypeSt = databaseProvider.getPolicyTypes(resourceOptimizationPtKey.getName(),
- resourceOptimizationPtKey.getVersion());
+ resourceOptimizationPtKey.getVersion());
assertEquals(3, resOptPolicyTypeSt.getPolicyTypesAsMap().size());
assertTrue(resOptPolicyTypeSt.getPolicyTypesAsMap().containsKey(resourceOptimizationPtKey));