Skip to content

Commit b21504d

Browse files
authored
1.x: add AnimalSniffer to the build process, fix and suppress violations (#4092)
* 1.x: add AnimalSniffer to the build process, fix and suppress violations * Add more time, disable excessive thread use tests * Demonstrate violation. * javac obviously fails with 8 methods * Undo demonstration
1 parent 5be3a4b commit b21504d

23 files changed

+129
-17
lines changed

build.gradle

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
buildscript {
2-
repositories { jcenter() }
3-
dependencies { classpath 'com.netflix.nebula:gradle-rxjava-project-plugin:4.0.0' }
2+
repositories {
3+
jcenter()
4+
}
5+
dependencies {
6+
classpath 'com.netflix.nebula:gradle-rxjava-project-plugin:4.0.0'
7+
classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.1.0'
8+
}
49
}
510

611
description = 'RxJava: Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.'
712

813
apply plugin: 'java'
914
apply plugin: 'pmd'
1015
apply plugin: 'jacoco'
16+
apply plugin: 'ru.vyarus.animalsniffer'
1117
apply plugin: 'nebula.rxjava-project'
1218

19+
repositories {
20+
mavenCentral()
21+
}
22+
1323
dependencies {
24+
signature 'org.codehaus.mojo.signature:java16:1.1@signature'
25+
1426
testCompile 'junit:junit:4.12'
1527
testCompile 'org.mockito:mockito-core:1.10.19'
1628

@@ -95,4 +107,8 @@ task pmdPrint(dependsOn: 'pmdMain') << {
95107
}
96108
}
97109

98-
build.dependsOn pmdPrint
110+
build.dependsOn pmdPrint
111+
112+
animalsniffer {
113+
annotation = 'rx.internal.util.SuppressAnimalSniffer'
114+
}

src/main/java/rx/internal/schedulers/NewThreadWorker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public static void deregisterExecutor(ScheduledExecutorService service) {
109109
}
110110

111111
/** Purges each registered executor and eagerly evicts shutdown executors. */
112+
@SuppressAnimalSniffer // CHM.keySet returns KeySetView in Java 8+; false positive here
112113
static void purgeExecutors() {
113114
try {
114115
Iterator<ScheduledThreadPoolExecutor> it = EXECUTORS.keySet().iterator();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package rx.internal.util;
18+
19+
import java.lang.annotation.*;
20+
21+
/**
22+
* Suppress errors by the AnimalSniffer plugin.
23+
*/
24+
@Retention(RetentionPolicy.CLASS)
25+
@Documented
26+
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
27+
public @interface SuppressAnimalSniffer {
28+
29+
}

src/main/java/rx/internal/util/UtilityFunctions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package rx.internal.util;
1414

15+
import java.util.Collections;
16+
1517
import rx.functions.Func0;
1618
import rx.functions.Func1;
1719
import rx.functions.Func2;
@@ -178,5 +180,4 @@ public R call(Object... args) {
178180
return null;
179181
}
180182
}
181-
182183
}

src/main/java/rx/internal/util/unsafe/BaseLinkedQueue.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import java.util.*;
2222

23+
import rx.internal.util.SuppressAnimalSniffer;
2324
import rx.internal.util.atomic.LinkedQueueNode;
2425

2526
abstract class BaseLinkedQueuePad0<E> extends AbstractQueue<E> {
2627
long p00, p01, p02, p03, p04, p05, p06, p07;
2728
long p30, p31, p32, p33, p34, p35, p36, p37;
2829
}
2930

31+
@SuppressAnimalSniffer
3032
abstract class BaseLinkedQueueProducerNodeRef<E> extends BaseLinkedQueuePad0<E> {
3133
protected final static long P_NODE_OFFSET = UnsafeAccess.addressOf(BaseLinkedQueueProducerNodeRef.class, "producerNode");
3234

@@ -50,6 +52,7 @@ abstract class BaseLinkedQueuePad1<E> extends BaseLinkedQueueProducerNodeRef<E>
5052
long p30, p31, p32, p33, p34, p35, p36, p37;
5153
}
5254

55+
@SuppressAnimalSniffer
5356
abstract class BaseLinkedQueueConsumerNodeRef<E> extends BaseLinkedQueuePad1<E> {
5457
protected final static long C_NODE_OFFSET = UnsafeAccess.addressOf(BaseLinkedQueueConsumerNodeRef.class, "consumerNode");
5558
protected LinkedQueueNode<E> consumerNode;
@@ -72,8 +75,9 @@ protected final LinkedQueueNode<E> lpConsumerNode() {
7275
*
7376
* @author nitsanw
7477
*
75-
* @param <E>
78+
* @param <E> the element type
7679
*/
80+
@SuppressAnimalSniffer
7781
abstract class BaseLinkedQueue<E> extends BaseLinkedQueueConsumerNodeRef<E> {
7882
long p00, p01, p02, p03, p04, p05, p06, p07;
7983
long p30, p31, p32, p33, p34, p35, p36, p37;

src/main/java/rx/internal/util/unsafe/ConcurrentCircularArrayQueue.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
2020

21-
import java.util.AbstractQueue;
22-
import java.util.Iterator;
21+
import java.util.*;
22+
23+
import rx.internal.util.SuppressAnimalSniffer;
2324

2425
abstract class ConcurrentCircularArrayQueueL0Pad<E> extends AbstractQueue<E> implements MessagePassingQueue<E> {
2526
long p00, p01, p02, p03, p04, p05, p06, p07;
@@ -40,8 +41,9 @@ abstract class ConcurrentCircularArrayQueueL0Pad<E> extends AbstractQueue<E> imp
4041
*
4142
* @author nitsanw
4243
*
43-
* @param <E>
44+
* @param <E> the element type
4445
*/
46+
@SuppressAnimalSniffer
4547
public abstract class ConcurrentCircularArrayQueue<E> extends ConcurrentCircularArrayQueueL0Pad<E> {
4648
protected static final int SPARSE_SHIFT = Integer.getInteger("sparse.shift", 0);
4749
protected static final int BUFFER_PAD = 32;

src/main/java/rx/internal/util/unsafe/ConcurrentSequencedCircularArrayQueue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
2020

21+
import rx.internal.util.SuppressAnimalSniffer;
22+
23+
@SuppressAnimalSniffer
2124
public abstract class ConcurrentSequencedCircularArrayQueue<E> extends ConcurrentCircularArrayQueue<E> {
2225
private static final long ARRAY_BASE;
2326
private static final int ELEMENT_SHIFT;

src/main/java/rx/internal/util/unsafe/MpmcArrayQueue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
2020

21+
import rx.internal.util.SuppressAnimalSniffer;
22+
2123
abstract class MpmcArrayQueueL1Pad<E> extends ConcurrentSequencedCircularArrayQueue<E> {
2224
long p10, p11, p12, p13, p14, p15, p16;
2325
long p30, p31, p32, p33, p34, p35, p36, p37;
@@ -27,6 +29,7 @@ public MpmcArrayQueueL1Pad(int capacity) {
2729
}
2830
}
2931

32+
@SuppressAnimalSniffer
3033
abstract class MpmcArrayQueueProducerField<E> extends MpmcArrayQueueL1Pad<E> {
3134
private final static long P_INDEX_OFFSET = UnsafeAccess.addressOf(MpmcArrayQueueProducerField.class, "producerIndex");
3235
private volatile long producerIndex;
@@ -53,6 +56,7 @@ public MpmcArrayQueueL2Pad(int capacity) {
5356
}
5457
}
5558

59+
@SuppressAnimalSniffer
5660
abstract class MpmcArrayQueueConsumerField<E> extends MpmcArrayQueueL2Pad<E> {
5761
private final static long C_INDEX_OFFSET = UnsafeAccess.addressOf(MpmcArrayQueueConsumerField.class, "consumerIndex");
5862
private volatile long consumerIndex;
@@ -94,6 +98,7 @@ protected final boolean casConsumerIndex(long expect, long newValue) {
9498
* @param <E>
9599
* type of the element stored in the {@link java.util.Queue}
96100
*/
101+
@SuppressAnimalSniffer
97102
public class MpmcArrayQueue<E> extends MpmcArrayQueueConsumerField<E> {
98103
long p40, p41, p42, p43, p44, p45, p46;
99104
long p30, p31, p32, p33, p34, p35, p36, p37;

src/main/java/rx/internal/util/unsafe/MpscLinkedQueue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package rx.internal.util.unsafe;
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
20+
21+
import rx.internal.util.SuppressAnimalSniffer;
2022
import rx.internal.util.atomic.LinkedQueueNode;
2123
/**
2224
* This is a direct Java port of the MPSC algorithm as presented <a
@@ -34,6 +36,7 @@
3436
*
3537
* @param <E>
3638
*/
39+
@SuppressAnimalSniffer
3740
public final class MpscLinkedQueue<E> extends BaseLinkedQueue<E> {
3841

3942
public MpscLinkedQueue() {

src/main/java/rx/internal/util/unsafe/SpmcArrayQueue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
2020

21+
import rx.internal.util.SuppressAnimalSniffer;
22+
2123
abstract class SpmcArrayQueueL1Pad<E> extends ConcurrentCircularArrayQueue<E> {
2224
long p10, p11, p12, p13, p14, p15, p16;
2325
long p30, p31, p32, p33, p34, p35, p36, p37;
@@ -27,6 +29,7 @@ public SpmcArrayQueueL1Pad(int capacity) {
2729
}
2830
}
2931

32+
@SuppressAnimalSniffer
3033
abstract class SpmcArrayQueueProducerField<E> extends SpmcArrayQueueL1Pad<E> {
3134
protected final static long P_INDEX_OFFSET = UnsafeAccess.addressOf(SpmcArrayQueueProducerField.class, "producerIndex");
3235
private volatile long producerIndex;
@@ -53,6 +56,7 @@ public SpmcArrayQueueL2Pad(int capacity) {
5356
}
5457
}
5558

59+
@SuppressAnimalSniffer
5660
abstract class SpmcArrayQueueConsumerField<E> extends SpmcArrayQueueL2Pad<E> {
5761
protected final static long C_INDEX_OFFSET = UnsafeAccess.addressOf(SpmcArrayQueueConsumerField.class, "consumerIndex");
5862
private volatile long consumerIndex;
@@ -79,6 +83,7 @@ public SpmcArrayQueueMidPad(int capacity) {
7983
}
8084
}
8185

86+
@SuppressAnimalSniffer
8287
abstract class SpmcArrayQueueProducerIndexCacheField<E> extends SpmcArrayQueueMidPad<E> {
8388
// This is separated from the consumerIndex which will be highly contended in the hope that this value spends most
8489
// of it's time in a cache line that is Shared(and rarely invalidated)
@@ -106,6 +111,7 @@ public SpmcArrayQueueL3Pad(int capacity) {
106111
}
107112
}
108113

114+
@SuppressAnimalSniffer
109115
public final class SpmcArrayQueue<E> extends SpmcArrayQueueL3Pad<E> {
110116

111117
public SpmcArrayQueue(final int capacity) {

src/main/java/rx/internal/util/unsafe/SpscArrayQueue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static rx.internal.util.unsafe.UnsafeAccess.UNSAFE;
2020

21+
import rx.internal.util.SuppressAnimalSniffer;
22+
2123
abstract class SpscArrayQueueColdField<E> extends ConcurrentCircularArrayQueue<E> {
2224
private static final Integer MAX_LOOK_AHEAD_STEP = Integer.getInteger("jctools.spsc.max.lookahead.step", 4096);
2325
protected final int lookAheadStep;
@@ -35,6 +37,7 @@ public SpscArrayQueueL1Pad(int capacity) {
3537
}
3638
}
3739

40+
@SuppressAnimalSniffer
3841
abstract class SpscArrayQueueProducerFields<E> extends SpscArrayQueueL1Pad<E> {
3942
protected final static long P_INDEX_OFFSET = UnsafeAccess.addressOf(SpscArrayQueueProducerFields.class, "producerIndex");
4043
protected long producerIndex;
@@ -54,6 +57,7 @@ public SpscArrayQueueL2Pad(int capacity) {
5457
}
5558
}
5659

60+
@SuppressAnimalSniffer
5761
abstract class SpscArrayQueueConsumerField<E> extends SpscArrayQueueL2Pad<E> {
5862
protected long consumerIndex;
5963
protected final static long C_INDEX_OFFSET = UnsafeAccess.addressOf(SpscArrayQueueConsumerField.class, "consumerIndex");
@@ -87,6 +91,7 @@ public SpscArrayQueueL3Pad(int capacity) {
8791
*
8892
* @param <E>
8993
*/
94+
@SuppressAnimalSniffer
9095
public final class SpscArrayQueue<E> extends SpscArrayQueueL3Pad<E> {
9196

9297
public SpscArrayQueue(final int capacity) {

src/main/java/rx/internal/util/unsafe/SpscUnboundedArrayQueue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.AbstractQueue;
2323
import java.util.Iterator;
2424

25+
import rx.internal.util.SuppressAnimalSniffer;
26+
2527
abstract class SpscUnboundedArrayQueueProducerFields<E> extends AbstractQueue<E> {
2628
protected long producerIndex;
2729
}
@@ -46,6 +48,7 @@ abstract class SpscUnboundedArrayQueueConsumerField<E> extends SpscUnboundedArra
4648
protected long consumerIndex;
4749
}
4850

51+
@SuppressAnimalSniffer
4952
public class SpscUnboundedArrayQueue<E> extends SpscUnboundedArrayQueueConsumerField<E>
5053
implements QueueProgressIndicators{
5154
static final int MAX_LOOK_AHEAD_STEP = Integer.getInteger("jctools.spsc.max.lookahead.step", 4096);

src/main/java/rx/internal/util/unsafe/UnsafeAccess.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.lang.reflect.Field;
1919

20+
import rx.internal.util.SuppressAnimalSniffer;
2021
import sun.misc.Unsafe;
2122

2223
/**
@@ -26,6 +27,7 @@
2627
* Note that you can force RxJava to not use Unsafe API by setting any value to System Property
2728
* {@code rx.unsafe-disable}.
2829
*/
30+
@SuppressAnimalSniffer
2931
public final class UnsafeAccess {
3032

3133
public static final Unsafe UNSAFE;

src/test/java/rx/internal/operators/OperatorMapPairTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package rx.internal.operators;
217

318
import org.junit.*;

src/test/java/rx/internal/operators/OperatorSwitchTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,9 @@ public void call(Throwable e) {
873873

874874
ts.awaitTerminalEvent(60, TimeUnit.SECONDS);
875875
if (!q.isEmpty()) {
876-
throw new AssertionError("Dropped exceptions", new CompositeException(q));
876+
AssertionError ae = new AssertionError("Dropped exceptions");
877+
ae.initCause(new CompositeException(q));
878+
throw ae;
877879
}
878880
ts.assertNoErrors();
879881
if (ts.getCompletions() == 0) {

src/test/java/rx/observers/TestSubscriberTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public void onNext(Integer t) {
590590
ts.awaitTerminalEvent();
591591
}
592592

593-
@Test(timeout = 1000)
593+
@Test(timeout = 5000)
594594
public void testOnErrorCrashCountsDownLatch() {
595595
Observer<Integer> to = new Observer<Integer>() {
596596
@Override

src/test/java/rx/plugins/RxJavaPluginsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public Date call(Date date) {
232232
throw new IllegalStateException("Trigger OnNextValue");
233233
}
234234
})
235-
.timeout(500, TimeUnit.MILLISECONDS)
235+
.timeout(5000, TimeUnit.MILLISECONDS)
236236
.toBlocking().first();
237237
fail("Did not expect onNext/onCompleted, got " + notExpected);
238238
} catch (IllegalStateException e) {

src/test/java/rx/schedulers/ResetSchedulersTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package rx.schedulers;
217

318

src/test/java/rx/subjects/BehaviorSubjectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void testCompletedAfterErrorIsNotSent3() {
240240
verify(o2, times(1)).onCompleted();
241241
verifyNoMoreInteractions(o2);
242242
}
243-
@Test(timeout = 1000)
243+
@Test(timeout = 5000)
244244
public void testUnsubscriptionCase() {
245245
BehaviorSubject<String> src = BehaviorSubject.create((String)null);
246246

0 commit comments

Comments
 (0)