Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit f73dc21

Browse files
author
hideki
committed
Fixed java-core 1163 - Replicator sends duplicated Replicator state.
- Reason duplicated replicator states are notified by `changed()` is because most of unit test codes checks replicator state which could be already changed to next state. So replicator enters STOPPING state, then fired, following enters STOPPED state, then fired. When listener receives notification for STOPPING, replicator stated already changed to STOPPED state. So it seems like STOPPED state twice. But actually STOPPING state and STOPPED states are fired. Instead of checking Replicator.status, needs to check transition destination.
1 parent b669a9a commit f73dc21

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

src/androidTest/java/com/couchbase/lite/DatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void testGetActiveReplications() throws Exception {
191191
assertEquals(0, database.getActiveReplications().size());
192192

193193
final CountDownLatch replicationRunning = new CountDownLatch(1);
194-
replication.addChangeListener(new ReplicationActiveObserver(replicationRunning));
194+
replication.addChangeListener(new ReplicationRunningObserver(replicationRunning));
195195

196196
replication.start();
197197

src/androidTest/java/com/couchbase/lite/LiteTestCaseWithDB.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.couchbase.lite.mockserver.MockHelper;
88
import com.couchbase.lite.replicator.CustomizableMockHttpClient;
99
import com.couchbase.lite.replicator.Replication;
10+
import com.couchbase.lite.replicator.ReplicationState;
1011
import com.couchbase.lite.router.Router;
1112
import com.couchbase.lite.router.RouterCallbackBlock;
1213
import com.couchbase.lite.router.URLConnection;
@@ -824,18 +825,16 @@ protected boolean isBulkDocJsonContainsDoc(RecordedRequest request, Document doc
824825
}
825826

826827
public static class ReplicationIdleObserver implements Replication.ChangeListener {
828+
private CountDownLatch idleSignal;
827829

828-
private CountDownLatch doneSignal;
829-
830-
public ReplicationIdleObserver(CountDownLatch doneSignal) {
831-
this.doneSignal = doneSignal;
830+
public ReplicationIdleObserver(CountDownLatch idleSignal) {
831+
this.idleSignal = idleSignal;
832832
}
833833

834834
@Override
835835
public void changed(Replication.ChangeEvent event) {
836-
837-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
838-
doneSignal.countDown();
836+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
837+
idleSignal.countDown();
839838
}
840839
}
841840
}
@@ -849,43 +848,39 @@ public ReplicationFinishedObserver(CountDownLatch doneSignal) {
849848

850849
@Override
851850
public void changed(Replication.ChangeEvent event) {
852-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_STOPPED) {
851+
if (event.getTransition().getDestination() == ReplicationState.STOPPED) {
853852
doneSignal.countDown();
854853
assertEquals(event.getChangeCount(), event.getCompletedChangeCount());
855854
}
856855
}
857856
}
858857

859858
public static class ReplicationOfflineObserver implements Replication.ChangeListener {
859+
private CountDownLatch offlineSignal;
860860

861-
private CountDownLatch doneSignal;
862-
863-
public ReplicationOfflineObserver(CountDownLatch doneSignal) {
864-
this.doneSignal = doneSignal;
861+
public ReplicationOfflineObserver(CountDownLatch offlineSignal) {
862+
this.offlineSignal = offlineSignal;
865863
}
866864

867865
@Override
868866
public void changed(Replication.ChangeEvent event) {
869-
870-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_OFFLINE) {
871-
doneSignal.countDown();
867+
if (event.getTransition().getDestination() == ReplicationState.OFFLINE) {
868+
offlineSignal.countDown();
872869
}
873870
}
874871
}
875872

876-
public static class ReplicationActiveObserver implements Replication.ChangeListener {
877-
878-
private CountDownLatch doneSignal;
873+
public static class ReplicationRunningObserver implements Replication.ChangeListener {
874+
private CountDownLatch runningSignal;
879875

880-
public ReplicationActiveObserver(CountDownLatch doneSignal) {
881-
this.doneSignal = doneSignal;
876+
public ReplicationRunningObserver(CountDownLatch runningSignal) {
877+
this.runningSignal = runningSignal;
882878
}
883879

884880
@Override
885881
public void changed(Replication.ChangeEvent event) {
886-
887-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_ACTIVE) {
888-
doneSignal.countDown();
882+
if (event.getTransition().getDestination() == ReplicationState.RUNNING) {
883+
runningSignal.countDown();
889884
}
890885
}
891886
}

src/androidTest/java/com/couchbase/lite/performance/PerformanceTestCase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import com.couchbase.lite.LiteTestCase;
2222
import com.couchbase.lite.Manager;
2323
import com.couchbase.lite.ManagerOptions;
24-
import com.couchbase.lite.storage.SQLiteNativeLibrary;
2524
import com.couchbase.lite.replicator.Replication;
25+
import com.couchbase.lite.replicator.ReplicationState;
26+
import com.couchbase.lite.storage.SQLiteNativeLibrary;
2627
import com.couchbase.lite.support.FileDirUtils;
2728
import com.couchbase.lite.util.Log;
2829
import com.couchbase.lite.util.Utils;
@@ -205,7 +206,7 @@ public ReplicationFinishedObserver(CountDownLatch doneSignal) {
205206

206207
@Override
207208
public void changed(Replication.ChangeEvent event) {
208-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_STOPPED) {
209+
if (event.getTransition().getDestination() == ReplicationState.STOPPED) {
209210
doneSignal.countDown();
210211
assertEquals(event.getChangeCount(), event.getCompletedChangeCount());
211212
}

src/androidTest/java/com/couchbase/lite/replicator/ReplicationTest.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ private void putReplicationOnline(Replication replication) throws InterruptedExc
26642664

26652665
// this was a useless test, the replication wasn't even started
26662666
final CountDownLatch wentOnline = new CountDownLatch(1);
2667-
Replication.ChangeListener changeListener = new ReplicationActiveObserver(wentOnline);
2667+
Replication.ChangeListener changeListener = new ReplicationRunningObserver(wentOnline);
26682668
replication.addChangeListener(changeListener);
26692669

26702670
replication.goOnline();
@@ -2979,7 +2979,7 @@ public void testGetReplicatorWithCustomHeader() throws Throwable {
29792979
replicator.start();
29802980

29812981
final CountDownLatch replicationStarted = new CountDownLatch(1);
2982-
replicator.addChangeListener(new ReplicationActiveObserver(replicationStarted));
2982+
replicator.addChangeListener(new ReplicationRunningObserver(replicationStarted));
29832983

29842984
boolean success = replicationStarted.await(30, TimeUnit.SECONDS);
29852985
assertTrue(success);
@@ -3040,7 +3040,7 @@ public void testGetReplicator() throws Throwable {
30403040
replicator.start();
30413041

30423042
final CountDownLatch replicationStarted = new CountDownLatch(1);
3043-
replicator.addChangeListener(new ReplicationActiveObserver(replicationStarted));
3043+
replicator.addChangeListener(new ReplicationRunningObserver(replicationStarted));
30443044

30453045
boolean success = replicationStarted.await(30, TimeUnit.SECONDS);
30463046
assertTrue(success);
@@ -3299,7 +3299,7 @@ public void testContinuousPullEntersIdleState() throws Exception {
32993299
pullReplication.addChangeListener(new Replication.ChangeListener() {
33003300
@Override
33013301
public void changed(Replication.ChangeEvent event) {
3302-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
3302+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
33033303
enteredIdleState.countDown();
33043304
}
33053305
}
@@ -4209,7 +4209,7 @@ public void testContinuousPullReplicationGoesIdleTwice() throws Exception {
42094209
pullReplication.addChangeListener(new Replication.ChangeListener() {
42104210
@Override
42114211
public void changed(Replication.ChangeEvent event) {
4212-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4212+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
42134213
Log.e(TAG, "Replication is IDLE 1");
42144214
enteredIdleState1.countDown();
42154215
pullReplication.removeChangeListener(this);
@@ -4237,15 +4237,15 @@ public void changed(Replication.ChangeEvent event) {
42374237
pullReplication.addChangeListener(new Replication.ChangeListener() {
42384238
@Override
42394239
public void changed(Replication.ChangeEvent event) {
4240-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_ACTIVE) {
4240+
if (event.getTransition().getDestination() == ReplicationState.RUNNING) {
42414241
if (enteredRunningState.getCount() > 0) {
42424242
Log.e(TAG, "Replication is RUNNING");
42434243
enteredRunningState.countDown();
42444244
}
42454245
}
42464246
// second IDLE change listener
42474247
// handling IDLE event here. It seems IDLE event was fired before set IDLE event handler
4248-
else if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4248+
else if (event.getTransition().getDestination() == ReplicationState.IDLE) {
42494249
if (enteredRunningState.getCount() <= 0 && enteredIdleState2.getCount() > 0) {
42504250
Log.e(TAG, "Replication is IDLE 2");
42514251
enteredIdleState2.countDown();
@@ -4312,11 +4312,12 @@ public void testContinuousPullReplicationSendStoppedOnce() throws Exception {
43124312
pullReplication.addChangeListener(new Replication.ChangeListener() {
43134313
@Override
43144314
public void changed(Replication.ChangeEvent event) {
4315-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4315+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
43164316
Log.d(TAG, "Replication is IDLE");
43174317
enteredIdleState.countDown();
4318-
} else if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_STOPPED) {
4319-
Log.d(TAG, "Replication is STOPPED");
4318+
} else if (event.getTransition().getDestination() == ReplicationState.STOPPED) {
4319+
event.getTransition().getDestination();
4320+
Log.e(TAG, "Replication is STOPPED", new Exception("HELLO WORLD"));
43204321
enteredStoppedState.countDown();
43214322
}
43224323
}
@@ -4383,8 +4384,7 @@ public void testOneTimePullReplicationSendStoppedOnce() throws Exception {
43834384
pullReplication.addChangeListener(new Replication.ChangeListener() {
43844385
@Override
43854386
public void changed(Replication.ChangeEvent event) {
4386-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_STOPPED &&
4387-
event.getTransition().getDestination() == ReplicationState.STOPPED) {
4387+
if (event.getTransition().getDestination() == ReplicationState.STOPPED) {
43884388
Log.d(TAG, "Replication is STOPPED");
43894389
enteredStoppedState.countDown();
43904390
}
@@ -4450,7 +4450,7 @@ public void testPullReplicatonSendIdleStateAfterCheckPoint() throws Exception {
44504450
pullReplication.addChangeListener(new Replication.ChangeListener() {
44514451
@Override
44524452
public void changed(Replication.ChangeEvent event) {
4453-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4453+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
44544454
pullInitialIdleState.countDown();
44554455
}
44564456
}
@@ -4469,20 +4469,20 @@ public void changed(Replication.ChangeEvent event) {
44694469

44704470

44714471
// 2. Update change event handler for handling ACTIVE and IDLE
4472-
final CountDownLatch activeSignal = new CountDownLatch(1);
4472+
final CountDownLatch runningSignal = new CountDownLatch(1);
44734473
final CountDownLatch idleSignal = new CountDownLatch(1);
44744474
pullReplication.addChangeListener(new Replication.ChangeListener() {
44754475
@Override
44764476
public void changed(Replication.ChangeEvent event) {
44774477
Log.e(TAG, "[changed] PULL -> " + event);
4478-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4478+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
44794479
// make sure pull replicator becomes IDLE after ACTIVE state.
44804480
// so ignore any IDLE state before ACTIVE.
4481-
if (activeSignal.getCount() == 0) {
4481+
if (runningSignal.getCount() == 0) {
44824482
idleSignal.countDown();
44834483
}
4484-
} else if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_ACTIVE) {
4485-
activeSignal.countDown();
4484+
} else if (event.getTransition().getDestination() == ReplicationState.RUNNING) {
4485+
runningSignal.countDown();
44864486
}
44874487
}
44884488
});
@@ -4510,8 +4510,8 @@ public void changed(Replication.ChangeEvent event) {
45104510
mockChangesFeedNoResponse2.setSticky(true);
45114511
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES, mockChangesFeedNoResponse2);
45124512

4513-
// 6. wait for Replication IDLE -> ACTIVE -> IDLE
4514-
success = activeSignal.await(30, TimeUnit.SECONDS);
4513+
// 6. wait for Replication IDLE -> RUNNING -> IDLE
4514+
success = runningSignal.await(30, TimeUnit.SECONDS);
45154515
assertTrue(success);
45164516
success = idleSignal.await(30, TimeUnit.SECONDS);
45174517
assertTrue(success);
@@ -4634,7 +4634,7 @@ public void changed(Replication.ChangeEvent event) {
46344634
if (event.getError() != null) {
46354635
Assert.fail("Should not have any error....");
46364636
}
4637-
if (event.getSource().getStatus() == Replication.ReplicationStatus.REPLICATION_IDLE) {
4637+
if (event.getTransition().getDestination() == ReplicationState.IDLE) {
46384638
idleSignal1.countDown();
46394639
idleSignal2.countDown();
46404640
}
@@ -4787,7 +4787,7 @@ public void testPushReplActiveState() throws Exception {
47874787

47884788
// Event handler for ACTIVE
47894789
CountDownLatch activeSignal = new CountDownLatch(1);
4790-
ReplicationActiveObserver activeObserver = new ReplicationActiveObserver(activeSignal);
4790+
ReplicationRunningObserver activeObserver = new ReplicationRunningObserver(activeSignal);
47914791
pullReplication.addChangeListener(activeObserver);
47924792

47934793
// Event handler for IDLE2

0 commit comments

Comments
 (0)