2 * Copyright (c) 2014 Cisco Systems, 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.controller.md.sal.dom.store.impl;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.util.concurrent.ExecutionException;
16 import org.junit.Before;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import com.google.common.util.concurrent.MoreExecutors;
32 public class InMemoryDataStoreTest {
34 private SchemaContext schemaContext;
35 private InMemoryDOMDataStore domStore;
38 public void setupStore() {
39 domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
40 schemaContext = TestModel.createTestContext();
41 domStore.onGlobalContextUpdated(schemaContext);
46 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
48 assertNotNull(domStore);
50 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
51 assertNotNull(readTx);
53 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
54 assertNotNull(writeTx);
57 * Writes /test in writeTx
60 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
64 * Reads /test from writeTx Read should return container.
67 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
68 assertTrue(writeTxContainer.get().isPresent());
72 * Reads /test from readTx Read should return Absent.
75 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
76 assertFalse(readTxContainer.get().isPresent());
80 public void testTransactionCommit() throws InterruptedException, ExecutionException {
82 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
83 assertNotNull(writeTx);
86 * Writes /test in writeTx
89 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
93 * Reads /test from writeTx Read should return container.
96 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
97 assertTrue(writeTxContainer.get().isPresent());
99 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
101 assertThreePhaseCommit(cohort);
103 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
105 assertTrue(afterCommitRead.isPresent());
109 public void testTransactionAbort() throws InterruptedException, ExecutionException {
111 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
112 assertNotNull(writeTx);
114 assertTestContainerWrite(writeTx);
116 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
118 assertTrue(cohort.canCommit().get().booleanValue());
119 cohort.preCommit().get();
120 cohort.abort().get();
122 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
124 assertFalse(afterCommitRead.isPresent());
128 public void testTransactionChain() throws InterruptedException, ExecutionException {
129 DOMStoreTransactionChain txChain = domStore.createTransactionChain();
130 assertNotNull(txChain);
133 * We alocate new read-write transaction and write /test
137 DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
138 assertTestContainerWrite(firstTx);
141 * First transaction is marked as ready, we are able to allocate chained
144 DOMStoreThreePhaseCommitCohort firstWriteTxCohort = firstTx.ready();
147 * We alocate chained transaction - read transaction, note first one is
148 * still not commited to datastore.
150 DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
154 * We test if we are able to read data from tx, read should not fail
155 * since we are using chained transaction.
159 assertTestContainerExists(secondReadTx);
163 * We alocate next transaction, which is still based on first one, but
167 DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
170 * We test existence of /test in third transaction container should
171 * still be visible from first one (which is still uncommmited).
175 assertTestContainerExists(thirdDeleteTx);
178 * We delete node in third transaction
180 thirdDeleteTx.delete(TestModel.TEST_PATH);
183 * third transaction is sealed.
185 DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
188 * We commit first transaction
191 assertThreePhaseCommit(firstWriteTxCohort);
193 // Alocates store transacion
194 DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
196 * We verify transaction is commited to store, container should exists
199 assertTestContainerExists(storeReadTx);
201 * We commit third transaction
204 assertThreePhaseCommit(thirdDeleteTxCohort);
209 public void testTransactionConflict() throws InterruptedException, ExecutionException {
210 DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
211 DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
212 assertTestContainerWrite(txOne);
213 assertTestContainerWrite(txTwo);
216 * Commits transaction
218 assertThreePhaseCommit(txOne.ready());
221 * Asserts that txTwo could not be commited
223 assertFalse(txTwo.ready().canCommit().get());
226 private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort)
227 throws InterruptedException, ExecutionException {
228 assertTrue(cohort.canCommit().get().booleanValue());
229 cohort.preCommit().get();
230 cohort.commit().get();
233 private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
234 throws InterruptedException, ExecutionException {
237 * Writes /test in writeTx
240 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
242 return assertTestContainerExists(writeTx);
246 * Reads /test from readTx Read should return container.
248 private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(final DOMStoreReadTransaction readTx)
249 throws InterruptedException, ExecutionException {
251 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
252 assertTrue(writeTxContainer.get().isPresent());
253 return writeTxContainer.get();