- writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
- }
-
- /**
- * Tests a simple DataChangeListener notification after a write.
- */
- @Test
- @SuppressWarnings("checkstyle:IllegalThrows")
- public void testDataChangeListener() throws Throwable {
-
- final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
-
- TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener();
-
- domBroker.registerDataChangeListener(OPERATIONAL, TestModel.TEST_PATH, dcListener, DataChangeScope.BASE);
-
- final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
- assertNotNull(writeTx);
-
- writeTx.put(OPERATIONAL, TestModel.TEST_PATH, testNode);
-
- AtomicReference<Throwable> caughtEx = submitTxAsync(writeTx);
-
- dcListener.waitForChange();
-
- if (caughtEx.get() != null) {
- throw caughtEx.get();
- }
-
- NormalizedNode<?, ?> actualNode = dcListener.capturedChange.getCreatedData().get(TestModel.TEST_PATH);
- assertEquals("Created node", testNode, actualNode);
- }
-
- /**
- * Tests a DataChangeListener that does an async submit of a write Tx in its onDataChanged method.
- * This should succeed without deadlock.
- */
- @Test
- @SuppressWarnings("checkstyle:IllegalThrows")
- public void testDataChangeListenerDoingAsyncWriteTxSubmit() throws Throwable {
-
- final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
- final CountDownLatch commitCompletedLatch = new CountDownLatch(1);
-
- TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
- @Override
- public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
-
- DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
- writeTx.put(OPERATIONAL, TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME));
- Futures.addCallback(writeTx.submit(), new FutureCallback<Void>() {
- @Override
- public void onSuccess(final Void result) {
- commitCompletedLatch.countDown();
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- caughtCommitEx.set(throwable);
- commitCompletedLatch.countDown();
- }
- });
-
- super.onDataChanged(change);
- }
- };
-
- domBroker.registerDataChangeListener(OPERATIONAL, TestModel.TEST_PATH, dcListener, DataChangeScope.BASE);
-
- final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
- assertNotNull(writeTx);
-
- writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-
- AtomicReference<Throwable> caughtEx = submitTxAsync(writeTx);
-
- dcListener.waitForChange();
-
- if (caughtEx.get() != null) {
- throw caughtEx.get();
- }
-
- assertTrue("Commit Future was not invoked", commitCompletedLatch.await(5, TimeUnit.SECONDS));
-
- if (caughtCommitEx.get() != null) {
- throw caughtCommitEx.get();
- }
- }
-
- /**
- * Tests a DataChangeListener that does a blocking submit of a write Tx in its onDataChanged method.
- * This should throw an exception and not deadlock.
- */
- @Test(expected = TransactionCommitDeadlockException.class)
- @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
- public void testDataChangeListenerDoingBlockingWriteTxSubmit() throws Throwable {
-
- final AtomicReference<Throwable> caughtCommitEx = new AtomicReference<>();
-
- TestDOMDataChangeListener dcListener = new TestDOMDataChangeListener() {
- @Override
- public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
- DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
- writeTx.put(OPERATIONAL, TestModel.TEST2_PATH, ImmutableNodes.containerNode(TestModel.TEST2_QNAME));
- try {
- writeTx.submit().get();
- } catch (ExecutionException e) {
- caughtCommitEx.set(e.getCause());
- } catch (Exception e) {
- caughtCommitEx.set(e);
- } finally {
- super.onDataChanged(change);
- }
- }
- };
-
- domBroker.registerDataChangeListener(OPERATIONAL, TestModel.TEST_PATH, dcListener, DataChangeScope.BASE);
-
- final DOMDataWriteTransaction writeTx = domBroker.newWriteOnlyTransaction();
- assertNotNull(writeTx);
-
- writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-
- AtomicReference<Throwable> caughtEx = submitTxAsync(writeTx);
-
- dcListener.waitForChange();
-
- if (caughtEx.get() != null) {
- throw caughtEx.get();
- }
-
- if (caughtCommitEx.get() != null) {
- throw caughtCommitEx.get();