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.client;
14 import java.util.Collection;
16 import org.junit.After;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.testing.annotation.Fails;
25 import org.simantics.db.testing.common.ExceptionUtils;
26 import org.simantics.db.testing.common.TestBase;
27 import org.simantics.db.testing.common.Tests;
28 import org.simantics.db.testing.common.WriteQuery;
29 import org.simantics.layer0.Layer0;
32 * Tests how interface handles Servers crash
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class CoreCrashTest extends TestBase {
38 private static final int WAIT_TIME = 200;
39 private Throwable error;
41 public void setUp() throws Exception {
46 public void tearDown() throws Exception {
48 Tests.getTestHandler().getServer().stop();
49 } catch (Exception e) {
55 public Session getSession() throws DatabaseException {
56 return Tests.getTestHandler().getSession();
59 * Tests how graph interface handles server crash when the crash occurs between transactions
62 public void testCrash() throws Exception {
63 final String randomName = "Name " + getRandomString();
66 Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) {
68 public void run(WriteGraph g) throws DatabaseException {
69 Layer0 b = Layer0.getInstance(g);
70 Resource instance = g.newResource();
71 g.claim(g.getRootLibrary(), b.ConsistsOf, instance);
72 g.claim(instance, b.InstanceOf, null, b.Type);
73 g.claimLiteral(instance, b.HasName, randomName);
79 // this should create an error
80 Tests.getTestHandler().getSession().asyncRequest(new TestReadRequest() {
82 public void run(ReadGraph g) throws DatabaseException {
83 Layer0 b = Layer0.getInstance(g);
84 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
85 fail("Missing exception after core killed.");
86 for (Resource r : resources) {
87 g.getRelatedValue(r, b.HasName);
92 // Wait for read request to complete
93 for (int i = 0; i < 10; i++) {
95 Thread.sleep(WAIT_TIME);
96 } catch (InterruptedException e) {
102 if (!hasException()) {
103 throw new Exception("Connection failure before or during request did not cause an error");
105 error = getException();
106 System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error) );
110 * Tests how graph interface handles server crash when the crash occurs between transactions
115 public void testCrash2() throws Exception {
116 final String randomName = "Name " + getRandomString();
121 Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) {
123 public void run(WriteGraph g) throws DatabaseException {
124 Layer0 b = Layer0.getInstance(g);
125 Resource instance = g.newResource();
126 g.claim(g.getRootLibrary(), b.ConsistsOf, instance);
127 g.claim(instance, b.InstanceOf, null, b.Type);
128 g.claimLiteral(instance, b.HasName, randomName);
131 if (!hasException()) {
132 throw new Exception("Connection failure did not cause an error");
134 error = getException();
135 System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error));