Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-2.9] fix: bundle resources cache version may duplicate #4626

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}