2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.genius.infra.tests;
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
13 import org.junit.Test;
14 import org.opendaylight.genius.infra.Datastore;
15 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
16 import org.opendaylight.genius.infra.RetryingManagedNewTransactionRunner;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
20 import org.opendaylight.mdsal.common.api.ReadFailedException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
24 * Test for {@link RetryingManagedNewTransactionRunner}.
25 * Note that this test (intentionally) extends the {@link ManagedNewTransactionRunnerImplTest}.
27 * @author Michael Vorburger.ch
29 public class RetryingManagedNewTransactionRunnerTest extends ManagedNewTransactionRunnerImplTest {
32 protected ManagedNewTransactionRunner createManagedNewTransactionRunnerToTest(DataBroker dataBroker) {
33 return new RetryingManagedNewTransactionRunner(dataBroker);
37 public void testCallWithNewWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
38 // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
39 // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
40 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
41 TopLevelList data = newTestDataObject();
42 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(
43 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
44 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
48 public void testCallWithNewTypedWriteOnlyTransactionOptimisticLockFailedException() throws Exception {
49 // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
50 // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
51 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
52 TopLevelList data = newTestDataObject();
53 managedNewTransactionRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
54 writeTx -> writeTx.put(TEST_PATH, data)).get();
55 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
59 public void testCallWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
60 // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
61 // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
62 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
63 TopLevelList data = newTestDataObject();
64 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
65 writeTx -> writeTx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data)).get();
66 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
70 public void testCallWithNewTypedReadWriteTransactionOptimisticLockFailedException() throws Exception {
71 // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
72 // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
73 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
74 TopLevelList data = newTestDataObject();
75 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
76 writeTx -> writeTx.put(TEST_PATH, data)).get();
77 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
81 public void testApplyWithNewReadWriteTransactionOptimisticLockFailedException() throws Exception {
82 // contrary to the super() test implementation for (just) ManagedNewTransactionRunnerImpl, in the parent class
83 // here we expect the x2 OptimisticLockFailedException to be retried, and then eventually succeed:
84 testableDataBroker.failSubmits(2, new OptimisticLockFailedException("bada boum bam!"));
85 TopLevelList data = newTestDataObject();
86 assertEquals(1, (long) managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
87 Datastore.OPERATIONAL,
89 writeTx.put(TEST_PATH, data);
92 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
96 public void testCallWithNewReadWriteTransactionReadFailedException() throws Exception {
97 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
98 TopLevelList data = newTestDataObject();
99 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(
101 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_PATH, data);
102 assertEquals(data, tx.read(LogicalDatastoreType.OPERATIONAL, TEST_PATH).get().get());
104 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
108 public void testCallWithNewTypedReadWriteTransactionReadFailedException() throws Exception {
109 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
110 TopLevelList data = newTestDataObject();
111 managedNewTransactionRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.OPERATIONAL,
113 tx.put(TEST_PATH, data);
114 assertEquals(data, tx.read(TEST_PATH).get().get());
116 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));
120 public void testApplyWithNewReadWriteTransactionReadFailedException() throws Exception {
121 testableDataBroker.failReads(2, new ReadFailedException("bada boum bam!"));
122 TopLevelList data = newTestDataObject();
123 assertEquals(data, managedNewTransactionRunner.applyWithNewReadWriteTransactionAndSubmit(
124 Datastore.OPERATIONAL,
126 tx.put(TEST_PATH, data);
127 return tx.read(TEST_PATH).get().get();
129 assertEquals(data, singleTransactionDataBroker.syncRead(OPERATIONAL, TEST_PATH));