2 * Copyright (c) 2014, 2015 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
9 package org.opendaylight.mdsal.dom.broker.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
16 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
18 import org.opendaylight.mdsal.dom.broker.test.util.TestModel;
20 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
22 import org.opendaylight.mdsal.common.api.AsyncDataChangeEvent;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.common.api.TransactionCommitDeadlockException;
25 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
26 import org.opendaylight.mdsal.common.api.AsyncDataBroker.DataChangeScope;
27 import org.opendaylight.mdsal.dom.broker.AbstractDOMDataBroker;
28 import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMDataChangeListener;
30 import org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMDataReadWriteTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMDataWriteTransaction;
33 import com.google.common.base.Optional;
34 import com.google.common.collect.ImmutableMap;
35 import com.google.common.util.concurrent.ForwardingExecutorService;
36 import com.google.common.util.concurrent.FutureCallback;
37 import com.google.common.util.concurrent.Futures;
38 import com.google.common.util.concurrent.ListenableFuture;
39 import com.google.common.util.concurrent.ListeningExecutorService;
40 import com.google.common.util.concurrent.MoreExecutors;
41 import java.util.Collections;
42 import java.util.concurrent.CountDownLatch;
43 import java.util.concurrent.ExecutionException;
44 import java.util.concurrent.ExecutorService;
45 import java.util.concurrent.Executors;
46 import java.util.concurrent.RejectedExecutionException;
47 import java.util.concurrent.TimeUnit;
48 import java.util.concurrent.atomic.AtomicReference;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.mockito.Mockito;
53 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
54 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
60 public class DOMBrokerTest {
62 private SchemaContext schemaContext;
63 private AbstractDOMDataBroker domBroker;
64 private ListeningExecutorService executor;
65 private ExecutorService futureExecutor;
66 private CommitExecutorService commitExecutor;
69 public void setupStore() {
71 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
72 MoreExecutors.newDirectExecutorService());
73 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
74 MoreExecutors.newDirectExecutorService());
75 schemaContext = TestModel.createTestContext();
77 operStore.onGlobalContextUpdated(schemaContext);
78 configStore.onGlobalContextUpdated(schemaContext);
80 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
81 .put(CONFIGURATION, configStore) //
82 .put(OPERATIONAL, operStore) //
85 commitExecutor = new CommitExecutorService(Executors.newSingleThreadExecutor());
86 futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB");
87 executor = new DeadlockDetectingListeningExecutorService(commitExecutor,
88 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER, futureExecutor);
89 domBroker = new SerializedDOMDataBroker(stores, executor);
93 public void tearDown() {
94 if( executor != null ) {
95 executor.shutdownNow();
98 if(futureExecutor != null) {
99 futureExecutor.shutdownNow();
104 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
106 assertNotNull(domBroker);
108 DOMDataReadTransaction readTx = domBroker.newReadOnlyTransaction();
109 assertNotNull(readTx);
111 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
112 assertNotNull(writeTx);
115 * Writes /test in writeTx
118 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
122 * Reads /test from writeTx Read should return container.
125 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
126 TestModel.TEST_PATH);
127 assertTrue(writeTxContainer.get().isPresent());
131 * Reads /test from readTx Read should return Absent.
134 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx
135 .read(OPERATIONAL, TestModel.TEST_PATH);
136 assertFalse(readTxContainer.get().isPresent());
140 public void testTransactionCommit() throws InterruptedException, ExecutionException {
142 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
143 assertNotNull(writeTx);
146 * Writes /test in writeTx
149 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
153 * Reads /test from writeTx Read should return container.
156 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
157 TestModel.TEST_PATH);
158 assertTrue(writeTxContainer.get().isPresent());
160 writeTx.submit().get();
162 Optional<NormalizedNode<?, ?>> afterCommitRead = domBroker.newReadOnlyTransaction()
163 .read(OPERATIONAL, TestModel.TEST_PATH).get();
164 assertTrue(afterCommitRead.isPresent());
167 @Test(expected=TransactionCommitFailedException.class)
168 public void testRejectedCommit() throws Exception {
170 commitExecutor.delegate = Mockito.mock( ExecutorService.class );
171 Mockito.doThrow( new RejectedExecutionException( "mock" ) )
172 .when( commitExecutor.delegate ).execute( Mockito.any( Runnable.class ) );
173 Mockito.doNothing().when( commitExecutor.delegate ).shutdown();
174 Mockito.doReturn( Collections.emptyList() ).when( commitExecutor.delegate ).shutdownNow();
175 Mockito.doReturn( "" ).when( commitExecutor.delegate ).toString();
176 Mockito.doReturn( true ).when( commitExecutor.delegate )
177 .awaitTermination( Mockito.anyLong(), Mockito.any( TimeUnit.class ) );
179 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
180 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME) );
182 writeTx.submit().checkedGet( 5, TimeUnit.SECONDS );
186 * Tests a simple DataChangeListener notification after a write.
189 public void testDataChangeListener() throws Throwable {
191 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode( TestModel.TEST_QNAME );
193 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener();
195 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
196 dcListener, DataChangeScope.BASE );
198 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
199 assertNotNull( writeTx );
201 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, testNode );
203 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
205 dcListener.waitForChange();
207 if( caughtEx.get() != null ) {
208 throw caughtEx.get();
211 NormalizedNode<?, ?> actualNode = dcListener.change.getCreatedData().get( TestModel.TEST_PATH );
212 assertEquals( "Created node", testNode, actualNode );
216 * Tests a DataChangeListener that does an async submit of a write Tx in its onDataChanged method.
217 * This should succeed without deadlock.
220 public void testDataChangeListenerDoingAsyncWriteTxSubmit() throws Throwable {
222 final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
223 final CountDownLatch commitCompletedLatch = new CountDownLatch( 1 );
225 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
227 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
229 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
230 writeTx.put( OPERATIONAL, TestModel.TEST2_PATH,
231 ImmutableNodes.containerNode( TestModel.TEST2_QNAME ) );
232 Futures.addCallback( writeTx.submit(), new FutureCallback<Void>() {
234 public void onSuccess( final Void result ) {
235 commitCompletedLatch.countDown();
239 public void onFailure( final Throwable t ) {
240 caughtCommitEx.set( t );
241 commitCompletedLatch.countDown();
245 super.onDataChanged( change );
249 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
250 dcListener, DataChangeScope.BASE );
252 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
253 assertNotNull( writeTx );
255 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode( TestModel.TEST_QNAME ) );
257 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
259 dcListener.waitForChange();
261 if( caughtEx.get() != null ) {
262 throw caughtEx.get();
265 assertTrue( "Commit Future was not invoked", commitCompletedLatch.await( 5, TimeUnit.SECONDS ) );
267 if( caughtCommitEx.get() != null ) {
268 throw caughtCommitEx.get();
273 * Tests a DataChangeListener that does a blocking submit of a write Tx in its onDataChanged method.
274 * This should throw an exception and not deadlock.
276 @Test(expected=TransactionCommitDeadlockException.class)
277 public void testDataChangeListenerDoingBlockingWriteTxSubmit() throws Throwable {
279 final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
281 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
283 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
284 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
285 writeTx.put( OPERATIONAL, TestModel.TEST2_PATH,
286 ImmutableNodes.containerNode( TestModel.TEST2_QNAME ) );
288 writeTx.submit().get();
289 } catch( ExecutionException e ) {
290 caughtCommitEx.set( e.getCause() );
291 } catch( Exception e ) {
292 caughtCommitEx.set( e );
295 super.onDataChanged( change );
300 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
301 dcListener, DataChangeScope.BASE );
303 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
304 assertNotNull( writeTx );
306 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode( TestModel.TEST_QNAME ) );
308 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
310 dcListener.waitForChange();
312 if( caughtEx.get() != null ) {
313 throw caughtEx.get();
316 if( caughtCommitEx.get() != null ) {
317 throw caughtCommitEx.get();
321 AtomicReference<Throwable> submitTxAsync( final DOMDataWriteTransaction writeTx ) {
322 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
329 } catch( Throwable e ) {
339 static class TestDOMDataChangeListener implements DOMDataChangeListener {
341 volatile AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change;
342 private final CountDownLatch latch = new CountDownLatch( 1 );
345 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
346 this.change = change;
350 void waitForChange() throws InterruptedException {
351 assertTrue( "onDataChanged was not called", latch.await( 5, TimeUnit.SECONDS ) );
355 static class CommitExecutorService extends ForwardingExecutorService {
357 ExecutorService delegate;
359 public CommitExecutorService( final ExecutorService delegate ) {
360 this.delegate = delegate;
364 protected ExecutorService delegate() {