Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions api/src/main/java/io/grpc/ConfiguratorRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
@GuardedBy("this")
private boolean wasConfiguratorsSet;
@GuardedBy("this")
private boolean configFrozen;
@GuardedBy("this")
private List<Configurator> configurators = Collections.emptyList();
@GuardedBy("this")
private int configuratorsCallCountBeforeSet = 0;

ConfiguratorRegistry() {}

Expand All @@ -56,22 +56,31 @@
* @throws IllegalStateException if this method is called more than once
*/
public synchronized void setConfigurators(List<? extends Configurator> configurators) {
if (configFrozen) {
if (wasConfiguratorsSet) {
throw new IllegalStateException("Configurators are already set");
}
this.configurators = Collections.unmodifiableList(new ArrayList<>(configurators));
configFrozen = true;
wasConfiguratorsSet = true;
}

/**
* Returns a list of the configurators in this registry.
*/
public synchronized List<Configurator> getConfigurators() {
configFrozen = true;
if (!wasConfiguratorsSet) {
configuratorsCallCountBeforeSet++;
}
return configurators;
}

/**
* Returns the number of times getConfigurators() was called before
* setConfigurators() was successfully invoked.
*/
public synchronized int getConfiguratorsCallCountBeforeSet() {
return configuratorsCallCountBeforeSet;

Check warning on line 81 in api/src/main/java/io/grpc/ConfiguratorRegistry.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/ConfiguratorRegistry.java#L81

Added line #L81 was not covered by tests
}

public synchronized boolean wasSetConfiguratorsCalled() {
return wasConfiguratorsSet;
}
Expand Down
4 changes: 4 additions & 0 deletions api/src/main/java/io/grpc/InternalConfiguratorRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@
public static boolean wasSetConfiguratorsCalled() {
return ConfiguratorRegistry.getDefaultRegistry().wasSetConfiguratorsCalled();
}

public static int getConfiguratorsCallCountBeforeSet() {
return ConfiguratorRegistry.getDefaultRegistry().getConfiguratorsCallCountBeforeSet();

Check warning on line 53 in api/src/main/java/io/grpc/InternalConfiguratorRegistry.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/io/grpc/InternalConfiguratorRegistry.java#L53

Added line #L53 was not covered by tests
}
}
14 changes: 6 additions & 8 deletions api/src/test/java/io/grpc/ConfiguratorRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ public static final class StaticTestingClassLoaderGetBeforeSet implements Runnab
@Override
public void run() {
assertThat(ConfiguratorRegistry.getDefaultRegistry().getConfigurators()).isEmpty();

try {
Comment thread
AgraVator marked this conversation as resolved.
ConfiguratorRegistry.getDefaultRegistry()
.setConfigurators(Arrays.asList(new NoopConfigurator()));
fail("should have failed for invoking set call after get is already called");
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().isEqualTo("Configurators are already set");
}
NoopConfigurator noopConfigurator = new NoopConfigurator();
ConfiguratorRegistry.getDefaultRegistry()
.setConfigurators(Arrays.asList(noopConfigurator));
assertThat(ConfiguratorRegistry.getDefaultRegistry().getConfigurators())
.containsExactly(noopConfigurator);
assertThat(InternalConfiguratorRegistry.getConfiguratorsCallCountBeforeSet()).isEqualTo(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,9 @@ public void run() {
List<ClientInterceptor> effectiveInterceptors =
builder.getEffectiveInterceptors("unused:///");
assertThat(effectiveInterceptors).hasSize(2);
try {
InternalConfiguratorRegistry.setConfigurators(Collections.emptyList());
fail("exception expected");
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("Configurators are already set");
}
InternalConfiguratorRegistry.setConfigurators(Collections.emptyList());
assertThat(InternalConfiguratorRegistry.getConfigurators()).isEmpty();
assertThat(InternalConfiguratorRegistry.getConfiguratorsCallCountBeforeSet()).isEqualTo(1);
}
}

Expand Down
10 changes: 3 additions & 7 deletions core/src/test/java/io/grpc/internal/ServerImplBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import io.grpc.InternalConfigurator;
import io.grpc.InternalConfiguratorRegistry;
Expand Down Expand Up @@ -145,12 +144,9 @@ public void run() {
});
assertThat(builder.getTracerFactories()).hasSize(2);
assertThat(builder.interceptors).hasSize(0);
try {
Comment thread
AgraVator marked this conversation as resolved.
InternalConfiguratorRegistry.setConfigurators(Collections.emptyList());
fail("exception expected");
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("Configurators are already set");
}
InternalConfiguratorRegistry.setConfigurators(Collections.emptyList());
assertThat(InternalConfiguratorRegistry.getConfigurators()).isEmpty();
assertThat(InternalConfiguratorRegistry.getConfiguratorsCallCountBeforeSet()).isEqualTo(1);
}
}

Expand Down