From 7490497ba677e465f385ed3e4b00cf6b13c029cf Mon Sep 17 00:00:00 2001 From: Masahiro Ide Date: Mon, 23 May 2022 09:41:13 +0900 Subject: [PATCH] Introduce SettableWatcher to make writing Watcher related integration test more easily --- .../centraldogma/testing/SettableWatcher.java | 76 +++++++++++++++++++ .../centraldogma/testing/package-info.java | 23 ++++++ 2 files changed, 99 insertions(+) create mode 100644 testing/common/src/main/java/com/linecorp/centraldogma/testing/SettableWatcher.java create mode 100644 testing/common/src/main/java/com/linecorp/centraldogma/testing/package-info.java diff --git a/testing/common/src/main/java/com/linecorp/centraldogma/testing/SettableWatcher.java b/testing/common/src/main/java/com/linecorp/centraldogma/testing/SettableWatcher.java new file mode 100644 index 0000000000..35e44da6e0 --- /dev/null +++ b/testing/common/src/main/java/com/linecorp/centraldogma/testing/SettableWatcher.java @@ -0,0 +1,76 @@ +/* + * Copyright 2022 LINE Corporation + * + * LINE Corporation licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.linecorp.centraldogma.testing; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.Executor; +import java.util.concurrent.ScheduledExecutorService; +import java.util.function.BiConsumer; + +import com.linecorp.armeria.common.CommonPools; +import com.linecorp.centraldogma.client.Latest; +import com.linecorp.centraldogma.client.Watcher; +import com.linecorp.centraldogma.common.Revision; + +public final class SettableWatcher implements Watcher { + private volatile T value; + private final List> updateListeners = new CopyOnWriteArrayList<>(); + + @Override + public ScheduledExecutorService watchScheduler() { + return CommonPools.blockingTaskExecutor(); + } + + @Override + public CompletableFuture> initialValueFuture() { + return CompletableFuture.completedFuture(latest()); + } + + @Override + public Latest latest() { + return new Latest<>(Revision.HEAD, value); + } + + @Override + public void close() { + updateListeners.clear(); + } + + public void setValue(T value) { + this.value = value; + if (this.value != null) { + for (BiConsumer l : updateListeners) { + l.accept(Revision.HEAD, value); + } + } + } + + @Override + public void watch(BiConsumer listener) { + updateListeners.add(listener); + if (value != null) { + listener.accept(Revision.HEAD, value); + } + } + + @Override + public void watch(BiConsumer listener, Executor executor) { + watch(listener); + } +} diff --git a/testing/common/src/main/java/com/linecorp/centraldogma/testing/package-info.java b/testing/common/src/main/java/com/linecorp/centraldogma/testing/package-info.java new file mode 100644 index 0000000000..6d0924a286 --- /dev/null +++ b/testing/common/src/main/java/com/linecorp/centraldogma/testing/package-info.java @@ -0,0 +1,23 @@ +/* + * Copyright 2022 LINE Corporation + * + * LINE Corporation licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Simplifies the integration testing with Central Dogma. + */ +@NonNullByDefault +package com.linecorp.centraldogma.testing; + +import com.linecorp.centraldogma.common.util.NonNullByDefault;