Skip to content

Commit

Permalink
fix: bundle resources cache version may duplicate (#4591)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.10.x

#### What this PR does / why we need it:
修复 bundle resource 的缓存 key 生成可能会重复的问题

#### Which issue(s) this PR fixes:
Fixes #4586

#### Does this PR introduce a user-facing change?
```release-note
修复 bundle resource 的缓存 key 生成可能会重复的问题
```
  • Loading branch information
guqing authored Sep 18, 2023
1 parent dd55a0f commit 84f413d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -186,10 +187,15 @@ public Mono<String> uglifyCssBundle() {
@Override
public Mono<String> generateJsBundleVersion() {
return Mono.fromSupplier(() -> {
if (RuntimeMode.DEVELOPMENT.equals(pluginManager.getRuntimeMode())) {
return String.valueOf(System.currentTimeMillis());
}
var compactVersion = pluginManager.getStartedPlugins()
.stream()
.sorted(Comparator.comparing(PluginWrapper::getPluginId))
.map(pluginWrapper -> pluginWrapper.getDescriptor().getVersion())
.map(pluginWrapper -> pluginWrapper.getPluginId() + ":"
+ pluginWrapper.getDescriptor().getVersion()
)
.collect(Collectors.joining());
return Hashing.sha256().hashUnencodedChars(compactVersion).toString();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import static org.mockito.Mockito.when;

import com.github.zafarkhaja.semver.Version;
import com.google.common.hash.Hashing;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand All @@ -27,8 +29,10 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginState;
import org.pf4j.PluginWrapper;
import org.pf4j.RuntimeMode;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand Down Expand Up @@ -226,4 +230,59 @@ void reload() {
);
verify(pluginWrapper, times(1)).getPluginPath();
}

@Test
void generateJsBundleVersionTest() {
when(pluginManager.getRuntimeMode()).thenReturn(RuntimeMode.DEVELOPMENT);

pluginService.generateJsBundleVersion()
.as(StepVerifier::create)
.consumeNextWith(version -> assertThat(version).isNotNull())
.verifyComplete();

when(pluginManager.getRuntimeMode()).thenReturn(RuntimeMode.DEPLOYMENT);
var plugin1 = mock(PluginWrapper.class);
var plugin2 = mock(PluginWrapper.class);
var plugin3 = mock(PluginWrapper.class);
when(pluginManager.getStartedPlugins()).thenReturn(List.of(plugin1, plugin2, plugin3));

var descriptor1 = mock(PluginDescriptor.class);
var descriptor2 = mock(PluginDescriptor.class);
var descriptor3 = mock(PluginDescriptor.class);
when(plugin1.getDescriptor()).thenReturn(descriptor1);
when(plugin2.getDescriptor()).thenReturn(descriptor2);
when(plugin3.getDescriptor()).thenReturn(descriptor3);

when(plugin1.getPluginId()).thenReturn("fake-1");
when(plugin2.getPluginId()).thenReturn("fake-2");
when(plugin3.getPluginId()).thenReturn("fake-3");

when(descriptor1.getVersion()).thenReturn("1.0.0");
when(descriptor2.getVersion()).thenReturn("2.0.0");
when(descriptor3.getVersion()).thenReturn("3.0.0");

var str = "fake-1:1.0.0fake-2:2.0.0fake-3:3.0.0";
var result = Hashing.sha256().hashUnencodedChars(str).toString();
assertThat(result.length()).isEqualTo(64);

pluginService.generateJsBundleVersion()
.as(StepVerifier::create)
.consumeNextWith(version -> assertThat(version).isEqualTo(result))
.verifyComplete();

var plugin4 = mock(PluginWrapper.class);
var descriptor4 = mock(PluginDescriptor.class);
when(plugin4.getDescriptor()).thenReturn(descriptor4);
when(plugin4.getPluginId()).thenReturn("fake-4");
when(descriptor4.getVersion()).thenReturn("3.0.0");
var str2 = "fake-1:1.0.0fake-2:2.0.0fake-4:3.0.0";
var result2 = Hashing.sha256().hashUnencodedChars(str2).toString();
when(pluginManager.getStartedPlugins()).thenReturn(List.of(plugin1, plugin2, plugin4));
pluginService.generateJsBundleVersion()
.as(StepVerifier::create)
.consumeNextWith(version -> assertThat(version).isEqualTo(result2))
.verifyComplete();

assertThat(result).isNotEqualTo(result2);
}
}

0 comments on commit 84f413d

Please sign in to comment.