1 package org.opendaylight.controller.md.sal.dom.store.impl;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
7 import java.util.concurrent.ExecutionException;
9 import org.junit.Before;
10 import org.junit.Ignore;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
24 public class InMemoryDataStoreTest {
26 private SchemaContext schemaContext;
27 private InMemoryDOMDataStore domStore;
30 public void setupStore() {
31 domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
32 schemaContext = TestModel.createTestContext();
33 domStore.onGlobalContextUpdated(schemaContext);
38 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
40 assertNotNull(domStore);
42 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
43 assertNotNull(readTx);
45 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
46 assertNotNull(writeTx);
49 * Writes /test in writeTx
52 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
56 * Reads /test from writeTx Read should return container.
59 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
60 assertTrue(writeTxContainer.get().isPresent());
64 * Reads /test from readTx Read should return Absent.
67 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
68 assertFalse(readTxContainer.get().isPresent());
72 public void testTransactionCommit() throws InterruptedException, ExecutionException {
74 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
75 assertNotNull(writeTx);
78 * Writes /test in writeTx
81 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
85 * Reads /test from writeTx Read should return container.
88 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
89 assertTrue(writeTxContainer.get().isPresent());
91 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
93 assertThreePhaseCommit(cohort);
95 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
97 assertTrue(afterCommitRead.isPresent());
101 public void testTransactionAbort() throws InterruptedException, ExecutionException {
103 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
104 assertNotNull(writeTx);
106 assertTestContainerWrite(writeTx);
108 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
110 assertTrue(cohort.canCommit().get().booleanValue());
111 cohort.preCommit().get();
112 cohort.abort().get();
114 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
116 assertFalse(afterCommitRead.isPresent());
120 public void testTransactionChain() throws InterruptedException, ExecutionException {
121 DOMStoreTransactionChain txChain = domStore.createTransactionChain();
122 assertNotNull(txChain);
125 * We alocate new read-write transaction and write /test
129 DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
130 assertTestContainerWrite(firstTx);
133 * First transaction is marked as ready, we are able to allocate chained
136 DOMStoreThreePhaseCommitCohort firstWriteTxCohort = firstTx.ready();
139 * We alocate chained transaction - read transaction, note first one is
140 * still not commited to datastore.
142 DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
146 * We test if we are able to read data from tx, read should not fail
147 * since we are using chained transaction.
151 assertTestContainerExists(secondReadTx);
155 * We alocate next transaction, which is still based on first one, but
159 DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
162 * We test existence of /test in third transaction container should
163 * still be visible from first one (which is still uncommmited).
167 assertTestContainerExists(thirdDeleteTx);
170 * We delete node in third transaction
172 thirdDeleteTx.delete(TestModel.TEST_PATH);
175 * third transaction is sealed.
177 DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
180 * We commit first transaction
183 assertThreePhaseCommit(firstWriteTxCohort);
185 // Alocates store transacion
186 DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
188 * We verify transaction is commited to store, container should exists
191 assertTestContainerExists(storeReadTx);
193 * We commit third transaction
196 assertThreePhaseCommit(thirdDeleteTxCohort);
201 public void testTransactionConflict() throws InterruptedException, ExecutionException {
202 DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
203 DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
204 assertTestContainerWrite(txOne);
205 assertTestContainerWrite(txTwo);
208 * Commits transaction
210 assertThreePhaseCommit(txOne.ready());
213 * Asserts that txTwo could not be commited
215 assertFalse(txTwo.ready().canCommit().get());
218 private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort)
219 throws InterruptedException, ExecutionException {
220 assertTrue(cohort.canCommit().get().booleanValue());
221 cohort.preCommit().get();
222 cohort.commit().get();
225 private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
226 throws InterruptedException, ExecutionException {
229 * Writes /test in writeTx
232 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
234 return assertTestContainerExists(writeTx);
239 * Reads /test from readTx Read should return container.
242 private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(DOMStoreReadTransaction readTx)
243 throws InterruptedException, ExecutionException {
245 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
246 assertTrue(writeTxContainer.get().isPresent());
247 return writeTxContainer.get();