DynamicSet: Store names of plugins who bound the items

For the items in DynamicItem and DynamicMap we are storing the name of
the plugin which has bound the item. Do the same for items in DynamicSet
so that we can provide items from DynamicSet by plugin.

Being able to get the name of the plugin that has bound the item is
useful for logging and debugging.

Change-Id: I990afdc117e18f8544ca376788ce0f35bb1c2ec5
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-08-24 15:49:36 +02:00
parent 5b337c20e8
commit 812635aae2
28 changed files with 197 additions and 64 deletions

View File

@@ -63,6 +63,7 @@ public class EventRecorder {
eventListenerRegistration = eventListenerRegistration =
eventListeners.add( eventListeners.add(
"gerrit",
new UserScopedEventListener() { new UserScopedEventListener() {
@Override @Override
public void onEvent(Event e) { public void onEvent(Event e) {

View File

@@ -14,6 +14,12 @@
package com.google.gerrit.extensions.registration; package com.google.gerrit.extensions.registration;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static java.util.Comparator.naturalOrder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.inject.Binder; import com.google.inject.Binder;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.Provider; import com.google.inject.Provider;
@@ -39,6 +45,24 @@ import java.util.concurrent.atomic.AtomicReference;
* singleton and non-singleton members. * singleton and non-singleton members.
*/ */
public class DynamicSet<T> implements Iterable<T> { public class DynamicSet<T> implements Iterable<T> {
public static class Entry<T> {
private final String pluginName;
private final Provider<T> provider;
private Entry(String pluginName, Provider<T> provider) {
this.pluginName = pluginName;
this.provider = provider;
}
public String getPluginName() {
return pluginName;
}
public Provider<T> getProvider() {
return provider;
}
}
/** /**
* Declare a singleton {@code DynamicSet<T>} with a binder. * Declare a singleton {@code DynamicSet<T>} with a binder.
* *
@@ -129,12 +153,12 @@ public class DynamicSet<T> implements Iterable<T> {
} }
public static <T> DynamicSet<T> emptySet() { public static <T> DynamicSet<T> emptySet() {
return new DynamicSet<>(Collections.<AtomicReference<Provider<T>>>emptySet()); return new DynamicSet<>(Collections.<AtomicReference<NamedProvider<T>>>emptySet());
} }
private final CopyOnWriteArrayList<AtomicReference<Provider<T>>> items; private final CopyOnWriteArrayList<AtomicReference<NamedProvider<T>>> items;
DynamicSet(Collection<AtomicReference<Provider<T>>> base) { DynamicSet(Collection<AtomicReference<NamedProvider<T>>> base) {
items = new CopyOnWriteArrayList<>(base); items = new CopyOnWriteArrayList<>(base);
} }
@@ -144,17 +168,33 @@ public class DynamicSet<T> implements Iterable<T> {
@Override @Override
public Iterator<T> iterator() { public Iterator<T> iterator() {
final Iterator<AtomicReference<Provider<T>>> itr = items.iterator(); Iterator<Entry<T>> entryIterator = entryIterator();
return new Iterator<T>() { return new Iterator<T>() {
private T next; @Override
public boolean hasNext() {
return entryIterator.hasNext();
}
@Override
public T next() {
Entry<T> next = entryIterator.next();
return next != null ? next.getProvider().get() : null;
}
};
}
public Iterator<Entry<T>> entryIterator() {
final Iterator<AtomicReference<NamedProvider<T>>> itr = items.iterator();
return new Iterator<Entry<T>>() {
private Entry<T> next;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
while (next == null && itr.hasNext()) { while (next == null && itr.hasNext()) {
Provider<T> p = itr.next().get(); NamedProvider<T> p = itr.next().get();
if (p != null) { if (p != null) {
try { try {
next = p.get(); next = new Entry<>(p.pluginName, p.impl);
} catch (RuntimeException e) { } catch (RuntimeException e) {
// TODO Log failed member of DynamicSet. // TODO Log failed member of DynamicSet.
} }
@@ -164,9 +204,9 @@ public class DynamicSet<T> implements Iterable<T> {
} }
@Override @Override
public T next() { public Entry<T> next() {
if (hasNext()) { if (hasNext()) {
T result = next; Entry<T> result = next;
next = null; next = null;
return result; return result;
} }
@@ -198,13 +238,29 @@ public class DynamicSet<T> implements Iterable<T> {
} }
/** /**
* Add one new element to the set. * Get the names of all running plugins supplying this type.
* *
* @param item the item to add to the collection. Must not be null. * @return sorted set of active plugins that supply at least one item.
* @return handle to remove the item at a later point in time.
*/ */
public RegistrationHandle add(T item) { public ImmutableSortedSet<String> plugins() {
return add(Providers.of(item)); return items
.stream()
.map(i -> i.get().pluginName)
.collect(toImmutableSortedSet(naturalOrder()));
}
/**
* Get the items exported by a single plugin.
*
* @param pluginName name of the plugin.
* @return items exported by a plugin.
*/
public ImmutableSet<Provider<T>> byPlugin(String pluginName) {
return items
.stream()
.filter(i -> i.get().pluginName.equals(pluginName))
.map(i -> i.get().impl)
.collect(toImmutableSet());
} }
/** /**
@@ -213,13 +269,24 @@ public class DynamicSet<T> implements Iterable<T> {
* @param item the item to add to the collection. Must not be null. * @param item the item to add to the collection. Must not be null.
* @return handle to remove the item at a later point in time. * @return handle to remove the item at a later point in time.
*/ */
public RegistrationHandle add(Provider<T> item) { public RegistrationHandle add(String pluginName, T item) {
final AtomicReference<Provider<T>> ref = new AtomicReference<>(item); return add(pluginName, Providers.of(item));
}
/**
* Add one new element to the set.
*
* @param item the item to add to the collection. Must not be null.
* @return handle to remove the item at a later point in time.
*/
public RegistrationHandle add(String pluginName, Provider<T> item) {
final AtomicReference<NamedProvider<T>> ref =
new AtomicReference<>(new NamedProvider<>(item, pluginName));
items.add(ref); items.add(ref);
return new RegistrationHandle() { return new RegistrationHandle() {
@Override @Override
public void remove() { public void remove() {
if (ref.compareAndSet(item, null)) { if (ref.compareAndSet(ref.get(), null)) {
items.remove(ref); items.remove(ref);
} }
} }
@@ -229,6 +296,7 @@ public class DynamicSet<T> implements Iterable<T> {
/** /**
* Add one new element that may be hot-replaceable in the future. * Add one new element that may be hot-replaceable in the future.
* *
* @param pluginName unique name of the plugin providing the item.
* @param key unique description from the item's Guice binding. This can be later obtained from * @param key unique description from the item's Guice binding. This can be later obtained from
* the registration handle to facilitate matching with the new equivalent instance during a * the registration handle to facilitate matching with the new equivalent instance during a
* hot reload. * hot reload.
@@ -236,18 +304,19 @@ public class DynamicSet<T> implements Iterable<T> {
* @return a handle that can remove this item later, or hot-swap the item without it ever leaving * @return a handle that can remove this item later, or hot-swap the item without it ever leaving
* the collection. * the collection.
*/ */
public ReloadableRegistrationHandle<T> add(Key<T> key, Provider<T> item) { public ReloadableRegistrationHandle<T> add(String pluginName, Key<T> key, Provider<T> item) {
AtomicReference<Provider<T>> ref = new AtomicReference<>(item); AtomicReference<NamedProvider<T>> ref =
new AtomicReference<>(new NamedProvider<>(item, pluginName));
items.add(ref); items.add(ref);
return new ReloadableHandle(ref, key, item); return new ReloadableHandle(ref, key, ref.get());
} }
private class ReloadableHandle implements ReloadableRegistrationHandle<T> { private class ReloadableHandle implements ReloadableRegistrationHandle<T> {
private final AtomicReference<Provider<T>> ref; private final AtomicReference<NamedProvider<T>> ref;
private final Key<T> key; private final Key<T> key;
private final Provider<T> item; private final NamedProvider<T> item;
ReloadableHandle(AtomicReference<Provider<T>> ref, Key<T> key, Provider<T> item) { ReloadableHandle(AtomicReference<NamedProvider<T>> ref, Key<T> key, NamedProvider<T> item) {
this.ref = ref; this.ref = ref;
this.key = key; this.key = key;
this.item = item; this.item = item;
@@ -267,8 +336,9 @@ public class DynamicSet<T> implements Iterable<T> {
@Override @Override
public ReloadableHandle replace(Key<T> newKey, Provider<T> newItem) { public ReloadableHandle replace(Key<T> newKey, Provider<T> newItem) {
if (ref.compareAndSet(item, newItem)) { NamedProvider<T> n = new NamedProvider<>(newItem, item.pluginName);
return new ReloadableHandle(ref, newKey, newItem); if (ref.compareAndSet(item, n)) {
return new ReloadableHandle(ref, newKey, n);
} }
return null; return null;
} }

View File

@@ -38,16 +38,17 @@ class DynamicSetProvider<T> implements Provider<DynamicSet<T>> {
return new DynamicSet<>(find(injector, type)); return new DynamicSet<>(find(injector, type));
} }
private static <T> List<AtomicReference<Provider<T>>> find(Injector src, TypeLiteral<T> type) { private static <T> List<AtomicReference<NamedProvider<T>>> find(
Injector src, TypeLiteral<T> type) {
List<Binding<T>> bindings = src.findBindingsByType(type); List<Binding<T>> bindings = src.findBindingsByType(type);
int cnt = bindings != null ? bindings.size() : 0; int cnt = bindings != null ? bindings.size() : 0;
if (cnt == 0) { if (cnt == 0) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<AtomicReference<Provider<T>>> r = new ArrayList<>(cnt); List<AtomicReference<NamedProvider<T>>> r = new ArrayList<>(cnt);
for (Binding<T> b : bindings) { for (Binding<T> b : bindings) {
if (b.getKey().getAnnotation() != null) { if (b.getKey().getAnnotation() != null) {
r.add(new AtomicReference<>(b.getProvider())); r.add(new AtomicReference<>(new NamedProvider<>(b.getProvider(), PluginName.GERRIT)));
} }
} }
return r; return r;

View File

@@ -107,7 +107,7 @@ public class PrivateInternals_DynamicTypes {
} }
public static List<RegistrationHandle> attachSets( public static List<RegistrationHandle> attachSets(
Injector src, Map<TypeLiteral<?>, DynamicSet<?>> sets) { Injector src, String pluginName, Map<TypeLiteral<?>, DynamicSet<?>> sets) {
if (src == null || sets == null || sets.isEmpty()) { if (src == null || sets == null || sets.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
@@ -123,7 +123,7 @@ public class PrivateInternals_DynamicTypes {
for (Binding<Object> b : bindings(src, type)) { for (Binding<Object> b : bindings(src, type)) {
if (b.getKey().getAnnotation() != null) { if (b.getKey().getAnnotation() != null) {
handles.add(set.add(b.getKey(), b.getProvider())); handles.add(set.add(pluginName, b.getKey(), b.getProvider()));
} }
} }
} }
@@ -174,7 +174,7 @@ public class PrivateInternals_DynamicTypes {
handles = new ArrayList<>(4); handles = new ArrayList<>(4);
Injector parent = self.getParent(); Injector parent = self.getParent();
while (parent != null) { while (parent != null) {
handles.addAll(attachSets(self, dynamicSetsOf(parent))); handles.addAll(attachSets(self, PluginName.GERRIT, dynamicSetsOf(parent)));
handles.addAll(attachMaps(self, PluginName.GERRIT, dynamicMapsOf(parent))); handles.addAll(attachMaps(self, PluginName.GERRIT, dynamicMapsOf(parent)));
parent = parent.getParent(); parent = parent.getParent();
} }

View File

@@ -76,7 +76,7 @@ public abstract class AllRequestFilter implements Filter {
// synchronized. // synchronized.
if (!initializedFilters.contains(filter)) { if (!initializedFilters.contains(filter)) {
filter.init(filterConfig); filter.init(filterConfig);
initializedFilters.add(filter); initializedFilters.add("gerrit", filter);
} }
} else { } else {
ret = false; ret = false;
@@ -89,7 +89,7 @@ public abstract class AllRequestFilter implements Filter {
initializedFilters = new DynamicSet<>(); initializedFilters = new DynamicSet<>();
for (AllRequestFilter filter : filtersToCleanUp) { for (AllRequestFilter filter : filtersToCleanUp) {
if (filters.contains(filter)) { if (filters.contains(filter)) {
initializedFilters.add(filter); initializedFilters.add("gerrit", filter);
} else { } else {
filter.destroy(); filter.destroy();
} }

View File

@@ -281,7 +281,8 @@ public class PluginGuiceEnvironment {
private void attachSet( private void attachSet(
Map<TypeLiteral<?>, DynamicSet<?>> sets, @Nullable Injector src, Plugin plugin) { Map<TypeLiteral<?>, DynamicSet<?>> sets, @Nullable Injector src, Plugin plugin) {
for (RegistrationHandle h : PrivateInternals_DynamicTypes.attachSets(src, sets)) { for (RegistrationHandle h :
PrivateInternals_DynamicTypes.attachSets(src, plugin.getName(), sets)) {
plugin.add(h); plugin.add(h);
} }
} }
@@ -434,7 +435,7 @@ public class PluginGuiceEnvironment {
oi.remove(); oi.remove();
replace(newPlugin, h2, b); replace(newPlugin, h2, b);
} else { } else {
newPlugin.add(set.add(b.getKey(), b.getProvider())); newPlugin.add(set.add(newPlugin.getName(), b.getKey(), b.getProvider()));
} }
} }
} }

View File

@@ -151,6 +151,7 @@ final class StreamEvents extends BaseCommand {
stdout = toPrintWriter(out); stdout = toPrintWriter(out);
eventListenerRegistration = eventListenerRegistration =
eventListeners.add( eventListeners.add(
"gerrit",
new UserScopedEventListener() { new UserScopedEventListener() {
@Override @Override
public void onEvent(Event event) { public void onEvent(Event event) {

View File

@@ -39,7 +39,7 @@ public class TestGroupBackendTest extends AbstractDaemonTest {
@Test @Test
public void universalGroupBackendHandlesTestGroup() throws Exception { public void universalGroupBackendHandlesTestGroup() throws Exception {
RegistrationHandle registrationHandle = groupBackends.add(testGroupBackend); RegistrationHandle registrationHandle = groupBackends.add("gerrit", testGroupBackend);
try { try {
assertThat(universalGroupBackend.handles(testUUID)).isTrue(); assertThat(universalGroupBackend.handles(testUUID)).isTrue();
} finally { } finally {

View File

@@ -230,7 +230,7 @@ public class AccountIT extends AbstractDaemonTest {
@Before @Before
public void addAccountIndexEventCounter() { public void addAccountIndexEventCounter() {
accountIndexedCounter = new AccountIndexedCounter(); accountIndexedCounter = new AccountIndexedCounter();
accountIndexEventCounterHandle = accountIndexedListeners.add(accountIndexedCounter); accountIndexEventCounterHandle = accountIndexedListeners.add("gerrit", accountIndexedCounter);
} }
@After @After
@@ -243,7 +243,7 @@ public class AccountIT extends AbstractDaemonTest {
@Before @Before
public void addRefUpdateCounter() { public void addRefUpdateCounter() {
refUpdateCounter = new RefUpdateCounter(); refUpdateCounter = new RefUpdateCounter();
refUpdateCounterHandle = refUpdateListeners.add(refUpdateCounter); refUpdateCounterHandle = refUpdateListeners.add("gerrit", refUpdateCounter);
} }
@After @After
@@ -532,6 +532,7 @@ public class AccountIT extends AbstractDaemonTest {
accountOperations.newAccount().preferredEmail("foo@deactivatable.com").create(); accountOperations.newAccount().preferredEmail("foo@deactivatable.com").create();
RegistrationHandle registrationHandle = RegistrationHandle registrationHandle =
accountActivationValidationListeners.add( accountActivationValidationListeners.add(
"gerrit",
new AccountActivationValidationListener() { new AccountActivationValidationListener() {
@Override @Override
public void validateActivation(AccountState account) throws ValidationException { public void validateActivation(AccountState account) throws ValidationException {

View File

@@ -203,7 +203,7 @@ public class ChangeIT extends AbstractDaemonTest {
@Before @Before
public void addChangeIndexedCounter() { public void addChangeIndexedCounter() {
changeIndexedCounter = new ChangeIndexedCounter(); changeIndexedCounter = new ChangeIndexedCounter();
changeIndexedCounterHandle = changeIndexedListeners.add(changeIndexedCounter); changeIndexedCounterHandle = changeIndexedListeners.add("gerrit", changeIndexedCounter);
} }
@After @After
@@ -2599,6 +2599,7 @@ public class ChangeIT extends AbstractDaemonTest {
PushOneCommit.Result change = createChange(); PushOneCommit.Result change = createChange();
RegistrationHandle handle = RegistrationHandle handle =
changeMessageModifiers.add( changeMessageModifiers.add(
"gerrit",
new ChangeMessageModifier() { new ChangeMessageModifier() {
@Override @Override
public String onSubmit( public String onSubmit(

View File

@@ -1298,7 +1298,7 @@ public class GroupsIT extends AbstractDaemonTest {
GroupIndexedCounter groupIndexedCounter = new GroupIndexedCounter(); GroupIndexedCounter groupIndexedCounter = new GroupIndexedCounter();
RegistrationHandle groupIndexEventCounterHandle = RegistrationHandle groupIndexEventCounterHandle =
groupIndexedListeners.add(groupIndexedCounter); groupIndexedListeners.add("gerrit", groupIndexedCounter);
try { try {
// Running the reindexer right after startup should not need to reindex any group since // Running the reindexer right after startup should not need to reindex any group since
// reindexing was already done on startup. // reindexing was already done on startup.
@@ -1355,7 +1355,7 @@ public class GroupsIT extends AbstractDaemonTest {
GroupIndexedCounter groupIndexedCounter = new GroupIndexedCounter(); GroupIndexedCounter groupIndexedCounter = new GroupIndexedCounter();
RegistrationHandle groupIndexEventCounterHandle = RegistrationHandle groupIndexEventCounterHandle =
groupIndexedListeners.add(groupIndexedCounter); groupIndexedListeners.add("gerrit", groupIndexedCounter);
try { try {
// No group indexing happened on startup. All groups should be reindexed now. // No group indexing happened on startup. All groups should be reindexed now.
slaveGroupIndexer.run(); slaveGroupIndexer.run();

View File

@@ -68,7 +68,7 @@ public class ProjectIT extends AbstractDaemonTest {
@Before @Before
public void addProjectIndexedCounter() { public void addProjectIndexedCounter() {
projectIndexedCounter = new ProjectIndexedCounter(); projectIndexedCounter = new ProjectIndexedCounter();
projectIndexedCounterHandle = projectIndexedListeners.add(projectIndexedCounter); projectIndexedCounterHandle = projectIndexedListeners.add("gerrit", projectIndexedCounter);
} }
@After @After

View File

@@ -920,6 +920,7 @@ public class RevisionIT extends AbstractDaemonTest {
CountDownLatch reindexed = new CountDownLatch(1); CountDownLatch reindexed = new CountDownLatch(1);
RegistrationHandle handle = RegistrationHandle handle =
changeIndexedListeners.add( changeIndexedListeners.add(
"gerrit",
new ChangeIndexedListener() { new ChangeIndexedListener() {
@Override @Override
public void onChangeIndexed(String projectName, int id) { public void onChangeIndexed(String projectName, int id) {
@@ -1086,6 +1087,7 @@ public class RevisionIT extends AbstractDaemonTest {
WebLinkInfo expectedWebLinkInfo = new WebLinkInfo("foo", "imageUrl", "url"); WebLinkInfo expectedWebLinkInfo = new WebLinkInfo("foo", "imageUrl", "url");
RegistrationHandle handle = RegistrationHandle handle =
patchSetLinks.add( patchSetLinks.add(
"gerrit",
new PatchSetWebLink() { new PatchSetWebLink() {
@Override @Override
public WebLinkInfo getPatchSetWebLink(String projectName, String commit) { public WebLinkInfo getPatchSetWebLink(String projectName, String commit) {

View File

@@ -52,9 +52,10 @@ public class TraceIT extends AbstractDaemonTest {
public void setup() { public void setup() {
projectCreationListener = new TraceValidatingProjectCreationValidationListener(); projectCreationListener = new TraceValidatingProjectCreationValidationListener();
projectCreationListenerRegistrationHandle = projectCreationListenerRegistrationHandle =
projectCreationValidationListeners.add(projectCreationListener); projectCreationValidationListeners.add("gerrit", projectCreationListener);
commitValidationListener = new TraceValidatingCommitValidationListener(); commitValidationListener = new TraceValidatingCommitValidationListener();
commitValidationRegistrationHandle = commitValidationListeners.add(commitValidationListener); commitValidationRegistrationHandle =
commitValidationListeners.add("gerrit", commitValidationListener);
} }
@After @After

View File

@@ -1324,7 +1324,7 @@ public abstract class AbstractSubmit extends AbstractDaemonTest {
protected void addOnSubmitValidationListener(OnSubmitValidationListener listener) { protected void addOnSubmitValidationListener(OnSubmitValidationListener listener) {
assertThat(onSubmitValidatorHandle).isNull(); assertThat(onSubmitValidatorHandle).isNull();
onSubmitValidatorHandle = onSubmitValidationListeners.add(listener); onSubmitValidatorHandle = onSubmitValidationListeners.add("gerrit", listener);
} }
private String getLatestDiff(Repository repo) throws Exception { private String getLatestDiff(Repository repo) throws Exception {

View File

@@ -331,7 +331,7 @@ public class ActionsIT extends AbstractDaemonTest {
assertThat(origActions.get("abandon").label).isEqualTo("Abandon"); assertThat(origActions.get("abandon").label).isEqualTo("Abandon");
Visitor v = new Visitor(); Visitor v = new Visitor();
visitorHandle = actionVisitors.add(v); visitorHandle = actionVisitors.add("gerrit", v);
Map<String, ActionInfo> newActions = Map<String, ActionInfo> newActions =
gApi.changes().id(id).get(EnumSet.of(ListChangesOption.CHANGE_ACTIONS)).actions; gApi.changes().id(id).get(EnumSet.of(ListChangesOption.CHANGE_ACTIONS)).actions;
@@ -380,7 +380,7 @@ public class ActionsIT extends AbstractDaemonTest {
assertThat(origActions.get("rebase").label).isEqualTo("Rebase"); assertThat(origActions.get("rebase").label).isEqualTo("Rebase");
Visitor v = new Visitor(); Visitor v = new Visitor();
visitorHandle = actionVisitors.add(v); visitorHandle = actionVisitors.add("gerrit", v);
// Test different codepaths within ActionJson... // Test different codepaths within ActionJson...
// ...via revision API. // ...via revision API.
@@ -443,7 +443,7 @@ public class ActionsIT extends AbstractDaemonTest {
assertThat(origActions.get("description").label).isEqualTo("Edit Description"); assertThat(origActions.get("description").label).isEqualTo("Edit Description");
Visitor v = new Visitor(); Visitor v = new Visitor();
visitorHandle = actionVisitors.add(v); visitorHandle = actionVisitors.add("gerrit", v);
// Unlike for the current revision, actions for old revisions are only available via the // Unlike for the current revision, actions for old revisions are only available via the
// revision API. // revision API.

View File

@@ -97,6 +97,7 @@ public class SubmitByCherryPickIT extends AbstractSubmit {
PushOneCommit.Result change = createChange(); PushOneCommit.Result change = createChange();
RegistrationHandle handle = RegistrationHandle handle =
changeMessageModifiers.add( changeMessageModifiers.add(
"gerrit",
new ChangeMessageModifier() { new ChangeMessageModifier() {
@Override @Override
public String onSubmit( public String onSubmit(

View File

@@ -87,6 +87,7 @@ public class SubmitByRebaseAlwaysIT extends AbstractSubmitByRebase {
RegistrationHandle handle = RegistrationHandle handle =
changeMessageModifiers.add( changeMessageModifiers.add(
"gerrit",
new ChangeMessageModifier() { new ChangeMessageModifier() {
@Override @Override
public String onSubmit( public String onSubmit(

View File

@@ -89,6 +89,7 @@ public class AccessIT extends AbstractDaemonTest {
public void webLink() throws Exception { public void webLink() throws Exception {
RegistrationHandle handle = RegistrationHandle handle =
fileHistoryWebLinkDynamicSet.add( fileHistoryWebLinkDynamicSet.add(
"gerrit",
new FileHistoryWebLink() { new FileHistoryWebLink() {
@Override @Override
public WebLinkInfo getFileHistoryWebLink( public WebLinkInfo getFileHistoryWebLink(
@@ -111,6 +112,7 @@ public class AccessIT extends AbstractDaemonTest {
public void webLinkNoRefsMetaConfig() throws Exception { public void webLinkNoRefsMetaConfig() throws Exception {
RegistrationHandle handle = RegistrationHandle handle =
fileHistoryWebLinkDynamicSet.add( fileHistoryWebLinkDynamicSet.add(
"gerrit",
new FileHistoryWebLink() { new FileHistoryWebLink() {
@Override @Override
public WebLinkInfo getFileHistoryWebLink( public WebLinkInfo getFileHistoryWebLink(

View File

@@ -76,6 +76,7 @@ public class CommentAddedEventIT extends AbstractDaemonTest {
eventListenerRegistration = eventListenerRegistration =
source.add( source.add(
"gerrit",
new CommentAddedListener() { new CommentAddedListener() {
@Override @Override
public void onCommentAdded(Event event) { public void onCommentAdded(Event event) {

View File

@@ -610,7 +610,7 @@ public class OnlineNoteDbMigrationIT extends AbstractDaemonTest {
} }
private void addListener(NotesMigrationStateListener listener) { private void addListener(NotesMigrationStateListener listener) {
addedListeners.add(listeners.add(listener)); addedListeners.add(listeners.add("gerrit", listener));
} }
private ImmutableSortedSet<String> getObjectFiles(Project.NameKey project) throws Exception { private ImmutableSortedSet<String> getObjectFiles(Project.NameKey project) throws Exception {

View File

@@ -82,6 +82,7 @@ public class CustomLabelIT extends AbstractDaemonTest {
eventListenerRegistration = eventListenerRegistration =
source.add( source.add(
"gerrit",
new CommentAddedListener() { new CommentAddedListener() {
@Override @Override
public void onCommentAdded(Event event) { public void onCommentAdded(Event event) {

View File

@@ -50,7 +50,7 @@ public abstract class AbstractIndexTests extends AbstractDaemonTest {
@Before @Before
public void addChangeIndexedCounter() { public void addChangeIndexedCounter() {
changeIndexedCounter = new ChangeIndexedCounter(); changeIndexedCounter = new ChangeIndexedCounter();
changeIndexedCounterHandle = changeIndexedListeners.add(changeIndexedCounter); changeIndexedCounterHandle = changeIndexedListeners.add("gerrit", changeIndexedCounter);
} }
@After @After

View File

@@ -27,7 +27,7 @@ public class SshTraceIT extends AbstractDaemonTest {
public void setup() { public void setup() {
projectCreationListener = new TraceValidatingProjectCreationValidationListener(); projectCreationListener = new TraceValidatingProjectCreationValidationListener();
projectCreationListenerRegistrationHandle = projectCreationListenerRegistrationHandle =
projectCreationValidationListeners.add(projectCreationListener); projectCreationValidationListeners.add("gerrit", projectCreationListener);
} }
@After @After

View File

@@ -15,9 +15,12 @@
package com.google.gerrit.extensions.registration; package com.google.gerrit.extensions.registration;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.stream.Collectors.toSet;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.util.Providers; import com.google.inject.util.Providers;
import java.util.Iterator;
import org.junit.Test; import org.junit.Test;
public class DynamicSetTest { public class DynamicSetTest {
@@ -40,7 +43,7 @@ public class DynamicSetTest {
@Test @Test
public void containsTrueWithSingleElement() throws Exception { public void containsTrueWithSingleElement() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>(); DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2); ds.add("gerrit", 2);
assertThat(ds.contains(2)).isTrue(); // See above comment about ds.contains assertThat(ds.contains(2)).isTrue(); // See above comment about ds.contains
} }
@@ -48,7 +51,7 @@ public class DynamicSetTest {
@Test @Test
public void containsFalseWithSingleElement() throws Exception { public void containsFalseWithSingleElement() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>(); DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2); ds.add("gerrit", 2);
assertThat(ds.contains(3)).isFalse(); // See above comment about ds.contains assertThat(ds.contains(3)).isFalse(); // See above comment about ds.contains
} }
@@ -56,8 +59,8 @@ public class DynamicSetTest {
@Test @Test
public void containsTrueWithTwoElements() throws Exception { public void containsTrueWithTwoElements() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>(); DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2); ds.add("gerrit", 2);
ds.add(4); ds.add("gerrit", 4);
assertThat(ds.contains(4)).isTrue(); // See above comment about ds.contains assertThat(ds.contains(4)).isTrue(); // See above comment about ds.contains
} }
@@ -65,8 +68,8 @@ public class DynamicSetTest {
@Test @Test
public void containsFalseWithTwoElements() throws Exception { public void containsFalseWithTwoElements() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>(); DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2); ds.add("gerrit", 2);
ds.add(4); ds.add("gerrit", 4);
assertThat(ds.contains(3)).isFalse(); // See above comment about ds.contains assertThat(ds.contains(3)).isFalse(); // See above comment about ds.contains
} }
@@ -74,12 +77,12 @@ public class DynamicSetTest {
@Test @Test
public void containsDynamic() throws Exception { public void containsDynamic() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>(); DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2); ds.add("gerrit", 2);
Key<Integer> key = Key.get(Integer.class); Key<Integer> key = Key.get(Integer.class);
ReloadableRegistrationHandle<Integer> handle = ds.add(key, Providers.of(4)); ReloadableRegistrationHandle<Integer> handle = ds.add("gerrit", key, Providers.of(4));
ds.add(6); ds.add("gerrit", 6);
// At first, 4 is contained. // At first, 4 is contained.
assertThat(ds.contains(4)).isTrue(); // See above comment about ds.contains assertThat(ds.contains(4)).isTrue(); // See above comment about ds.contains
@@ -90,4 +93,49 @@ public class DynamicSetTest {
// And now 4 should no longer be contained. // And now 4 should no longer be contained.
assertThat(ds.contains(4)).isFalse(); // See above comment about ds.contains assertThat(ds.contains(4)).isFalse(); // See above comment about ds.contains
} }
@Test
public void plugins() {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add("foo", 1);
ds.add("bar", 2);
ds.add("bar", 3);
assertThat(ds.plugins()).containsExactly("bar", "foo").inOrder();
}
@Test
public void byPlugin() {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add("foo", 1);
ds.add("bar", 2);
ds.add("bar", 3);
assertThat(ds.byPlugin("foo").stream().map(Provider::get).collect(toSet())).containsExactly(1);
assertThat(ds.byPlugin("bar").stream().map(Provider::get).collect(toSet()))
.containsExactly(2, 3);
}
@Test
public void entryIterator() {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add("foo", 1);
ds.add("bar", 2);
ds.add("bar", 3);
Iterator<DynamicSet.Entry<Integer>> entryIterator = ds.entryIterator();
DynamicSet.Entry<Integer> next = entryIterator.next();
assertThat(next.getPluginName()).isEqualTo("foo");
assertThat(next.getProvider().get()).isEqualTo(1);
next = entryIterator.next();
assertThat(next.getPluginName()).isEqualTo("bar");
assertThat(next.getProvider().get()).isEqualTo(2);
next = entryIterator.next();
assertThat(next.getPluginName()).isEqualTo("bar");
assertThat(next.getProvider().get()).isEqualTo(3);
assertThat(entryIterator.hasNext()).isFalse();
}
} }

View File

@@ -76,7 +76,7 @@ public class AllRequestFilterFilterProxyTest {
*/ */
private ReloadableRegistrationHandle<AllRequestFilter> addFilter(AllRequestFilter filter) { private ReloadableRegistrationHandle<AllRequestFilter> addFilter(AllRequestFilter filter) {
Key<AllRequestFilter> key = Key.get(AllRequestFilter.class); Key<AllRequestFilter> key = Key.get(AllRequestFilter.class);
return filters.add(key, Providers.of(filter)); return filters.add("gerrit", key, Providers.of(filter));
} }
@Test @Test

View File

@@ -55,7 +55,7 @@ public class UniversalGroupBackendTest extends GerritBaseTests {
user = createNiceMock(IdentifiedUser.class); user = createNiceMock(IdentifiedUser.class);
replay(user); replay(user);
backends = new DynamicSet<>(); backends = new DynamicSet<>();
backends.add(new SystemGroupBackend(new Config())); backends.add("gerrit", new SystemGroupBackend(new Config()));
backend = new UniversalGroupBackend(backends); backend = new UniversalGroupBackend(backends);
} }
@@ -123,7 +123,7 @@ public class UniversalGroupBackendTest extends GerritBaseTests {
replay(member, notMember, backend); replay(member, notMember, backend);
backends = new DynamicSet<>(); backends = new DynamicSet<>();
backends.add(backend); backends.add("gerrit", backend);
backend = new UniversalGroupBackend(backends); backend = new UniversalGroupBackend(backends);
GroupMembership checker = backend.membershipsOf(member); GroupMembership checker = backend.membershipsOf(member);

View File

@@ -586,7 +586,7 @@ public class Schema_166_to_167_WithGroupsInReviewDbTest {
AccountGroup group = createInReviewDb("group"); AccountGroup group = createInReviewDb("group");
TestGroupBackend testGroupBackend = new TestGroupBackend(); TestGroupBackend testGroupBackend = new TestGroupBackend();
backends.add(testGroupBackend); backends.add("gerrit", testGroupBackend);
AccountGroup.UUID subgroupUuid = testGroupBackend.create("test").getGroupUUID(); AccountGroup.UUID subgroupUuid = testGroupBackend.create("test").getGroupUUID();
assertThat(groupBackend.handles(subgroupUuid)).isTrue(); assertThat(groupBackend.handles(subgroupUuid)).isTrue();
addSubgroupsInReviewDb(group.getId(), subgroupUuid); addSubgroupsInReviewDb(group.getId(), subgroupUuid);