Skip to content

Commit

Permalink
Introduce SettableWatcher to make writing Watcher related integration…
Browse files Browse the repository at this point in the history
… test more easily
  • Loading branch information
imasahiro committed May 23, 2022
1 parent 8a32763 commit 7490497
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<T> implements Watcher<T> {
private volatile T value;
private final List<BiConsumer<? super Revision, ? super T>> updateListeners = new CopyOnWriteArrayList<>();

@Override
public ScheduledExecutorService watchScheduler() {
return CommonPools.blockingTaskExecutor();
}

@Override
public CompletableFuture<Latest<T>> initialValueFuture() {
return CompletableFuture.completedFuture(latest());
}

@Override
public Latest<T> 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<? super Revision, ? super T> l : updateListeners) {
l.accept(Revision.HEAD, value);
}
}
}

@Override
public void watch(BiConsumer<? super Revision, ? super T> listener) {
updateListeners.add(listener);
if (value != null) {
listener.accept(Revision.HEAD, value);
}
}

@Override
public void watch(BiConsumer<? super Revision, ? super T> listener, Executor executor) {
watch(listener);
}
}
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7490497

Please sign in to comment.