]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/listening/DisposedListenerTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / listening / DisposedListenerTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.request.listening;
13
14 import java.util.concurrent.Semaphore;
15
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;
24
25 public class DisposedListenerTest extends ExistingDatabaseTest {
26
27     /**
28      * Performs an asynchronous dummy graph request with a listener that is
29      * already disposed when the request is first performed.
30      * 
31      * <p>
32      * The expected outcome is that the request will complete normally, calling
33      * execute of the listener procedure once and only once.
34      * 
35      * <p>
36      * The outcome when this test was created was that QueryProcessor would
37      * crash internally with a NullPointerException.
38      * 
39      * This problem is fixed by: https://www.simulationsite.net/trac/simantics/changeset/13869
40      * 
41      * @throws Exception
42      */
43         @Test
44     public void testDisposedListener() throws Exception{
45         final Session session = getSession();
46
47         final Semaphore sem = new Semaphore(0);
48         final DataContainer<Throwable> exc = new DataContainer<Throwable>();
49
50         session.asyncRequest(new AsyncRead<Object>() {
51             @Override
52             public int getFlags() {
53                 return 0;
54             }
55                     @Override
56                     public int threadHash() {
57                         return hashCode();
58                     }
59             @Override
60             public void perform(AsyncReadGraph graph, AsyncProcedure<Object> procedure) {
61                 procedure.execute(graph, new Object());
62             }
63         }, new AsyncListenerAdapter<Object>() {
64             @Override
65             public boolean isDisposed() {
66                 return true;
67             }
68             @Override
69             public void exception(AsyncReadGraph graph, Throwable t) {
70                 System.out.println(getClass().getSimpleName() + ": exception:");
71                 t.printStackTrace();
72                 exc.set(t);
73                 sem.release();
74             }
75             @Override
76             public void execute(AsyncReadGraph graph, Object result) {
77                 if (DEBUG)
78                     System.out.println(getClass().getSimpleName() + ": execute: " + result);
79                 sem.release();
80             }
81         });
82
83         sem.acquire();
84
85         Throwable t = exc.get();
86         if (t != null) {
87             if (t instanceof Exception)
88                 throw (Exception) t;
89             else
90                 throw new Exception(t);
91         }
92     }
93 }