]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/CleanShutdownTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / CleanShutdownTest.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.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.testing.annotation.Fails;
22 import org.simantics.db.testing.common.TestBase;
23 import org.simantics.db.testing.common.Tests;
24 import org.simantics.db.testing.common.WriteQuery;
25 import org.simantics.layer0.Layer0;
26 /**
27  * Tests if session can be closed after core crash.
28  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29  *
30  */
31 public class CleanShutdownTest extends TestBase {
32         private boolean completed = false;
33
34     @Test
35     @Fails
36         public void testShutdown() throws Exception {
37                 completed = false;
38                 Thread t = new Thread() {
39                         @Override
40                         public void run() {
41                                 try {
42                                         Tests.getTestHandler().getSession().syncRequest(new WriteQuery(CleanShutdownTest.this) {
43                                                 @Override
44                                                 public void run(WriteGraph g) throws DatabaseException {
45                                                     Layer0 b = Layer0.getInstance(g);
46                                                         Resource instance = g.newResource();
47                                                         g.claim(g.getRootLibrary(), b.ConsistsOf, instance);
48                                                         g.claim(instance, b.InstanceOf, null, b.Type);
49                                                         g.claimLiteral(instance, b.HasName, "Name");
50                                                 }
51                                         });
52                                 } catch (DatabaseException e1) {
53                                         e1.printStackTrace();
54                                 }
55                 try {
56                     Tests.killCore(); // kill the core before read transaction
57                 } catch (DatabaseException e1) {
58                     fail("Killing core failed: " + e1.getMessage());
59                 }
60                                 try {
61                             Tests.getTestHandler().getSession().syncRequest(new TestReadRequest() {
62                                                 @Override
63                                                 public void run(ReadGraph g) throws DatabaseException {
64                                                     Layer0 b = Layer0.getInstance(g);
65                                                         Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
66                                                         for (Resource r : resources) {
67                                                                 g.getRelatedValue(r, b.HasName);
68                                                         }
69                                                 }
70                                 });
71                                 } catch (Exception e) {
72                                         // this is the correct behavior for this test
73                                 }
74                                 try {
75                     Tests.closeSession(Tests.getTestHandler().getSession());
76                 } catch (DatabaseException e) {
77                     e.printStackTrace();
78                 }
79                                 completed = true;
80                         }
81                 };
82                 t.start();
83
84                 for (int i = 0; i < 30; i++) {
85                         try {
86                                 Thread.sleep(1000);
87                         } catch (InterruptedException e) {
88
89                         }
90                         if (completed)
91                                 break;
92                 }
93                 if (!completed)
94                         throw new Exception("Core did not shutdown in 30 seconds");
95         }
96
97 }