]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/CoreCrashTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / CoreCrashTest.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.client;
13
14 import java.util.Collection;
15
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;
30
31 /**
32  * Tests how interface handles Servers crash
33  *
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class CoreCrashTest extends TestBase {
38         private static final int WAIT_TIME = 200;
39         private Throwable error;
40         @Before
41         public void setUp() throws Exception {
42                 super.setUp();
43         }
44
45         @After
46         public void tearDown() throws Exception {
47                 try {
48                     Tests.getTestHandler().getServer().stop();
49                 } catch (Exception e) {
50
51                 }
52                 super.tearDown();
53         }
54         @Override
55         public Session getSession() throws DatabaseException {
56                 return Tests.getTestHandler().getSession();
57         }
58         /**
59          * Tests how graph interface handles server crash when the crash occurs between transactions
60          * @throws Exception
61          */
62         public void testCrash() throws Exception {
63                 final String randomName = "Name " + getRandomString();
64
65                 // create something
66                 Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) {
67                         @Override
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);
74                         }
75                 });
76                 checkException();
77                 Tests.killCore();
78
79                 // this should create an error
80                 Tests.getTestHandler().getSession().asyncRequest(new TestReadRequest() {
81                         @Override
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);
88                                 }
89                         }
90                 });
91
92                 // Wait for read request to complete
93                 for (int i = 0; i < 10; i++) {
94                         try {
95                                 Thread.sleep(WAIT_TIME);
96                         } catch (InterruptedException e) {
97
98                         }
99                         if (hasException())
100                                 break;
101                 }
102                 if (!hasException()) {
103                         throw new Exception("Connection failure before or during request did not cause an error");
104                 }
105                 error = getException();
106                 System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error) );
107         }
108
109         /**
110          * Tests how graph interface handles server crash when the crash occurs between transactions
111          * @throws Exception
112          */
113     @Test
114     @Fails
115         public void testCrash2() throws Exception {
116                 final String randomName = "Name " + getRandomString();
117
118                 Tests.killCore();
119
120                 // create something
121                 Tests.getTestHandler().getSession().syncRequest(new WriteQuery(this) {
122                         @Override
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);
129                         }
130                 });
131                 if (!hasException()) {
132                         throw new Exception("Connection failure did not cause an error");
133                 }
134                 error = getException();
135                 System.out.println("Connection failure caused an error:\n" + ExceptionUtils.getStackTrace(error));
136         }
137 }