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.DOMDataWriteTransaction;
32 import com.google.common.base.Optional;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.util.concurrent.ForwardingExecutorService;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
37 import com.google.common.util.concurrent.ListenableFuture;
38 import com.google.common.util.concurrent.ListeningExecutorService;
39 import com.google.common.util.concurrent.MoreExecutors;
40 import java.util.Collections;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.ExecutionException;
43 import java.util.concurrent.ExecutorService;
44 import java.util.concurrent.Executors;
45 import java.util.concurrent.RejectedExecutionException;
46 import java.util.concurrent.TimeUnit;
47 import java.util.concurrent.atomic.AtomicReference;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.Mockito;
52 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
53 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
59 public class DOMBrokerTest {
61 private SchemaContext schemaContext;
62 private AbstractDOMDataBroker domBroker;
63 private ListeningExecutorService executor;
64 private ExecutorService futureExecutor;
65 private CommitExecutorService commitExecutor;
68 public void setupStore() {
70 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
71 MoreExecutors.newDirectExecutorService());
72 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
73 MoreExecutors.newDirectExecutorService());
74 schemaContext = TestModel.createTestContext();
76 operStore.onGlobalContextUpdated(schemaContext);
77 configStore.onGlobalContextUpdated(schemaContext);
79 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
80 .put(CONFIGURATION, configStore) //
81 .put(OPERATIONAL, operStore) //
84 commitExecutor = new CommitExecutorService(Executors.newSingleThreadExecutor());
85 futureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(1, 5, "FCB");
86 executor = new DeadlockDetectingListeningExecutorService(commitExecutor,
87 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER, futureExecutor);
88 domBroker = new SerializedDOMDataBroker(stores, executor);
92 public void tearDown() {
93 if( executor != null ) {
94 executor.shutdownNow();
97 if(futureExecutor != null) {
98 futureExecutor.shutdownNow();
103 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
105 assertNotNull(domBroker);
107 DOMDataReadTransaction readTx = domBroker.newReadOnlyTransaction();
108 assertNotNull(readTx);
110 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
111 assertNotNull(writeTx);
114 * Writes /test in writeTx
117 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
122 * Reads /test from readTx Read should return Absent.
125 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx
126 .read(OPERATIONAL, TestModel.TEST_PATH);
127 assertFalse(readTxContainer.get().isPresent());
131 public void testTransactionCommit() throws InterruptedException, ExecutionException {
133 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
134 assertNotNull(writeTx);
137 * Writes /test in writeTx
140 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
142 writeTx.submit().get();
144 Optional<NormalizedNode<?, ?>> afterCommitRead = domBroker.newReadOnlyTransaction()
145 .read(OPERATIONAL, TestModel.TEST_PATH).get();
146 assertTrue(afterCommitRead.isPresent());
149 @Test(expected=TransactionCommitFailedException.class)
150 public void testRejectedCommit() throws Exception {
152 commitExecutor.delegate = Mockito.mock( ExecutorService.class );
153 Mockito.doThrow( new RejectedExecutionException( "mock" ) )
154 .when( commitExecutor.delegate ).execute( Mockito.any( Runnable.class ) );
155 Mockito.doNothing().when( commitExecutor.delegate ).shutdown();
156 Mockito.doReturn( Collections.emptyList() ).when( commitExecutor.delegate ).shutdownNow();
157 Mockito.doReturn( "" ).when( commitExecutor.delegate ).toString();
158 Mockito.doReturn( true ).when( commitExecutor.delegate )
159 .awaitTermination( Mockito.anyLong(), Mockito.any( TimeUnit.class ) );
161 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
162 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME) );
164 writeTx.submit().checkedGet( 5, TimeUnit.SECONDS );
168 * Tests a simple DataChangeListener notification after a write.
171 public void testDataChangeListener() throws Throwable {
173 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode( TestModel.TEST_QNAME );
175 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener();
177 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
178 dcListener, DataChangeScope.BASE );
180 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
181 assertNotNull( writeTx );
183 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, testNode );
185 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
187 dcListener.waitForChange();
189 if( caughtEx.get() != null ) {
190 throw caughtEx.get();
193 NormalizedNode<?, ?> actualNode = dcListener.change.getCreatedData().get( TestModel.TEST_PATH );
194 assertEquals( "Created node", testNode, actualNode );
198 * Tests a DataChangeListener that does an async submit of a write Tx in its onDataChanged method.
199 * This should succeed without deadlock.
202 public void testDataChangeListenerDoingAsyncWriteTxSubmit() throws Throwable {
204 final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
205 final CountDownLatch commitCompletedLatch = new CountDownLatch( 1 );
207 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
209 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
211 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
212 writeTx.put( OPERATIONAL, TestModel.TEST2_PATH,
213 ImmutableNodes.containerNode( TestModel.TEST2_QNAME ) );
214 Futures.addCallback( writeTx.submit(), new FutureCallback<Void>() {
216 public void onSuccess( final Void result ) {
217 commitCompletedLatch.countDown();
221 public void onFailure( final Throwable t ) {
222 caughtCommitEx.set( t );
223 commitCompletedLatch.countDown();
227 super.onDataChanged( change );
231 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
232 dcListener, DataChangeScope.BASE );
234 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
235 assertNotNull( writeTx );
237 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode( TestModel.TEST_QNAME ) );
239 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
241 dcListener.waitForChange();
243 if( caughtEx.get() != null ) {
244 throw caughtEx.get();
247 assertTrue( "Commit Future was not invoked", commitCompletedLatch.await( 5, TimeUnit.SECONDS ) );
249 if( caughtCommitEx.get() != null ) {
250 throw caughtCommitEx.get();
255 * Tests a DataChangeListener that does a blocking submit of a write Tx in its onDataChanged method.
256 * This should throw an exception and not deadlock.
258 @Test(expected=TransactionCommitDeadlockException.class)
259 public void testDataChangeListenerDoingBlockingWriteTxSubmit() throws Throwable {
261 final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
263 TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
265 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
266 DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
267 writeTx.put( OPERATIONAL, TestModel.TEST2_PATH,
268 ImmutableNodes.containerNode( TestModel.TEST2_QNAME ) );
270 writeTx.submit().get();
271 } catch( ExecutionException e ) {
272 caughtCommitEx.set( e.getCause() );
273 } catch( Exception e ) {
274 caughtCommitEx.set( e );
277 super.onDataChanged( change );
282 domBroker.registerDataChangeListener( OPERATIONAL, TestModel.TEST_PATH,
283 dcListener, DataChangeScope.BASE );
285 final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
286 assertNotNull( writeTx );
288 writeTx.put( OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode( TestModel.TEST_QNAME ) );
290 AtomicReference<Throwable> caughtEx = submitTxAsync( writeTx );
292 dcListener.waitForChange();
294 if( caughtEx.get() != null ) {
295 throw caughtEx.get();
298 if( caughtCommitEx.get() != null ) {
299 throw caughtCommitEx.get();
303 AtomicReference<Throwable> submitTxAsync( final DOMDataWriteTransaction writeTx ) {
304 final AtomicReference<Throwable> caughtEx = new AtomicReference<>();
311 } catch( Throwable e ) {
321 static class TestDOMDataChangeListener implements DOMDataChangeListener {
323 volatile AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change;
324 private final CountDownLatch latch = new CountDownLatch( 1 );
327 public void onDataChanged( final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change ) {
328 this.change = change;
332 void waitForChange() throws InterruptedException {
333 assertTrue( "onDataChanged was not called", latch.await( 5, TimeUnit.SECONDS ) );
337 static class CommitExecutorService extends ForwardingExecutorService {
339 ExecutorService delegate;
341 public CommitExecutorService( final ExecutorService delegate ) {
342 this.delegate = delegate;
346 protected ExecutorService delegate() {