]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/request/WriteCancelTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / request / WriteCancelTest2.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.api.write.request;
13
14 import org.junit.Test;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.CancelTransactionException;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.service.TransactionSupport;
23 import org.simantics.db.testing.base.ExistingDatabaseTest;
24 import org.simantics.layer0.Layer0;
25
26 /**
27  * Checks that asynchronous read requests work correctly with canceled writes
28  * 
29  * @author Antti Villberg
30  */
31 public class WriteCancelTest2 extends ExistingDatabaseTest {
32
33     private static final int SIZE = 10000;
34
35     Resource[] written = new Resource[SIZE];
36
37         @Test
38     public void testWriteCancel2() throws DatabaseException{
39
40         // First create a set of resources for testing
41         try {
42
43             getSession().syncRequest(new WriteRequest() {
44                 @Override
45                 public void perform(WriteGraph graph) throws DatabaseException {
46                     if (DEBUG)
47                         System.err.println("a");
48                     for(int i=0;i<SIZE;i++) written[i] = graph.newResource();
49                     if (DEBUG)
50                         System.err.println("a2");
51                 }
52             });
53             
54         } catch (DatabaseException e) {
55             if (DEBUG)
56                 e.printStackTrace();
57         }
58
59         // Make a decent amount of modifications and cancel them
60         getSession().asyncRequest(new WriteRequest() {
61             @Override
62             public void perform(WriteGraph graph) throws DatabaseException {
63                 Layer0 L0 = Layer0.getInstance(graph);
64                 if (DEBUG)
65                     System.err.println("b");
66                 for(int i=0;i<SIZE;i++) graph.claim(written[i], L0.IsComposedOf, graph.newResource());
67                 if (DEBUG)
68                     System.err.println("b2");
69                 throw new CancelTransactionException("Intentional write request cancellation");
70             }
71         });
72         
73         // Ensure that the previously written data does not exist.
74         getSession().asyncRequest(new ReadRequest() {
75             @Override
76             public void run(ReadGraph graph) throws DatabaseException {
77
78                 Layer0 L0 = Layer0.getInstance(graph);
79                 if (DEBUG)
80                     System.err.println("c");
81                 for(int i=0;i<SIZE;i++)
82                     assertTrue(!graph.hasStatement(written[i], L0.IsComposedOf));
83                 if (DEBUG)
84                     System.err.println("c2");
85
86             }
87         });
88
89         // Do not let the test die before asynchronous requests have finished
90         TransactionSupport support = getSession().getService(TransactionSupport.class);
91         support.waitCompletion();
92         
93     }
94
95 }