Skip to content

Commit

Permalink
Fix concurrent issue during bundle file concurrent generation testing (
Browse files Browse the repository at this point in the history
…halo-dev#6043)

#### What type of PR is this?

/kind failing-test
/area core
/milestone 2.17.x

#### What this PR does / why we need it:

I wrongly invoked `Arraylist#add`(probes) method in multi threads. So the unit test was unstable and might encounter the problem as follows:

```java
Expected :1
Actual   :0
<Click to see difference>

org.opentest4j.AssertionFailedError: expected: <1> but was: <0>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
	at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:166)
	at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:161)
	at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:632)
	at run.halo.app.core.extension.service.impl.PluginServiceImplTest$BundleCacheTest.concurrentComputeBundleFileIfAbsent(PluginServiceImplTest.java:460)
```

See https://github.com/halo-dev/halo/actions/runs/9382059472/job/25832681545 for more.

This PR moves the invocation outside thread tasks.

#### Does this PR introduce a user-facing change?

```release-note
None
```
  • Loading branch information
JohnNiang authored and ruibaby committed Jun 6, 2024
1 parent 82e269a commit 3dc5156
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,20 @@ void concurrentComputeBundleFileIfAbsent() {

var probes = new ArrayList<PublisherProbe<DataBuffer>>();
List<? extends Future<?>> futures = IntStream.range(0, 10)
.mapToObj(i -> executorService.submit(() -> {
.mapToObj(i -> {
var fakeContent = Mono.<DataBuffer>just(sharedInstance.wrap(
("fake-content-" + i).getBytes(UTF_8)
));
var probe = PublisherProbe.of(fakeContent);
probes.add(probe);
cache.computeIfAbsent("fake-version", probe.mono())
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}))
return executorService.submit(
() -> {
cache.computeIfAbsent("fake-version", probe.mono())
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
});
})
.toList();
executorService.shutdown();
futures.forEach(future -> {
Expand Down

0 comments on commit 3dc5156

Please sign in to comment.