Skip to content

Commit 1efd68d

Browse files
author
Kaushik Gopal
committed
feat: add a randomly long task to check poll is respected
1 parent 87124aa commit 1efd68d

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

app/src/main/java/com/morihacky/android/rxjava/fragments/PollingFragment.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class PollingFragment
3434

3535
private static final int INITIAL_DELAY = 0;
3636
private static final int POLLING_INTERVAL = 1000;
37+
private static final int POLL_COUNT = 8;
3738

3839
@Bind(R.id.list_threading_log) ListView _logsList;
3940

@@ -66,24 +67,28 @@ public void onDestroy() {
6667

6768
@OnClick(R.id.btn_start_simple_polling)
6869
public void onStartSimplePollingClicked() {
70+
71+
final int pollCount = POLL_COUNT;
72+
6973
_subscriptions.add(//
7074
Observable.interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS)
7175
.map(new Func1<Long, String>() {
7276
@Override
7377
public String call(Long heartBeat) {
74-
return _doNetworkCallAndGetStringResult();
78+
return _doNetworkCallAndGetStringResult(heartBeat);
7579
}
76-
}).take(5)
80+
}).take(pollCount)
7781
.doOnSubscribe(new Action0() {
7882
@Override
7983
public void call() {
80-
_log(String.format("Simple String polling - %s", _counter));
84+
_log(String.format("Start simple polling - %s", _counter));
8185
}
8286
})
8387
.subscribe(new Action1<String>() {
8488
@Override
85-
public void call(String s) {
86-
_log(String.format("Start simple polling - %s", s));
89+
public void call(String taskName) {
90+
_log(String.format(Locale.US, "Executing polled task [%s] now time : [xx:%02d]",
91+
taskName, _getSecondHand()));
8792
}
8893
})
8994
);
@@ -94,14 +99,14 @@ public void onStartIncreasinglyDelayedPolling() {
9499
_setupLogger();
95100

96101
final int pollingInterval = POLLING_INTERVAL;
97-
final int repeatLimit = 5;
102+
final int pollCount = POLL_COUNT;
98103

99104
_log(String.format(Locale.US, "Start increasingly delayed polling now time: [xx:%02d]",
100105
_getSecondHand()));
101106

102107
_subscriptions.add(//
103108
Observable.just(1)
104-
.repeatWhen(new RepeatWithDelay(repeatLimit, pollingInterval))
109+
.repeatWhen(new RepeatWithDelay(pollCount, pollingInterval))
105110
.subscribe(new Action1<Object>() {
106111
@Override
107112
public void call(Object o) {
@@ -175,10 +180,16 @@ public Observable<?> call(Void blah) {
175180
// -----------------------------------------------------------------------------------
176181
// Method that help wiring up the example (irrelevant to RxJava)
177182

178-
private String _doNetworkCallAndGetStringResult() {
179-
183+
private String _doNetworkCallAndGetStringResult(long attempt) {
180184
try {
181-
Thread.sleep(3000);
185+
if (attempt == 4) {
186+
// randomly make one event super long so we test that the repeat logic waits
187+
// and accounts for this.
188+
Thread.sleep(9000);
189+
} else {
190+
Thread.sleep(3000);
191+
}
192+
182193
} catch (InterruptedException e) {
183194
Timber.d("Operation was interrupted");
184195
}

0 commit comments

Comments
 (0)