@@ -148,24 +148,30 @@ protected void invokeBeforeClassMethods(ITestClass testClass, IMethodInstance mi
148148
149149 // the whole invocation must be synchronized as other threads must
150150 // get a full initialized test object (not the same for @After)
151- Map <ITestClass , Set <Object >> invokedBeforeClassMethods = m_classMethodMap .getInvokedBeforeClassMethods ();
152- Set <Object > instances = invokedBeforeClassMethods .get (testClass );
153- if (null == instances ) {
154- instances = new HashSet <>();
155- invokedBeforeClassMethods .put (testClass , instances );
156- }
157- Object instance = mi .getInstance ();
158- if (!instances .contains (instance )) {
159- instances .add (instance );
160- for (IClassListener listener : m_listeners ) {
161- listener .onBeforeClass (testClass );
151+ // Synchronization on local variables is generally considered a bad practice, but this is an exception.
152+ // We need to ensure that two threads that are querying for the same "Class" then they
153+ // should be mutually exclusive. In all other cases, parallelism can be allowed.
154+ //DO NOT REMOVE THIS SYNC LOCK.
155+ synchronized (testClass ) {
156+ Map <ITestClass , Set <Object >> invokedBeforeClassMethods = m_classMethodMap .getInvokedBeforeClassMethods ();
157+ Set <Object > instances = invokedBeforeClassMethods .get (testClass );
158+ if (null == instances ) {
159+ instances = new HashSet <>();
160+ invokedBeforeClassMethods .put (testClass , instances );
161+ }
162+ Object instance = mi .getInstance ();
163+ if (!instances .contains (instance )) {
164+ instances .add (instance );
165+ for (IClassListener listener : m_listeners ) {
166+ listener .onBeforeClass (testClass );
167+ }
168+ m_invoker .invokeConfigurations (testClass ,
169+ testClass .getBeforeClassMethods (),
170+ m_suite ,
171+ m_parameters ,
172+ null , /* no parameter values */
173+ instance );
162174 }
163- m_invoker .invokeConfigurations (testClass ,
164- testClass .getBeforeClassMethods (),
165- m_suite ,
166- m_parameters ,
167- null , /* no parameter values */
168- instance );
169175 }
170176 }
171177
0 commit comments