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.request.misc;
14 import java.util.concurrent.atomic.AtomicInteger;
16 import org.junit.Test;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.request.AsyncReadRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.AsyncListener;
23 import org.simantics.db.procedure.AsyncProcedure;
24 import org.simantics.db.request.AsyncRead;
25 import org.simantics.db.request.Read;
26 import org.simantics.db.service.QueryControl;
27 import org.simantics.db.service.QueryControl.ControlProcedure;
28 import org.simantics.db.testing.base.ExistingDatabaseTest;
30 public class RequestQueuingTest extends ExistingDatabaseTest {
32 final private static int LOOPS = 10;
34 private Object RESULT = new Object();
35 private DatabaseException EXCEPTION = new DatabaseException();
37 private AtomicInteger results = new AtomicInteger(0);
38 private AtomicInteger exceptions = new AtomicInteger(0);
40 class A implements AsyncRead<Object> {
42 private void perform(AsyncReadGraph graph, final AsyncProcedure<Object> procedure, final int todo) {
46 procedure.execute(graph, RESULT);
50 final QueryControl control = graph.getService(QueryControl.class);
51 final int current = control.getGraphThread(graph);
52 final int next = (current + 1) % control.getAmountOfQueryThreads();
53 control.schedule(graph, next, new ControlProcedure() {
55 public void execute(AsyncReadGraph graph) {
56 perform(graph, procedure, todo-1);
67 public void perform(AsyncReadGraph graph, final AsyncProcedure<Object> procedure) {
68 perform(graph, procedure, 5);
72 public int threadHash() {
77 public int getFlags() {
83 class B implements Read<Object> {
86 public Object perform(ReadGraph graph) throws DatabaseException {
88 return graph.syncRequest(new A());
94 class A2 implements AsyncRead<Object> {
96 private void perform(AsyncReadGraph graph, final AsyncProcedure<Object> procedure, final int todo) {
100 procedure.exception(graph, EXCEPTION);
104 final QueryControl control = graph.getService(QueryControl.class);
105 final int current = control.getGraphThread(graph);
106 final int next = (current + 1) % control.getAmountOfQueryThreads();
107 control.schedule(graph, next, new ControlProcedure() {
109 public void execute(AsyncReadGraph graph) {
110 perform(graph, procedure, todo-1);
121 public void perform(AsyncReadGraph graph, final AsyncProcedure<Object> procedure) {
122 perform(graph, procedure, 5);
126 public int threadHash() {
131 public int getFlags() {
137 class B2 implements Read<Object> {
140 public Object perform(ReadGraph graph) throws DatabaseException {
142 return graph.syncRequest(new A2());
148 class C implements AsyncListener<Object> {
151 public void execute(AsyncReadGraph graph, Object result) {
152 if(RESULT == result) results.incrementAndGet();
156 public void exception(AsyncReadGraph graph, Throwable throwable) {
157 if(EXCEPTION == throwable) exceptions.incrementAndGet();
161 public boolean isDisposed() {
168 public void test() throws Exception {
170 Session session = getSession();
172 session.syncRequest(new AsyncReadRequest() {
175 public void run(AsyncReadGraph graph) {
181 for(int i=0;i<LOOPS;i++) graph.asyncRequest(a, new C());
182 for(int i=0;i<LOOPS;i++) graph.asyncRequest(a2, new C());
183 for(int i=0;i<LOOPS;i++) graph.asyncRequest(b, new C());
184 for(int i=0;i<LOOPS;i++) graph.asyncRequest(b2, new C());
190 if(results.get() != 2*LOOPS) fail("Incorrect amount of reported results (expected " + 2*LOOPS + ", got " + results.get() + ")");
191 if(exceptions.get() != 2*LOOPS) fail("Incorrect amount of reported exceptions (expected " + 2*LOOPS + ", got " + exceptions.get() + ")");