+ @Test
+ public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
+ String testName = "testSingleShardTransactionsWithLeaderChanges";
+ initDatastores(testName);
+
+ String followerCarShardName = "member-2-shard-cars-" + testName;
+ InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class );
+
+ // Write top-level car container from the follower so it uses a remote Tx.
+
+ DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
+
+ writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
+ writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
+
+ followerTestKit.doCommit(writeTx.ready());
+
+ InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
+
+ // Switch the leader to the follower
+
+ followerDatastoreContextBuilder.shardElectionTimeoutFactor(1);
+ followerDistributedDataStore.onDatastoreContextUpdated(followerDatastoreContextBuilder.build());
+
+ JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
+
+ followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), SHARD_NAMES);
+
+ leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
+ Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
+
+ DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder().
+ shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
+ IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder);
+ DistributedDataStore newMember1Datastore = newMember1TestKit.
+ setupDistributedDataStore(testName, MODULE_SHARDS_CONFIG, false, SHARD_NAMES);
+
+ followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), SHARD_NAMES);
+
+ // Write a car entry to the new leader - should switch to local Tx
+
+ writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
+
+ MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
+ YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
+ writeTx.merge(car1Path, car1);
+
+ followerTestKit.doCommit(writeTx.ready());
+
+ verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
+ }
+