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.api.write.request;
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;
27 * Checks that asynchronous read requests work correctly with canceled writes
29 * @author Antti Villberg
31 public class WriteCancelTest2 extends ExistingDatabaseTest {
33 private static final int SIZE = 10000;
35 Resource[] written = new Resource[SIZE];
38 public void testWriteCancel2() throws DatabaseException{
40 // First create a set of resources for testing
43 getSession().syncRequest(new WriteRequest() {
45 public void perform(WriteGraph graph) throws DatabaseException {
47 System.err.println("a");
48 for(int i=0;i<SIZE;i++) written[i] = graph.newResource();
50 System.err.println("a2");
54 } catch (DatabaseException e) {
59 // Make a decent amount of modifications and cancel them
60 getSession().asyncRequest(new WriteRequest() {
62 public void perform(WriteGraph graph) throws DatabaseException {
63 Layer0 L0 = Layer0.getInstance(graph);
65 System.err.println("b");
66 for(int i=0;i<SIZE;i++) graph.claim(written[i], L0.IsComposedOf, graph.newResource());
68 System.err.println("b2");
69 throw new CancelTransactionException("Intentional write request cancellation");
73 // Ensure that the previously written data does not exist.
74 getSession().asyncRequest(new ReadRequest() {
76 public void run(ReadGraph graph) throws DatabaseException {
78 Layer0 L0 = Layer0.getInstance(graph);
80 System.err.println("c");
81 for(int i=0;i<SIZE;i++)
82 assertTrue(!graph.hasStatement(written[i], L0.IsComposedOf));
84 System.err.println("c2");
89 // Do not let the test die before asynchronous requests have finished
90 TransactionSupport support = getSession().getService(TransactionSupport.class);
91 support.waitCompletion();