Skip to content

Commit

Permalink
Earlier after creating the in-memory queue, the data list was not bei…
Browse files Browse the repository at this point in the history
…ng added to the same, due to which while removing the data collection, an no element found exception was thrown.
  • Loading branch information
anirudhramanan committed Apr 5, 2017
1 parent b0270c8 commit 0ab7521
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package com.flipkart.batching.persistence;

import android.support.annotation.VisibleForTesting;

import com.flipkart.batching.core.Batch;
import com.flipkart.batching.core.Data;
import com.flipkart.batching.core.SerializationStrategy;
Expand Down Expand Up @@ -125,9 +127,25 @@ private void tryCreatingQueueFile() {
File file = new File(filePath);
this.queueFile = new LenientFileObjectQueue(file, converter, this);
} catch (IOException e) {
this.queueFile = new InMemoryObjectQueue<E>();
LogUtil.log(TAG, e.getLocalizedMessage());
createInMemoryQueueFile(e);
}
}

@VisibleForTesting
void createInMemoryQueueFile(IOException e) {
this.queueFile = new InMemoryObjectQueue<E>();
/*
* Due to an exception while creating a tape queue file (which may happen due to low available memory), we are creating an in-memory queue file,
* and adding the dataList to the in-memory queue.
*/
for (E data: dataList) {
try {
this.queueFile.add(data);
} catch (IOException e1) {
LogUtil.log(TAG, e1.getLocalizedMessage());
}
}
LogUtil.log(TAG, e.getLocalizedMessage());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.robolectric.annotation.Config;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -99,6 +100,24 @@ public void testInsertHugeData() {
Assert.assertEquals(dataArrayList, persistenceStrategy.getData());
}

@Test
public void testIfQueueCreatedAgain() throws Exception {
SerializationStrategy serializationStrategy = new GsonSerializationStrategy();
serializationStrategy.build();
TapePersistenceStrategy<Data> persistenceStrategy = new TapePersistenceStrategy<>(createRandomFile().getPath(), serializationStrategy);
persistenceStrategy.onInitialized();
//add data list to the persistence
ArrayList<Data> dataArrayList = Utils.fakeCollection(1000);
persistenceStrategy.add(dataArrayList);

//trying to re create the queue file, which may happen due to low memory available to create the queue file
persistenceStrategy.createInMemoryQueueFile(new IOException());

//now try to remove the data from the queue file. Since due to the low memory, we did create an in-memory queue. This will remove the list
//from the in-memory queue file as well.
persistenceStrategy.removeData(dataArrayList);
}

/**
* Initialize the TapePersistenceStrategy
*/
Expand Down

0 comments on commit 0ab7521

Please sign in to comment.