Skip to content

Commit a429676

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Test which AtomicHelper is used by AggregateFutureState, including after Android optimization.
This is similar to cl/742859752 and cl/742922048, which did the same for `AbstractFutureState`. RELNOTES=n/a PiperOrigin-RevId: 744264888
1 parent e4f3118 commit a429676

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2015 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.util.concurrent;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import junit.framework.TestCase;
20+
import org.jspecify.annotations.NullUnmarked;
21+
22+
/**
23+
* Tests that {@link AggregateFutureState} uses the expected {@code AtomicHelper} implementation.
24+
*
25+
* <p>We have more thorough testing of {@code AtomicHelper} implementations in {@link
26+
* AggregateFutureStateFallbackAtomicHelperTest}. The advantage to this test is that it can run
27+
* under Android.
28+
*/
29+
@NullUnmarked
30+
public class AggregateFutureStateDefaultAtomicHelperTest extends TestCase {
31+
public void testUsingExpectedAtomicHelper() throws Exception {
32+
assertThat(AggregateFutureState.atomicHelperTypeForTest()).isEqualTo("SafeAtomicHelper");
33+
}
34+
}

android/guava/src/com/google/common/util/concurrent/AggregateFutureState.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater;
2121

2222
import com.google.common.annotations.GwtCompatible;
23+
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.j2objc.annotations.ReflectionSupport;
2425
import java.util.Set;
2526
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
@@ -143,13 +144,20 @@ final void clearSeenExceptions() {
143144
seenExceptions = null;
144145
}
145146

147+
@VisibleForTesting
148+
static String atomicHelperTypeForTest() {
149+
return ATOMIC_HELPER.atomicHelperTypeForTest();
150+
}
151+
146152
private abstract static class AtomicHelper {
147153
/** Atomic compare-and-set of the {@link AggregateFutureState#seenExceptions} field. */
148154
abstract void compareAndSetSeenExceptions(
149155
AggregateFutureState<?> state, @Nullable Set<Throwable> expect, Set<Throwable> update);
150156

151157
/** Atomic decrement-and-get of the {@link AggregateFutureState#remaining} field. */
152158
abstract int decrementAndGetRemainingCount(AggregateFutureState<?> state);
159+
160+
abstract String atomicHelperTypeForTest();
153161
}
154162

155163
private static final class SafeAtomicHelper extends AtomicHelper {
@@ -170,6 +178,11 @@ void compareAndSetSeenExceptions(
170178
int decrementAndGetRemainingCount(AggregateFutureState<?> state) {
171179
return remainingCountUpdater.decrementAndGet(state);
172180
}
181+
182+
@Override
183+
String atomicHelperTypeForTest() {
184+
return "SafeAtomicHelper";
185+
}
173186
}
174187

175188
private static final class SynchronizedAtomicHelper extends AtomicHelper {
@@ -189,6 +202,11 @@ int decrementAndGetRemainingCount(AggregateFutureState<?> state) {
189202
return --state.remaining;
190203
}
191204
}
205+
206+
@Override
207+
String atomicHelperTypeForTest() {
208+
return "SynchronizedAtomicHelper";
209+
}
192210
}
193211

194212
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2015 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.util.concurrent;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import junit.framework.TestCase;
20+
import org.jspecify.annotations.NullUnmarked;
21+
22+
/**
23+
* Tests that {@link AggregateFutureState} uses the expected {@code AtomicHelper} implementation.
24+
*
25+
* <p>We have more thorough testing of {@code AtomicHelper} implementations in {@link
26+
* AggregateFutureStateFallbackAtomicHelperTest}. The advantage to this test is that it can run
27+
* under Android.
28+
*/
29+
@NullUnmarked
30+
public class AggregateFutureStateDefaultAtomicHelperTest extends TestCase {
31+
public void testUsingExpectedAtomicHelper() throws Exception {
32+
assertThat(AggregateFutureState.atomicHelperTypeForTest()).isEqualTo("SafeAtomicHelper");
33+
}
34+
}

guava/src/com/google/common/util/concurrent/AggregateFutureState.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater;
2121

2222
import com.google.common.annotations.GwtCompatible;
23+
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.j2objc.annotations.ReflectionSupport;
2425
import java.util.Set;
2526
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
@@ -143,13 +144,20 @@ final void clearSeenExceptions() {
143144
seenExceptions = null;
144145
}
145146

147+
@VisibleForTesting
148+
static String atomicHelperTypeForTest() {
149+
return ATOMIC_HELPER.atomicHelperTypeForTest();
150+
}
151+
146152
private abstract static class AtomicHelper {
147153
/** Atomic compare-and-set of the {@link AggregateFutureState#seenExceptions} field. */
148154
abstract void compareAndSetSeenExceptions(
149155
AggregateFutureState<?> state, @Nullable Set<Throwable> expect, Set<Throwable> update);
150156

151157
/** Atomic decrement-and-get of the {@link AggregateFutureState#remaining} field. */
152158
abstract int decrementAndGetRemainingCount(AggregateFutureState<?> state);
159+
160+
abstract String atomicHelperTypeForTest();
153161
}
154162

155163
private static final class SafeAtomicHelper extends AtomicHelper {
@@ -170,6 +178,11 @@ void compareAndSetSeenExceptions(
170178
int decrementAndGetRemainingCount(AggregateFutureState<?> state) {
171179
return remainingCountUpdater.decrementAndGet(state);
172180
}
181+
182+
@Override
183+
String atomicHelperTypeForTest() {
184+
return "SafeAtomicHelper";
185+
}
173186
}
174187

175188
private static final class SynchronizedAtomicHelper extends AtomicHelper {
@@ -189,6 +202,11 @@ int decrementAndGetRemainingCount(AggregateFutureState<?> state) {
189202
return --state.remaining;
190203
}
191204
}
205+
206+
@Override
207+
String atomicHelperTypeForTest() {
208+
return "SynchronizedAtomicHelper";
209+
}
192210
}
193211

194212
/**

0 commit comments

Comments
 (0)