forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix concurrent execution for PER_CLASS lifecycle
- Loading branch information
1 parent
e3fffdd
commit 35c9c34
Showing
5 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
...st/java/io/helidon/microprofile/tests/testing/junit5/TestPerClassConcurrentExecution.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,92 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.microprofile.tests.testing.junit5; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import io.helidon.microprofile.testing.junit5.AddBean; | ||
import io.helidon.microprofile.testing.junit5.HelidonTest; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@HelidonTest | ||
@AddBean(TestPerClassConcurrentExecution.State.class) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class TestPerClassConcurrentExecution { | ||
|
||
@Inject | ||
State state; | ||
|
||
@Test | ||
@Execution(ExecutionMode.CONCURRENT) | ||
void testA() throws InterruptedException { | ||
state.add("a"); | ||
state.countDownA(); | ||
state.awaitB(); | ||
assertThat(state.get(), is(Set.of("a", "b"))); | ||
} | ||
|
||
@Test | ||
@Execution(ExecutionMode.CONCURRENT) | ||
void testB() throws InterruptedException { | ||
state.awaitA(); | ||
assertThat(state.get(), is(Set.of("a"))); | ||
state.add("b"); | ||
state.countDownB(); | ||
} | ||
|
||
@ApplicationScoped | ||
static class State { | ||
|
||
final Set<String> events = ConcurrentHashMap.newKeySet(); | ||
final CountDownLatch latchA = new CountDownLatch(1); | ||
final CountDownLatch latchB = new CountDownLatch(1); | ||
|
||
void add(String event) { | ||
events.add(event); | ||
} | ||
|
||
Set<String> get() { | ||
return events; | ||
} | ||
|
||
void countDownA() { | ||
latchA.countDown(); | ||
} | ||
|
||
void awaitA() throws InterruptedException { | ||
latchA.await(); | ||
} | ||
|
||
void countDownB() { | ||
latchB.countDown(); | ||
} | ||
|
||
void awaitB() throws InterruptedException { | ||
latchB.await(); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...src/test/java/io/helidon/microprofile/tests/testing/junit5/TestPerClassImplicitReset.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 (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.microprofile.tests.testing.junit5; | ||
|
||
import io.helidon.microprofile.testing.junit5.AddBean; | ||
import io.helidon.microprofile.testing.junit5.HelidonTest; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@HelidonTest | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class TestPerClassImplicitReset { | ||
|
||
@Inject | ||
CounterBean counterBean; | ||
|
||
@Test | ||
@AddBean(CounterBean.class) | ||
void firstTest() { | ||
assertThat(counterBean.value++, is(0)); | ||
} | ||
|
||
@Test | ||
@AddBean(CounterBean.class) | ||
void secondTest() { | ||
assertThat(counterBean.value++, is(0)); | ||
} | ||
|
||
@ApplicationScoped | ||
static class CounterBean { | ||
int value = 0; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
microprofile/tests/testing/junit5/src/test/resources/junit-platform.properties
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,16 @@ | ||
# | ||
# Copyright (c) 2024 Oracle and/or its affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
junit.jupiter.execution.parallel.enabled=true |