Skip to content

Commit e11a969

Browse files
committed
Refactoring tests
1 parent 7c0d40c commit e11a969

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/delegation/AbstractDelegationTokenSecretManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.hadoop.classification.InterfaceStability;
3939
import org.apache.hadoop.fs.statistics.IOStatistics;
4040
import org.apache.hadoop.fs.statistics.IOStatisticsSource;
41+
import org.apache.hadoop.fs.statistics.impl.IOStatisticsBinding;
4142
import org.apache.hadoop.fs.statistics.impl.IOStatisticsStore;
4243
import org.apache.hadoop.io.Text;
4344
import org.apache.hadoop.metrics2.annotation.Metric;
@@ -59,8 +60,6 @@
5960
import org.slf4j.Logger;
6061
import org.slf4j.LoggerFactory;
6162

62-
import static org.apache.hadoop.fs.statistics.impl.IOStatisticsBinding.iostatisticsStore;
63-
6463

6564
@InterfaceAudience.Public
6665
@InterfaceStability.Evolving
@@ -109,7 +108,7 @@ private String formatTokenId(TokenIdent id) {
109108
*/
110109
private DelegationKey currentKey;
111110
/**
112-
* Metrics to track token management operations
111+
* Metrics to track token management operations.
113112
*/
114113
protected DelegationTokenSecretManagerMetrics metrics;
115114

@@ -890,7 +889,7 @@ static DelegationTokenSecretManagerMetrics create() {
890889
}
891890

892891
public DelegationTokenSecretManagerMetrics() {
893-
ioStatistics = iostatisticsStore()
892+
ioStatistics = IOStatisticsBinding.iostatisticsStore()
894893
.withDurationTracking(STORE_TOKEN_STAT, UPDATE_TOKEN_STAT, REMOVE_TOKEN_STAT)
895894
.withCounters(TOKEN_FAILURE_STAT)
896895
.build();

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestDelegationToken.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import java.util.List;
3131
import java.util.Map;
3232

33+
import java.util.concurrent.Callable;
34+
import org.apache.hadoop.metrics2.lib.MutableCounterLong;
35+
import org.apache.hadoop.metrics2.lib.MutableRate;
3336
import org.apache.hadoop.test.LambdaTestUtils;
3437
import org.junit.Assert;
3538

@@ -625,18 +628,12 @@ public void testDelegationTokenSecretManagerMetrics() throws Exception {
625628
try {
626629
dtSecretManager.startThreads();
627630

628-
Assert.assertEquals(0, dtSecretManager.metrics.storeToken.lastStat().numSamples());
629-
final Token<TestDelegationTokenIdentifier> token =
630-
generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker");
631-
Assert.assertEquals(1, dtSecretManager.metrics.storeToken.lastStat().numSamples());
631+
final Token<TestDelegationTokenIdentifier> token = callAndValidateMetrics(dtSecretManager.metrics.storeToken,
632+
() -> generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker"), 1);
632633

633-
Assert.assertEquals(0, dtSecretManager.metrics.updateToken.lastStat().numSamples());
634-
dtSecretManager.renewToken(token, "JobTracker");
635-
Assert.assertEquals(1, dtSecretManager.metrics.updateToken.lastStat().numSamples());
634+
callAndValidateMetrics(dtSecretManager.metrics.updateToken, () -> dtSecretManager.renewToken(token, "JobTracker"), 1);
636635

637-
Assert.assertEquals(0, dtSecretManager.metrics.removeToken.lastStat().numSamples());
638-
dtSecretManager.cancelToken(token, "JobTracker");
639-
Assert.assertEquals(1, dtSecretManager.metrics.removeToken.lastStat().numSamples());
636+
callAndValidateMetrics(dtSecretManager.metrics.removeToken, () -> dtSecretManager.cancelToken(token, "JobTracker"), 1);
640637
} finally {
641638
dtSecretManager.stopThreads();
642639
}
@@ -654,17 +651,32 @@ public void testDelegationTokenSecretManagerMetricsFailures() throws Exception {
654651

655652
dtSecretManager.setThrowError(true);
656653

657-
Assert.assertEquals(0, dtSecretManager.metrics.tokenFailure.value());
658-
generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker");
659-
Assert.assertEquals(1, dtSecretManager.metrics.tokenFailure.value());
654+
callAndValidateMetrics(dtSecretManager.metrics.tokenFailure,
655+
() -> generateDelegationToken(dtSecretManager, "SomeUser", "JobTracker"), 1, false);
660656

661-
LambdaTestUtils.intercept(Exception.class, () -> dtSecretManager.renewToken(token, "JobTracker"));
662-
Assert.assertEquals(2, dtSecretManager.metrics.tokenFailure.value());
657+
callAndValidateMetrics(dtSecretManager.metrics.tokenFailure, () -> dtSecretManager.renewToken(token, "JobTracker"), 2, true);
663658

664-
LambdaTestUtils.intercept(Exception.class, () -> dtSecretManager.cancelToken(token, "JobTracker"));
665-
Assert.assertEquals(3, dtSecretManager.metrics.tokenFailure.value());
659+
callAndValidateMetrics(dtSecretManager.metrics.tokenFailure, () -> dtSecretManager.cancelToken(token, "JobTracker"), 3, true);
666660
} finally {
667661
dtSecretManager.stopThreads();
668662
}
669663
}
664+
665+
private <T> T callAndValidateMetrics(MutableRate metric, Callable<T> callable, int expectedCount) throws Exception {
666+
Assert.assertEquals(expectedCount - 1, metric.lastStat().numSamples());
667+
T returnedObject = callable.call();
668+
Assert.assertEquals(expectedCount, metric.lastStat().numSamples());
669+
return returnedObject;
670+
}
671+
672+
private <T> void callAndValidateMetrics(MutableCounterLong counter, Callable<T> callable, int expectedCount, boolean expectError)
673+
throws Exception {
674+
Assert.assertEquals(expectedCount - 1, counter.value());
675+
if (expectError) {
676+
LambdaTestUtils.intercept(IOException.class, callable);
677+
} else {
678+
callable.call();
679+
}
680+
Assert.assertEquals(expectedCount, counter.value());
681+
}
670682
}

0 commit comments

Comments
 (0)