Skip to content

Commit eb7ecd5

Browse files
committed
core: A new TestUtil for setting flags
1 parent 54fdc07 commit eb7ecd5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/src/testFixtures/java/io/grpc/internal/TestUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,25 @@ public void log(ChannelLogLevel level, String message) {}
164164
@Override
165165
public void log(ChannelLogLevel level, String messageFormat, Object... args) {}
166166
}
167+
168+
/**
169+
* Configures {@link GrpcUtil#getFlag(String, boolean)} to return 'value' for 'flag' for the
170+
* lifetime of the returned {@link AutoCloseable}.
171+
*
172+
* <p>Use the returned {@link AutoCloseable} in a try-with-resources statement to set a flag just
173+
* for its scope and unconditionally restore the flag's previous value upon scope exit.
174+
*
175+
* <p>Use this method in a @Before junit method to set a flag for the scope of a whole test.
176+
* You must save the returned {@link AutoCloseable} and close it in an @After method.
177+
*/
178+
public static AutoCloseable setFlagForScope(String flag, boolean value) {
179+
String oldValue = System.setProperty(flag, Boolean.toString(value));
180+
return () -> {
181+
if (oldValue != null) {
182+
System.setProperty(flag, oldValue);
183+
} else {
184+
System.clearProperty(flag);
185+
}
186+
};
187+
}
167188
}

0 commit comments

Comments
 (0)