-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft: Implement adding ExclusiveResource programmatically.
Issue: #2677
- Loading branch information
Vladimir Dmitrienko
committed
Jul 16, 2024
1 parent
6d2ae1d
commit a51a66b
Showing
9 changed files
with
370 additions
and
3 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
junit-jupiter-api/src/main/java/org/junit/jupiter/api/parallel/ExclusiveResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api.parallel; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Inherited | ||
public @interface ExclusiveResources { | ||
|
||
Class<? extends ExclusiveResourcesProvider>[] value(); | ||
} |
53 changes: 53 additions & 0 deletions
53
...-jupiter-api/src/main/java/org/junit/jupiter/api/parallel/ExclusiveResourcesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api.parallel; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
public interface ExclusiveResourcesProvider { | ||
default Set<ExclusiveResource> provideForClass(Class<?> testClass) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
default Set<ExclusiveResource> provideForMethod(Class<?> testClass, Method testMethod) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
final class ExclusiveResource { | ||
|
||
private final String key; | ||
|
||
private final ResourceAccessMode accessMode; | ||
|
||
public ExclusiveResource(String key, ResourceAccessMode accessMode) { | ||
this.key = key; | ||
this.accessMode = accessMode; | ||
} | ||
|
||
public static ExclusiveResource readLockOf(String key) { | ||
return new ExclusiveResource(key, ResourceAccessMode.READ); | ||
} | ||
|
||
public static ExclusiveResource readWriteLockOf(String key) { | ||
return new ExclusiveResource(key, ResourceAccessMode.READ_WRITE); | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public ResourceAccessMode getAccessMode() { | ||
return accessMode; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...upiter-api/src/main/java/org/junit/jupiter/api/parallel/MyExclusiveResourcesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.jupiter.api.parallel; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
public final class MyExclusiveResourcesProvider implements ExclusiveResourcesProvider { | ||
|
||
public MyExclusiveResourcesProvider() { | ||
} | ||
|
||
@Override | ||
public Set<ExclusiveResource> provideForClass(Class<?> testClass) { | ||
// Distribute locks depending on class name, package name, declared fields etc. | ||
// ... | ||
return Stream.of(ExclusiveResource.readWriteLockOf("class_lock")).collect(toSet()); | ||
} | ||
|
||
@Override | ||
public Set<ExclusiveResource> provideForMethod(Class<?> testClass, Method testMethod) { | ||
// Distribute locks depending on method name, parameters etc. | ||
// ... | ||
return Stream.of(ExclusiveResource.readWriteLockOf("method_lock")).collect(toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...rg/junit/platform/engine/support/hierarchical/ExclusiveResourcesProviderOnClassTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.engine.support.hierarchical; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.ExclusiveResources; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.junit.jupiter.api.parallel.MyExclusiveResourcesProvider; | ||
|
||
@Execution(ExecutionMode.CONCURRENT) | ||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
class ExclusiveResourcesProviderOnClassTests { | ||
|
||
@Test | ||
void a() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void b() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void c() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void d() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void e() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void f() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void g() { | ||
sleep(); | ||
} | ||
|
||
@Test | ||
void h() { | ||
sleep(); | ||
} | ||
|
||
static void sleep() { | ||
try { | ||
Thread.sleep(1000); | ||
} | ||
catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...g/junit/platform/engine/support/hierarchical/ExclusiveResourcesProviderOnMethodTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2015-2024 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.engine.support.hierarchical; | ||
|
||
import static org.junit.platform.engine.support.hierarchical.ExclusiveResourcesProviderOnClassTests.sleep; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.ExclusiveResources; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.junit.jupiter.api.parallel.MyExclusiveResourcesProvider; | ||
|
||
@Execution(ExecutionMode.CONCURRENT) | ||
class ExclusiveResourcesProviderOnMethodTests { | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void a() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void b() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void c() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void d() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void e() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void f() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void g() { | ||
sleep(); | ||
} | ||
|
||
@ExclusiveResources(MyExclusiveResourcesProvider.class) | ||
@Test | ||
void h() { | ||
sleep(); | ||
} | ||
} |
Oops, something went wrong.