Skip to content

Package: RAPConcurrentAccess_PTest

RAPConcurrentAccess_PTest

nameinstructionbranchcomplexitylinemethod
RAPConcurrentAccess_PTest()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getNewTest()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testConcurrentAccess()
M: 3 C: 53
95%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 1 C: 15
94%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Neil Mackenzie - initial implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.core.rap.concurrent.test;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.Iterator;
19:
20: import org.eclipse.emf.common.util.EList;
21: import org.eclipse.emf.common.util.TreeIterator;
22: import org.eclipse.emf.common.util.URI;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EReference;
25: import org.eclipse.emf.ecore.resource.Resource;
26: import org.eclipse.emf.ecore.resource.ResourceSet;
27: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
28: import org.eclipse.emf.ecore.util.EcoreUtil;
29: import org.eclipse.emf.ecp.core.ECPProject;
30: import org.eclipse.emf.ecp.core.ECPProjectManager;
31: import org.eclipse.emf.ecp.core.exceptions.ECPProjectWithNameExistsException;
32: import org.eclipse.emf.ecp.core.rap.sessionprovider.concurrent.test.MockConcurrentSessionProvider;
33: import org.eclipse.emf.ecp.core.util.ECPUtil;
34: import org.eclipse.emf.ecp.emfstore.core.internal.ECPEMFUtils;
35: import org.eclipse.emf.ecp.emfstore.core.internal.EMFStoreProvider;
36: import org.eclipse.emf.edit.command.AddCommand;
37: import org.eclipse.emf.edit.command.ChangeCommand;
38: import org.eclipse.emf.edit.command.SetCommand;
39: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
40: import org.eclipse.emf.edit.domain.EditingDomain;
41: import org.junit.Test;
42:
43: import junit.framework.TestCase;
44:
45: /**
46: *
47: * @author neil
48: * Tests concurrent Access to EMFStorePRovider.
49: */
50: @SuppressWarnings("restriction")
51: public class RAPConcurrentAccess_PTest extends TestCase {
52:
53:         /**
54:          * counter used ito help make unique project names
55:          */
56:         private static int testCounter;
57:         /**
58:          * number of concurrent threads to run
59:          */
60:         private final int numberOfThreads = 5;
61:
62:         /**
63:          * class which opens a creates a project, loads an XMI file with some tasks
64:          * from the makeithappen model, and then reads the contents of th project.
65:          *
66:          * @author neil
67:          *
68:          */
69:         public class TestRun implements Runnable {
70:                 private final int testcount;
71:
72:                 /**
73:                  * Constructor.
74:                  *
75:                  * @param counter
76:                  */
77:                 public TestRun(int counter) {
78:                         testcount = counter;
79:                 }
80:
81:                 /**
82:                  * {@inheritDoc}
83:                  *
84:                  * @see java.lang.Runnable#run()
85:                  */
86:                 @Override
87:                 public void run() {
88:                         final String fileName = "Task.xmi"; //$NON-NLS-1$
89:
90:                         final ECPProjectManager projectManager = ECPUtil.getECPProjectManager();
91:                         ECPProject project1 = null;
92:                         // perhaps delete project if It exists??
93:                         try {
94:                                 project1 = projectManager.createProject(new EMFStoreProvider(),
95:                                         "concurrentTestProject" + testcount); //$NON-NLS-1$
96:                         } catch (final ECPProjectWithNameExistsException ex) {
97:                                 ex.printStackTrace();
98:                         }
99:                         final Object parentObject = project1;
100:                         final URI fileURI = URI.createFileURI(fileName);
101:
102:                         // create resource set and resource
103:                         final ResourceSet resourceSet = new ResourceSetImpl();
104:
105:                         final Resource resource = resourceSet.getResource(fileURI, true);
106:
107:                         final EObject eObjectImport = resource.getContents().get(0);
108:
109:                         // add the items from the model to the project
110:                         if (parentObject instanceof EObject) {
111:
112:                                 for (final EReference ref : ((EObject) parentObject).eClass().getEAllContainments()) {
113:                                         if (ref.getEReferenceType().isInstance(eObjectImport)) {
114:                                                 final EditingDomain editingDomain = AdapterFactoryEditingDomain
115:                                                         .getEditingDomainFor(parentObject);
116:                                                 if (ref.isMany()) {
117:                                                         editingDomain.getCommandStack().execute(
118:                                                                 new AddCommand(editingDomain, (EObject) parentObject, ref, EcoreUtil
119:                                                                         .copy(eObjectImport)));
120:                                                 } else {
121:                                                         editingDomain.getCommandStack().execute(
122:                                                                 new SetCommand(editingDomain, (EObject) parentObject, ref, EcoreUtil
123:                                                                         .copy(eObjectImport)));
124:                                                 }
125:
126:                                                 break;
127:                                         }
128:                                 }
129:                         } else if (parentObject instanceof ECPProject) {
130:                                 final EditingDomain editingDomain = ((ECPProject) parentObject).getEditingDomain();
131:                                 editingDomain.getCommandStack().execute(new ChangeCommand(eObjectImport) {
132:
133:                                         @Override
134:                                         protected void doExecute() {
135:                                                 ((ECPProject) parentObject).getContents().add(EcoreUtil.copy(eObjectImport));
136:                                         }
137:                                 });
138:                         }
139:
140:                         final EList<Object> list = project1.getContents();
141:
142:                         final EObject root = (EObject) list.get(0);
143:                         final TreeIterator<EObject> iter = EcoreUtil.getAllContents(root, true);
144:
145:                         // output the contents of the project.
146:                         while (iter.hasNext()) {
147:                                 System.out.println(iter.next().toString());
148:                         }
149:
150:                 }
151:
152:         }
153:
154:         /**
155:          * Tests concurrent Access to EMFStorePRovider.
156:          */
157:         @Test
158:         public final void testConcurrentAccess() {
159:
160:                 MockConcurrentSessionProvider.getInstance();
161:                 // Initialisation of the EMFStore workspace is not currently threadsafe in
162:                 // EMFStore client in EMFSTore 1.8, so we initialize it first in this thread.
163:
164:                 ECPEMFUtils.getESWorkspaceProviderInstance();
165:
166:                 final Collection<Thread> collection = new ArrayList<Thread>();
167:
168:                 // because we are using a service factory for the RAP implementation we must unget
169:                 // the service so that the service factory is called again on each call. otherwise
170:                 // the service factor will keep returning the same cached instance as the reference
171:                 // count of the service will remain greater than zero
172:
173:•                for (int i = 0; i < numberOfThreads; i++) {
174:                         final Runnable runnable = getNewTest();
175:                         final Thread t = new Thread(runnable);
176:                         t.setName("ECPConcurrentTestThread" + i); //$NON-NLS-1$
177:                         // add the thread to the collection so that we can later
178:                         // wait for all these threads to finish.
179:                         collection.add(t);
180:                         t.start();
181:                 }
182:                 // we wait for all the started threads to finish
183:                 // this is because we don't want the main testing thread to finish
184:                 // until the newly started threads have finished. It the main test
185:                 // thread finished first then the other started threads throw errors
186:                 // that some bundles appear to be not resolved.
187:
188:                 final Iterator<Thread> iter = collection.iterator();
189:
190:•                while (iter.hasNext()) {
191:                         final Thread thread = iter.next();
192:                         try {
193:                                 thread.join();
194:                         } catch (final InterruptedException ex) {
195:                                 ex.printStackTrace();
196:                         }
197:                 }
198:
199:         }
200:
201:         /**
202:          * create a test to run in a thread.
203:          *
204:          * @return a Runnable test
205:          */
206:         private Runnable getNewTest() {
207:                 testCounter++;
208:                 return new TestRun(testCounter);
209:
210:         }
211: }