From 6e1f0f31a5d6166b0f580c7abba2844eab42ee35 Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Thu, 13 Jun 2024 18:00:36 +0800 Subject: [PATCH] [release-2.16] Fix the problem that bundle files are not changed in development mode (#6077) This is an automated cherry-pick of #6073 /assign guqing ```release-note None ``` --- .../service/impl/PluginServiceImpl.java | 16 ++++++++++++++++ .../service/impl/PluginServiceImplTest.java | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java b/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java index 89ec3e1d5..e891887f5 100644 --- a/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java +++ b/application/src/main/java/run/halo/app/core/extension/service/impl/PluginServiceImpl.java @@ -14,6 +14,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Clock; import java.time.Duration; import java.util.ArrayList; import java.util.Comparator; @@ -90,6 +91,8 @@ public class PluginServiceImpl implements PluginService, InitializingBean, Dispo private final Scheduler scheduler = Schedulers.boundedElastic(); + private Clock clock = Clock.systemUTC(); + public PluginServiceImpl(ReactiveExtensionClient client, SystemVersionSupplier systemVersion, PluginProperties pluginProperties, SpringPluginManager pluginManager) { this.client = client; @@ -101,6 +104,16 @@ public class PluginServiceImpl implements PluginService, InitializingBean, Dispo this.cssBundleCache = new BundleCache(".css"); } + /** + * The method is only for testing. + * + * @param clock new clock + */ + void setClock(Clock clock) { + Assert.notNull(clock, "Clock must not be null"); + this.clock = clock; + } + @Override public Flux getPresets() { // list presets from classpath @@ -269,6 +282,9 @@ public class PluginServiceImpl implements PluginService, InitializingBean, Dispo @Override public Mono generateBundleVersion() { + if (pluginManager.isDevelopment()) { + return Mono.just(String.valueOf(clock.instant().toEpochMilli())); + } return Flux.fromIterable(new ArrayList<>(pluginManager.getStartedPlugins())) .sort(Comparator.comparing(PluginWrapper::getPluginId)) .map(pw -> pw.getPluginId() + ':' + pw.getDescriptor().getVersion()) diff --git a/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java b/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java index 532dfdd43..8d34e55d6 100644 --- a/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java +++ b/application/src/test/java/run/halo/app/core/extension/service/impl/PluginServiceImplTest.java @@ -23,6 +23,9 @@ import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; @@ -249,7 +252,6 @@ class PluginServiceImplTest { } - @Test void generateBundleVersionTest() { var plugin1 = mock(PluginWrapper.class); @@ -297,6 +299,19 @@ class PluginServiceImplTest { assertThat(result).isNotEqualTo(result2); } + @Test + void shouldGenerateRandomBundleVersionInDevelopment() { + var clock = Clock.fixed(Instant.now(), ZoneId.systemDefault()); + pluginService.setClock(clock); + when(pluginManager.isDevelopment()).thenReturn(true); + pluginService.generateBundleVersion() + .as(StepVerifier::create) + .expectNext(String.valueOf(clock.instant().toEpochMilli())) + .verifyComplete(); + + verify(pluginManager, never()).getStartedPlugins(); + } + @Nested class PluginStateChangeTest {