From 68d94f6653172585bf349d9f466cd765ed50152e Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:16:54 +0800 Subject: [PATCH] refactor: optimize the extension watch parameters to always be of real type (#6180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.17.x #### What this PR does / why we need it: 优化触发 Extension Watch 方法(onAdd/onUpdate/onDelete)时的参数始终为真实类型避免使用时进行类型转换 #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../ReactiveExtensionClientImpl.java | 8 ++-- .../ReactiveExtensionClientTest.java | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java index ad6077492..822a13847 100644 --- a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java +++ b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java @@ -228,7 +228,7 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient { }) .map(converter::convertTo) .flatMap(extStore -> doCreate(extension, extStore.getName(), extStore.getData()) - .doOnNext(watchers::onAdd) + .doOnNext(created -> watchers.onAdd(convertToRealExtension(created))) ) .retryWhen(Retry.backoff(3, Duration.ofMillis(100)) // retry when generateName is set @@ -266,7 +266,9 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient { var store = this.converter.convertTo(newJsonExt); var updated = doUpdate(extension, store.getName(), store.getVersion(), store.getData()); if (!onlyStatusChanged) { - updated = updated.doOnNext(ext -> watchers.onUpdate(old, ext)); + updated = updated.doOnNext(ext -> watchers.onUpdate(convertToRealExtension(old), + convertToRealExtension(ext)) + ); } return updated; }); @@ -293,7 +295,7 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient { var extensionStore = converter.convertTo(extension); return doUpdate(extension, extensionStore.getName(), extensionStore.getVersion(), extensionStore.getData() - ).doOnNext(watchers::onDelete); + ).doOnNext(updated -> watchers.onDelete(convertToRealExtension(extension))); } @Override diff --git a/application/src/test/java/run/halo/app/extension/ReactiveExtensionClientTest.java b/application/src/test/java/run/halo/app/extension/ReactiveExtensionClientTest.java index 892184c28..3363dae14 100644 --- a/application/src/test/java/run/halo/app/extension/ReactiveExtensionClientTest.java +++ b/application/src/test/java/run/halo/app/extension/ReactiveExtensionClientTest.java @@ -688,6 +688,47 @@ class ReactiveExtensionClientTest { verify(watcher, times(1)).onDelete(any()); } + + @Test + void shouldWatchRealType() { + var extensionStore = createExtensionStore("/registry/fake.halo.run/fakes/fake"); + var fake = createFakeExtension("fake", 1L); + var unstructured = Unstructured.OBJECT_MAPPER.convertValue(fake, Unstructured.class); + + when(storeClient.fetchByName(extensionStore.getName())) + .thenReturn(Mono.just(extensionStore)); + when(converter.convertTo(any())).thenReturn(extensionStore); + when(converter.convertFrom(same(Unstructured.class), any())).thenReturn(unstructured); + + var indexer = mock(Indexer.class); + when(indexerFactory.getIndexer(eq(fake.groupVersionKind()))).thenReturn(indexer); + + // on add + when(storeClient.create(any(), any())).thenReturn(Mono.just(extensionStore)); + doNothing().when(watcher).onAdd(any(Extension.class)); + StepVerifier.create(client.create(unstructured)) + .expectNext(unstructured) + .verifyComplete(); + verify(watcher, times(1)).onAdd(isA(FakeExtension.class)); + + // on update + when(storeClient.update(any(), any(), any())).thenReturn(Mono.just(extensionStore)); + + doNothing().when(watcher).onUpdate(any(), any()); + StepVerifier.create(client.update(unstructured)) + .expectNext(unstructured) + .verifyComplete(); + verify(watcher, times(1)) + .onUpdate(isA(FakeExtension.class), isA(FakeExtension.class)); + + // on delete + doNothing().when(watcher).onDelete(any()); + StepVerifier.create(client.delete(unstructured)) + .expectNext(unstructured) + .verifyComplete(); + verify(watcher, times(1)).onDelete(isA(FakeExtension.class)); + + } } } \ No newline at end of file