X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fclient%2FCoreCrashTest.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fclient%2FCoreCrashTest.java;h=2cec94fb86585c62b16481d2369f7e1c806884eb;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/CoreCrashTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/CoreCrashTest.java new file mode 100644 index 000000000..2cec94fb8 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/CoreCrashTest.java @@ -0,0 +1,137 @@ +/******************************************************************************* + * 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)); + } +}