1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.request.listening;
14 import java.util.concurrent.Semaphore;
16 import org.junit.Test;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.Session;
19 import org.simantics.db.common.procedure.adapter.AsyncListenerAdapter;
20 import org.simantics.db.procedure.AsyncProcedure;
21 import org.simantics.db.request.AsyncRead;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23 import org.simantics.utils.DataContainer;
25 public class DisposedListenerTest extends ExistingDatabaseTest {
28 * Performs an asynchronous dummy graph request with a listener that is
29 * already disposed when the request is first performed.
32 * The expected outcome is that the request will complete normally, calling
33 * execute of the listener procedure once and only once.
36 * The outcome when this test was created was that QueryProcessor would
37 * crash internally with a NullPointerException.
39 * This problem is fixed by: https://www.simulationsite.net/trac/simantics/changeset/13869
44 public void testDisposedListener() throws Exception{
45 final Session session = getSession();
47 final Semaphore sem = new Semaphore(0);
48 final DataContainer<Throwable> exc = new DataContainer<Throwable>();
50 session.asyncRequest(new AsyncRead<Object>() {
52 public int getFlags() {
56 public int threadHash() {
60 public void perform(AsyncReadGraph graph, AsyncProcedure<Object> procedure) {
61 procedure.execute(graph, new Object());
63 }, new AsyncListenerAdapter<Object>() {
65 public boolean isDisposed() {
69 public void exception(AsyncReadGraph graph, Throwable t) {
70 System.out.println(getClass().getSimpleName() + ": exception:");
76 public void execute(AsyncReadGraph graph, Object result) {
78 System.out.println(getClass().getSimpleName() + ": execute: " + result);
85 Throwable t = exc.get();
87 if (t instanceof Exception)
90 throw new Exception(t);