Skip to content

Commit

Permalink
#111: fixed listening for client updates during bulk synchronisation
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Nov 5, 2017
1 parent 9080dc9 commit fec2875
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.buggy.shoplist.sharing;

import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;

Expand Down Expand Up @@ -80,7 +82,14 @@ public void updateServerEntity(Category clientEntity, DatabaseReference serverEn
final EntitySynchronizationRecord<Category> synchronizationRecord = getDao().findSynchronizationRecord(
clientEntity.getId(), listId, Category.class);

updateServerEntity(clientEntity, serverEntity, synchronizationRecord);
if (synchronizationRecord != null) {
updateServerEntity(clientEntity, serverEntity, synchronizationRecord);
} else {
Log.w("CategorySynchronizer", "updateServerEntity: sync record not found" +
". listId=" + listId +
", externalId=" + serverEntity +
", category=" + clientEntity);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public void entityAdded(final T newEntity) {
}

final DatabaseReference userList = FirebaseSynchronizer.this.activeUserList;
if (userList == null) {
if ((userList == null) || (stateReference.get() != State.SUBSCRIBED)) {
return;
}

Expand Down Expand Up @@ -552,7 +552,7 @@ public void entityChanged(final T changedEntity) {
changedEntity.getId(), entityClass, new Date(), false);

final DatabaseReference userList = FirebaseSynchronizer.this.activeUserList;
if (userList == null) {
if ((userList == null) || (stateReference.get() != State.SUBSCRIBED)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ public void updateServerEntity(Product clientProduct, DatabaseReference serverEn
final EntitySynchronizationRecord<Product> productRecord = getDao().findSynchronizationRecord(
clientProduct.getId(), listId, Product.class);

updateServerEntity(clientProduct, serverEntity, productRecord);
if (productRecord != null) {
updateServerEntity(clientProduct, serverEntity, productRecord);
} else {
Log.w("ProductSynchronizer", "updateServerEntity: record not found" +
". listId=" + listId +
", externalId=" + serverEntity.getKey() +
", product=" + clientProduct);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ public void updateServerEntity(ShopItem clientEntity, DatabaseReference serverEn
final EntitySynchronizationRecord<ShopItem> itemRecord =
getDao().findSynchronizationRecord(clientEntity.getId(), listId, ShopItem.class);

updateServerEntity(clientEntity, serverEntity, itemRecord);
if (itemRecord != null) {
updateServerEntity(clientEntity, serverEntity, itemRecord);
} else {
Log.w("ShopItemSynchronizer", "updateServerEntity: sync record not found" +
". listId=" + listId +
", externalId=" + serverEntity.getKey() +
", shopItem=" + clientEntity);
}
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions SheLi/src/test/java/net/buggy/shoplist/data/InMemoryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public List<Category> getCategories() {

@Override
public void removeCategory(Category category) {
Set<Product> changedProducts = new LinkedHashSet<>();

if (category != null) {
final Set<Product> existingProducts = productsTable.getRawEntities();
for (Product existingProduct : existingProducts) {
Expand All @@ -141,13 +143,18 @@ public void removeCategory(Category category) {
final boolean removed = linkedCategories.remove(category);
if (removed) {
existingProduct.setCategories(linkedCategories);
changedProducts.add(existingProduct);
}
}
}

categoriesTable.remove(category);

notifyEntityRemoved(category);

for (Product changedProduct : changedProducts) {
notifyEntityChanged(changedProduct);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,30 @@ public void testReconnectBulkSyncDeleteServerProduct() {
assertEquals(0, loadSyncRecords(Product.class).size());
}

@Test(timeout = DEFAULT_TIMEOUT)
public void testReconnectBulkSyncDeleteServerProductWithCategory() {
final DataSnapshot category1 = addServerCategory("category1", 255);
final DataSnapshot product1 = addServerProduct("product1", null, null, null, category1.getKey());

authenticateAndWaitSynchronizer();

disconnect();

product1.getRef().removeValue();
category1.getRef().removeValue();

authenticateAndWaitSynchronizer();

final List<Category> categories = dao.getCategories();
assertEquals(0, categories.size());
assertEquals(0, loadSyncRecords(Category.class).size());

final List<Product> products = dao.getProducts();
assertEquals(0, products.size());
assertEquals(0, loadSyncRecords(Product.class).size());
}


@Test(timeout = DEFAULT_TIMEOUT)
public void testReconnectBulkSyncDeleteServerShopItem() {
final DataSnapshot product1 = addServerProduct("product1", null, null, null);
Expand Down

0 comments on commit fec2875

Please sign in to comment.