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.client;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.junit.Test;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.event.SessionEvent;
22 import org.simantics.db.event.SessionListener;
23 import org.simantics.db.testing.base.ExistingDatabaseTest;
24 import org.simantics.db.testing.common.Tests;
25 import org.simantics.db.testing.common.WriteQuery;
26 import org.simantics.db.tests.common.Configuration;
27 import org.simantics.layer0.Layer0;
29 import fi.vtt.simantics.procore.SessionManagerSource;
32 * Tests how server handles multiple simultaneous connections.
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class ConnectionTest extends ExistingDatabaseTest {
39 private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
40 private static final int THREAD_COUNT = Configuration.get().connectionThreadCount;
41 private static final int WAIT_TIME = 10;
42 private static boolean WRITE = false;
44 private Throwable error;
45 private List<ConnectionThread> threads = new ArrayList<ConnectionThread>();
46 private volatile int count = 0; // not totally correct code but works for me
47 public void setError(Throwable error) {
48 if (this.error == null) {
50 for (ConnectionThread t : threads) {
56 private boolean notDone() {
57 for (ConnectionThread t : threads) {
66 * Tests how server can handle multiple simultaneous connections
69 static int sessionClosed = 0;
70 private synchronized void inc() {
74 public void testConnection() throws Exception {
75 for (int i = 0; i < THREAD_COUNT; i++) {
76 ConnectionThread t = new ConnectionThread();
81 System.out.println("Created " + THREAD_COUNT + " threads.");
83 SessionManagerSource.getSessionManager().addSessionListener(
84 new SessionListener() {
86 public void sessionClosed(SessionEvent e) {
88 System.out.println("Session closed s=" + e.getSession());
89 if (null != e.getCause())
90 e.getCause().printStackTrace();
96 public void sessionOpened(SessionEvent e) {
98 System.out.println("Session opened s=" + e.getSession());
102 public void sessionException(SessionEvent e) {
104 System.out.println("Session exception s=" + e.getSession());
105 if (null != e.getCause())
106 e.getCause().printStackTrace();
110 // Wait for correct number of closes.
111 while (RECONNECT_COUNT * THREAD_COUNT != sessionClosed) {
115 } catch (InterruptedException e) {
117 System.out.println("Interrupted.");
121 // Wait for write request to complete
124 int currentCount = count;
126 Thread.sleep(WAIT_TIME * 1000);
127 } catch (InterruptedException e) {
129 System.out.println("Interrupted.");
132 System.out.println(count);
133 if (count == currentCount)
137 // check success flag
139 throw new Exception("First error was",error);
142 throw new Exception("Test was not completed, server did not resepond for "
143 + WAIT_TIME + " seconds");
147 private class ConnectionThread extends Thread {
148 private boolean done = false;
149 private boolean disposed = false;
150 public ConnectionThread() {
154 for (int i = 0; i < RECONNECT_COUNT; i++) {
158 final Session session = Tests.getTestHandler().getSession();
159 final Resource rootLibrary = session.getRootLibrary();
160 assertTrue(rootLibrary != null);
161 final String name = this.getName() + " " + Integer.toString(i);
163 session.syncRequest(new WriteQuery(ConnectionTest.this) {
165 public void run(WriteGraph g) throws Throwable {
166 Layer0 b = Layer0.getInstance(g);
167 Resource newResource = g.newResource();
168 g.claim(newResource, b.InstanceOf, null, b.Type);
169 g.claimLiteral(newResource, b.HasName, name);
170 g.claim(rootLibrary, b.ConsistsOf, newResource);
174 Tests.closeSession(session);
175 } catch (Exception e) {
182 System.out.println("count=" + count);
186 public boolean isDone() {
190 public void dispose() {