/******************************************************************************* * 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.client; import java.util.Collection; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.testing.annotation.Fails; import org.simantics.db.testing.common.ExceptionUtils; import org.simantics.db.testing.common.TestBase; import org.simantics.db.testing.common.Tests; import org.simantics.db.testing.common.WriteQuery; import org.simantics.layer0.Layer0; /** * Tests how interface handles Servers crash * * @author Marko Luukkainen * */ public class CoreCrashTest extends TestBase { private static final int WAIT_TIME = 200; private Throwable error; @Before public void setUp() throws Exception { super.setUp(); } @After public void tearDown() throws Exception { try { Tests.getTestHandler().getServer().stop(); } catch (Exception e) { } super.tearDown(); } @Override public Session getSession() throws DatabaseException { return Tests.getTestHandler().getSession(); } /** * Tests how graph interface handles server crash when the crash occurs between transactions * @throws Exception */ public void testCrash() throws Exception { final String randomName = "Name " + getRandomString(); // create something Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Resource instance = g.newResource(); g.claim(g.getRootLibrary(), b.ConsistsOf, instance); g.claim(instance, b.InstanceOf, null, b.Type); g.claimLiteral(instance, b.HasName, randomName); } }); checkException(); Tests.killCore(); // this should create an error Tests.getTestHandler().getSession().asyncRequest(new TestReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); fail("Missing exception after core killed."); for (Resource r : resources) { g.getRelatedValue(r, b.HasName); } } }); // Wait for read request to complete for (int i = 0; i < 10; i++) { try { Thread.sleep(WAIT_TIME); } catch (InterruptedException e) { } if (hasException()) break; } if (!hasException()) { throw new Exception("Connection failure before or during request did not cause an error"); } error = getException(); System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error) ); } /** * Tests how graph interface handles server crash when the crash occurs between transactions * @throws Exception */ @Test @Fails public void testCrash2() throws Exception { final String randomName = "Name " + getRandomString(); Tests.killCore(); // create something Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Resource instance = g.newResource(); g.claim(g.getRootLibrary(), b.ConsistsOf, instance); g.claim(instance, b.InstanceOf, null, b.Type); g.claimLiteral(instance, b.HasName, randomName); } }); if (!hasException()) { throw new Exception("Connection failure did not cause an error"); } error = getException(); System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error)); } }