Skip to content

Commit cc690ff

Browse files
committed
2.x: Avoid using System.getProperties(), some cleanup, up RS 1.0.3
1 parent 13772a1 commit cc690ff

File tree

10 files changed

+91
-65
lines changed

10 files changed

+91
-65
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ targetCompatibility = JavaVersion.VERSION_1_6
5252
// ---------------------------------------
5353

5454
def junitVersion = "4.12"
55-
def reactiveStreamsVersion = "1.0.2"
55+
def reactiveStreamsVersion = "1.0.3"
5656
def mockitoVersion = "2.1.0"
5757
def jmhLibVersion = "1.20"
5858
def testNgVersion = "6.11"

src/main/java/io/reactivex/internal/operators/completable/CompletableAndThenCompletable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static final class NextObserver implements CompletableObserver {
8484

8585
final CompletableObserver downstream;
8686

87-
public NextObserver(AtomicReference<Disposable> parent, CompletableObserver downstream) {
87+
NextObserver(AtomicReference<Disposable> parent, CompletableObserver downstream) {
8888
this.parent = parent;
8989
this.downstream = downstream;
9090
}

src/main/java/io/reactivex/internal/operators/flowable/FlowablePublish.java

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public Publisher<T> source() {
7979
}
8080

8181
/**
82+
* The internal buffer size of this FloawblePublish operator.
8283
* @return The internal buffer size of this FloawblePublish operator.
8384
*/
8485
@Override

src/main/java/io/reactivex/internal/operators/flowable/FlowablePublishAlt.java

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public Publisher<T> source() {
6262
}
6363

6464
/**
65+
* The internal buffer size of this FloawblePublishAlt operator.
6566
* @return The internal buffer size of this FloawblePublishAlt operator.
6667
*/
6768
public int publishBufferSize() {

src/main/java/io/reactivex/internal/operators/flowable/FlowablePublishClassic.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
public interface FlowablePublishClassic<T> {
3131

3232
/**
33+
* The upstream source of this publish operator.
3334
* @return the upstream source of this publish operator
3435
*/
3536
Publisher<T> publishSource();
3637

3738
/**
39+
* The internal buffer size of this publish operator.
3840
* @return the internal buffer size of this publish operator
3941
*/
4042
int publishBufferSize();

src/main/java/io/reactivex/internal/operators/observable/ObservablePublishAlt.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static final class PublishConnection<T>
146146
Throwable error;
147147

148148
@SuppressWarnings("unchecked")
149-
public PublishConnection(AtomicReference<PublishConnection<T>> current) {
149+
PublishConnection(AtomicReference<PublishConnection<T>> current) {
150150
this.connect = new AtomicBoolean();
151151
this.current = current;
152152
this.upstream = new AtomicReference<Disposable>();
@@ -261,7 +261,7 @@ static final class InnerDisposable<T>
261261

262262
final Observer<? super T> downstream;
263263

264-
public InnerDisposable(Observer<? super T> downstream, PublishConnection<T> parent) {
264+
InnerDisposable(Observer<? super T> downstream, PublishConnection<T> parent) {
265265
this.downstream = downstream;
266266
lazySet(parent);
267267
}

src/main/java/io/reactivex/internal/operators/observable/ObservablePublishClassic.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public interface ObservablePublishClassic<T> {
3131

3232
/**
33+
* The upstream source of this publish operator.
3334
* @return the upstream source of this publish operator
3435
*/
3536
ObservableSource<T> publishSource();

src/main/java/io/reactivex/internal/schedulers/SchedulerPoolFactory.java

+35-25
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.*;
2121
import java.util.concurrent.atomic.AtomicReference;
2222

23+
import io.reactivex.functions.Function;
24+
2325
/**
2426
* Manages the creating of ScheduledExecutorServices and sets up purging.
2527
*/
@@ -90,40 +92,48 @@ public static void shutdown() {
9092
}
9193

9294
static {
93-
Properties properties = System.getProperties();
94-
95-
PurgeProperties pp = new PurgeProperties();
96-
pp.load(properties);
97-
98-
PURGE_ENABLED = pp.purgeEnable;
99-
PURGE_PERIOD_SECONDS = pp.purgePeriod;
95+
SystemPropertyAccessor propertyAccessor = new SystemPropertyAccessor();
96+
PURGE_ENABLED = getBooleanProperty(true, PURGE_ENABLED_KEY, true, true, propertyAccessor);
97+
PURGE_PERIOD_SECONDS = getIntProperty(PURGE_ENABLED, PURGE_PERIOD_SECONDS_KEY, 1, 1, propertyAccessor);
10098

10199
start();
102100
}
103101

104-
static final class PurgeProperties {
105-
106-
boolean purgeEnable;
107-
108-
int purgePeriod;
109-
110-
void load(Properties properties) {
111-
if (properties.containsKey(PURGE_ENABLED_KEY)) {
112-
purgeEnable = Boolean.parseBoolean(properties.getProperty(PURGE_ENABLED_KEY));
113-
} else {
114-
purgeEnable = true;
102+
static int getIntProperty(boolean enabled, String key, int defaultNotFound, int defaultNotEnabled, Function<String, String> propertyAccessor) {
103+
if (enabled) {
104+
try {
105+
String value = propertyAccessor.apply(key);
106+
if (value == null) {
107+
return defaultNotFound;
108+
}
109+
return Integer.parseInt(value);
110+
} catch (Throwable ex) {
111+
return defaultNotFound;
115112
}
113+
}
114+
return defaultNotEnabled;
115+
}
116116

117-
if (purgeEnable && properties.containsKey(PURGE_PERIOD_SECONDS_KEY)) {
118-
try {
119-
purgePeriod = Integer.parseInt(properties.getProperty(PURGE_PERIOD_SECONDS_KEY));
120-
} catch (NumberFormatException ex) {
121-
purgePeriod = 1;
117+
static boolean getBooleanProperty(boolean enabled, String key, boolean defaultNotFound, boolean defaultNotEnabled, Function<String, String> propertyAccessor) {
118+
if (enabled) {
119+
try {
120+
String value = propertyAccessor.apply(key);
121+
if (value == null) {
122+
return defaultNotFound;
122123
}
123-
} else {
124-
purgePeriod = 1;
124+
return "true".equals(value);
125+
} catch (Throwable ex) {
126+
return defaultNotFound;
125127
}
126128
}
129+
return defaultNotEnabled;
130+
}
131+
132+
static final class SystemPropertyAccessor implements Function<String, String> {
133+
@Override
134+
public String apply(String t) throws Exception {
135+
return System.getProperty(t);
136+
}
127137
}
128138

129139
/**

src/main/java/io/reactivex/observers/BaseTestConsumer.java

-1
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ public final U assertValues(T... values) {
555555
* @return this
556556
* @since 2.2
557557
*/
558-
@SuppressWarnings("unchecked")
559558
public final U assertValuesOnly(T... values) {
560559
return assertSubscribed()
561560
.assertValues(values)

src/test/java/io/reactivex/internal/schedulers/SchedulerPoolFactoryTest.java

+47-35
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
import static org.junit.Assert.*;
2020

21-
import java.util.Properties;
22-
2321
import org.junit.Test;
2422

2523
import io.reactivex.TestHelper;
26-
import io.reactivex.internal.schedulers.SchedulerPoolFactory.PurgeProperties;
24+
import io.reactivex.functions.Function;
25+
import io.reactivex.internal.functions.Functions;
2726
import io.reactivex.schedulers.Schedulers;
2827

2928
public class SchedulerPoolFactoryTest {
@@ -78,53 +77,66 @@ public void run() {
7877
}
7978

8079
@Test
81-
public void loadPurgeProperties() {
82-
Properties props1 = new Properties();
83-
84-
PurgeProperties pp = new PurgeProperties();
85-
pp.load(props1);
86-
87-
assertTrue(pp.purgeEnable);
88-
assertEquals(pp.purgePeriod, 1);
80+
public void boolPropertiesDisabledReturnsDefaultDisabled() throws Throwable {
81+
assertTrue(SchedulerPoolFactory.getBooleanProperty(false, "key", false, true, failingPropertiesAccessor));
82+
assertFalse(SchedulerPoolFactory.getBooleanProperty(false, "key", true, false, failingPropertiesAccessor));
8983
}
9084

9185
@Test
92-
public void loadPurgePropertiesDisabled() {
93-
Properties props1 = new Properties();
94-
props1.setProperty(SchedulerPoolFactory.PURGE_ENABLED_KEY, "false");
86+
public void boolPropertiesEnabledMissingReturnsDefaultMissing() throws Throwable {
87+
assertTrue(SchedulerPoolFactory.getBooleanProperty(true, "key", true, false, missingPropertiesAccessor));
88+
assertFalse(SchedulerPoolFactory.getBooleanProperty(true, "key", false, true, missingPropertiesAccessor));
89+
}
9590

96-
PurgeProperties pp = new PurgeProperties();
97-
pp.load(props1);
91+
@Test
92+
public void boolPropertiesFailureReturnsDefaultMissing() throws Throwable {
93+
assertTrue(SchedulerPoolFactory.getBooleanProperty(true, "key", true, false, failingPropertiesAccessor));
94+
assertFalse(SchedulerPoolFactory.getBooleanProperty(true, "key", false, true, failingPropertiesAccessor));
95+
}
9896

99-
assertFalse(pp.purgeEnable);
100-
assertEquals(pp.purgePeriod, 1);
97+
@Test
98+
public void boolPropertiesReturnsValue() throws Throwable {
99+
assertTrue(SchedulerPoolFactory.getBooleanProperty(true, "true", true, false, Functions.<String>identity()));
100+
assertFalse(SchedulerPoolFactory.getBooleanProperty(true, "false", false, true, Functions.<String>identity()));
101101
}
102102

103103
@Test
104-
public void loadPurgePropertiesEnabledCustomPeriod() {
105-
Properties props1 = new Properties();
106-
props1.setProperty(SchedulerPoolFactory.PURGE_ENABLED_KEY, "true");
107-
props1.setProperty(SchedulerPoolFactory.PURGE_PERIOD_SECONDS_KEY, "2");
104+
public void intPropertiesDisabledReturnsDefaultDisabled() throws Throwable {
105+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(false, "key", 0, -1, failingPropertiesAccessor));
106+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(false, "key", 1, -1, failingPropertiesAccessor));
107+
}
108108

109-
PurgeProperties pp = new PurgeProperties();
110-
pp.load(props1);
109+
@Test
110+
public void intPropertiesEnabledMissingReturnsDefaultMissing() throws Throwable {
111+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 0, missingPropertiesAccessor));
112+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 1, missingPropertiesAccessor));
113+
}
111114

112-
assertTrue(pp.purgeEnable);
113-
assertEquals(pp.purgePeriod, 2);
115+
@Test
116+
public void intPropertiesFailureReturnsDefaultMissing() throws Throwable {
117+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 0, failingPropertiesAccessor));
118+
assertEquals(-1, SchedulerPoolFactory.getIntProperty(true, "key", -1, 1, failingPropertiesAccessor));
114119
}
115120

116121
@Test
117-
public void loadPurgePropertiesEnabledCustomPeriodNaN() {
118-
Properties props1 = new Properties();
119-
props1.setProperty(SchedulerPoolFactory.PURGE_ENABLED_KEY, "true");
120-
props1.setProperty(SchedulerPoolFactory.PURGE_PERIOD_SECONDS_KEY, "abc");
122+
public void intPropertiesReturnsValue() throws Throwable {
123+
assertEquals(1, SchedulerPoolFactory.getIntProperty(true, "1", 0, 4, Functions.<String>identity()));
124+
assertEquals(2, SchedulerPoolFactory.getIntProperty(true, "2", 3, 5, Functions.<String>identity()));
125+
}
121126

122-
PurgeProperties pp = new PurgeProperties();
123-
pp.load(props1);
127+
static final Function<String, String> failingPropertiesAccessor = new Function<String, String>() {
128+
@Override
129+
public String apply(String v) throws Exception {
130+
throw new SecurityException();
131+
}
132+
};
124133

125-
assertTrue(pp.purgeEnable);
126-
assertEquals(pp.purgePeriod, 1);
127-
}
134+
static final Function<String, String> missingPropertiesAccessor = new Function<String, String>() {
135+
@Override
136+
public String apply(String v) throws Exception {
137+
return null;
138+
}
139+
};
128140

129141
@Test
130142
public void putIntoPoolNoPurge() {

0 commit comments

Comments
 (0)