/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.tests.api.request.external; import java.util.HashSet; import java.util.concurrent.Semaphore; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Session; import org.simantics.db.common.request.SingletonPrimitiveRead; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.db.testing.annotation.Fails; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.layer0.Layer0; public class ExternalRequestTest4 extends ExistingDatabaseTest { private Error exception; boolean listenerAlive = true; int counter = 0; HashSet> listeners = new HashSet>(); class Primitive extends SingletonPrimitiveRead { @Override public void register(ReadGraph graph, Listener procedure) { listeners.add(procedure); procedure.execute("Result" + counter); } } public void fireNewPrimitive() { counter++; for(Listener listener : listeners) listener.execute("Result" + counter); } @Test @Fails public void test() throws Exception { Session session = getSession(); final Semaphore s1 = new Semaphore(-6); final Semaphore s2 = new Semaphore(-6); session.syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { Layer0 b = Layer0.getInstance(graph); String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName); String appendix = graph.syncRequest(new Primitive()); return base + appendix; } }, new Listener() { @Override public void exception(Throwable t) { } @Override public void execute(String result) { s1.release(); } @Override public boolean isDisposed() { return !listenerAlive; } }); fireNewPrimitive(); fireNewPrimitive(); fireNewPrimitive(); Thread.sleep(500); session.syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { Layer0 b = Layer0.getInstance(graph); String base = graph.getRelatedValue(graph.getRootLibrary(), b.HasName); String appendix = graph.syncRequest(new Primitive()); return base + appendix; } }, new Listener() { @Override public void exception(Throwable t) { } @Override public void execute(String result) { s2.release(); } @Override public boolean isDisposed() { return false; } }); fireNewPrimitive(); fireNewPrimitive(); fireNewPrimitive(); Thread.sleep(500); listenerAlive = false; fireNewPrimitive(); fireNewPrimitive(); fireNewPrimitive(); s1.acquire(); s2.acquire(); if(exception != null) { fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through "); } } }