]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest.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.ArrayList;
15 import java.util.List;
16
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;
28
29 import fi.vtt.simantics.procore.SessionManagerSource;
30
31 /**
32  * Tests how server handles multiple simultaneous connections.
33  *
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class ConnectionTest extends ExistingDatabaseTest {
38
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;
43
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) {
49             this.error = error;
50             for (ConnectionThread t : threads) {
51                 t.dispose();
52             }
53         }
54     }
55
56     private boolean notDone() {
57         for (ConnectionThread t : threads) {
58             if(!t.isDone()) {
59                 return true;
60             }
61         }
62         return false;
63     }
64
65     /**
66      * Tests how server can handle multiple simultaneous connections
67      * @throws Exception
68      */
69     static int sessionClosed = 0;
70     private synchronized void inc() {
71         ++sessionClosed;
72     }
73     @Test
74     public void testConnection() throws Exception {
75         for (int i = 0; i < THREAD_COUNT; i++) {
76             ConnectionThread t = new ConnectionThread();
77             threads.add(t);
78             t.start();
79         }
80         if (DEBUG)
81             System.out.println("Created " + THREAD_COUNT + " threads.");
82         sessionClosed = 0;
83         SessionManagerSource.getSessionManager().addSessionListener(
84             new SessionListener() {
85                 @Override
86                 public void sessionClosed(SessionEvent e) {
87                     if (DEBUG)
88                         System.out.println("Session closed s=" + e.getSession());
89                     if (null != e.getCause())
90                         e.getCause().printStackTrace();
91                     else
92                         inc();
93                 }
94
95                 @Override
96                 public void sessionOpened(SessionEvent e) {
97                     if (DEBUG)
98                         System.out.println("Session opened s=" + e.getSession());
99                 }
100
101                 @Override
102                 public void sessionException(SessionEvent e) {
103                     if (DEBUG)
104                         System.out.println("Session exception s=" + e.getSession());
105                     if (null != e.getCause())
106                         e.getCause().printStackTrace();
107                 }
108             }
109         );
110         // Wait for correct number of closes.
111         while (RECONNECT_COUNT * THREAD_COUNT != sessionClosed) {
112             checkException();
113             try {
114                 Thread.sleep(100);
115             } catch (InterruptedException e) {
116                 if (DEBUG)
117                     System.out.println("Interrupted.");
118             }
119         }
120
121         // Wait for write request to complete
122         while (notDone()) {
123             checkException();
124             int currentCount = count;
125             try {
126                 Thread.sleep(WAIT_TIME * 1000);
127             } catch (InterruptedException e) {
128                 if (DEBUG)
129                     System.out.println("Interrupted.");
130             }
131             if (DEBUG)
132                 System.out.println(count);
133             if (count == currentCount)
134                 break;
135         }
136         checkException();
137         // check success flag
138         if (error != null) {
139             throw new Exception("First error was",error);
140         }
141         if (notDone())
142             throw new Exception("Test was not completed, server did not resepond for "
143                     + WAIT_TIME + " seconds");
144     }
145
146
147     private class ConnectionThread extends Thread {
148         private boolean done = false;
149         private boolean disposed = false;
150         public ConnectionThread() {
151         }
152         @Override
153         public void run() {
154             for (int i = 0; i < RECONNECT_COUNT; i++) {
155                 if (disposed)
156                     return;
157                 try {
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);
162                     if (WRITE) {
163                         session.syncRequest(new WriteQuery(ConnectionTest.this) {
164                             @Override
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);
171                             }
172                         });
173                     }
174                     Tests.closeSession(session);
175                 } catch (Exception e) {
176                     setError(e);
177                     return;
178                 }
179                 ++count;
180             }
181             if (DEBUG)
182                 System.out.println("count=" + count);
183             done = true;
184         }
185
186         public boolean isDone() {
187             return done;
188         }
189
190         public void dispose() {
191             disposed = true;
192         }
193     }
194
195 }