@Test public void testReInitialized() { ShadowLooper shadowLooper = Shadows.shadowOf(Looper.getMainLooper()); Handler handler = new Handler(); String filePath = createRandomFile().getPath(); String filePath1 = createRandomFile().getPath(); SerializationStrategy<Data, Batch<Data>> serializationStrategy = new GsonSerializationStrategy<>(); TapePersistenceStrategy<Data> persistenceStrategy = new TapePersistenceStrategy<>(filePath1, serializationStrategy); Context context = RuntimeEnvironment.application; SizeTimeBatchingStrategy sizeTimeBatchingStrategy = new SizeTimeBatchingStrategy(persistenceStrategy, 2, 5000); NetworkBatchListener batchListener = new NetworkBatchListener() { @Override public void performNetworkRequest(Batch batch, ValueCallback callback) { callback.onReceiveValue(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(false, 500)); } }; NetworkPersistedBatchReadyListener batchReadyListener = new NetworkPersistedBatchReadyListener(context, filePath, serializationStrategy, handler, batchListener, 2, 50, 50, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, null); BatchController batchController = new BatchManager.Builder() .setSerializationStrategy(serializationStrategy) .setBatchingStrategy(sizeTimeBatchingStrategy) .setHandler(handler) .setOnBatchReadyListener(batchReadyListener) .build(context); ArrayList<Data> firstBatch = Utils.fakeCollection(2); ArrayList<Data> secondBatch = Utils.fakeCollection(1); ArrayList<Data> thirdBatch = Utils.fakeCollection(1); ArrayList<Data> fourthBatch = Utils.fakeCollection(1); ArrayList<Data> fifthBatch = Utils.fakeCollection(1); batchController.addToBatch(firstBatch); shadowLooper.runToEndOfTasks(); batchController.addToBatch(secondBatch); shadowLooper.runToEndOfTasks(); batchController.addToBatch(thirdBatch); shadowLooper.runToEndOfTasks(); ObjectQueue<Batch<Data>> oldQueueFile = batchReadyListener.getQueueFile(); Assert.assertTrue(oldQueueFile.size() == 3); batchReadyListener.setQueueFile(oldQueueFile); final ArrayList outputData = new ArrayList(); NetworkBatchListener batchListener2 = new NetworkBatchListener() { @Override public void performNetworkRequest(Batch batch, ValueCallback callback) { callback.onReceiveValue(new NetworkPersistedBatchReadyListener.NetworkRequestResponse(true, 200)); outputData.addAll(batch.getDataCollection()); } @Override public boolean isNetworkConnected(Context context) { return true; } }; persistenceStrategy = new TapePersistenceStrategy<>(filePath1, serializationStrategy); sizeTimeBatchingStrategy = new SizeTimeBatchingStrategy(persistenceStrategy, 2, 5000); NetworkBatchListener batchListener2Spy = spy(batchListener2); batchReadyListener = new NetworkPersistedBatchReadyListener(context, filePath, serializationStrategy, handler, batchListener2Spy, 2, 50, 50, TrimPersistedBatchReadyListener.MODE_TRIM_AT_START, null); serializationStrategy = new GsonSerializationStrategy<>(); batchController = new BatchManager.Builder() .setSerializationStrategy(serializationStrategy) .setBatchingStrategy(sizeTimeBatchingStrategy) .setHandler(handler) .setOnBatchReadyListener(batchReadyListener) .build(context); batchController.addToBatch(fourthBatch); batchController.addToBatch(fifthBatch); shadowLooper.runToEndOfTasks(); verify(batchListener2Spy, times(4)).performNetworkRequest(any(Batch.class), any(ValueCallback.class)); Assert.assertTrue(outputData.size() == 6); }
@Override public void addToBatch(final Collection<E> dataCollection) { handler.post(new Runnable() { @Override public void run() { assignEventIds(dataCollection); if (batchingStrategy.isInitialized()) { batchingStrategy.onDataPushed(dataCollection); batchingStrategy.flush(false); } else { throw new IllegalAccessError("BatchingStrategy is not initialized"); } } }); }
BatchManager implements BatchController<E, T> { @Override public void addToBatch(final Collection<E> dataCollection) { handler.post(new Runnable() { @Override public void run() { assignEventIds(dataCollection); if (batchingStrategy.isInitialized()) { batchingStrategy.onDataPushed(dataCollection); batchingStrategy.flush(false); } else { throw new IllegalAccessError("BatchingStrategy is not initialized"); } } }); } }
BatchManager implements BatchController<E, T> { @Override public void addToBatch(final Collection<E> dataCollection) { handler.post(new Runnable() { @Override public void run() { assignEventIds(dataCollection); if (batchingStrategy.isInitialized()) { batchingStrategy.onDataPushed(dataCollection); batchingStrategy.flush(false); } else { throw new IllegalAccessError("BatchingStrategy is not initialized"); } } }); } protected  BatchManager(Builder builder, final Context context); }
BatchManager implements BatchController<E, T> { @Override public void addToBatch(final Collection<E> dataCollection) { handler.post(new Runnable() { @Override public void run() { assignEventIds(dataCollection); if (batchingStrategy.isInitialized()) { batchingStrategy.onDataPushed(dataCollection); batchingStrategy.flush(false); } else { throw new IllegalAccessError("BatchingStrategy is not initialized"); } } }); } protected  BatchManager(Builder builder, final Context context); @Override void addToBatch(final Collection<E> dataCollection); @Override Handler getHandler(); @Override SerializationStrategy<E, T> getSerializationStrategy(); @Override void flush(final boolean forced); void initialize(BatchController<E, T> batchController, Context context,
                           OnBatchReadyListener<E, T> onBatchReadyListener, Handler handler); }
BatchManager implements BatchController<E, T> { @Override public void addToBatch(final Collection<E> dataCollection) { handler.post(new Runnable() { @Override public void run() { assignEventIds(dataCollection); if (batchingStrategy.isInitialized()) { batchingStrategy.onDataPushed(dataCollection); batchingStrategy.flush(false); } else { throw new IllegalAccessError("BatchingStrategy is not initialized"); } } }); } protected  BatchManager(Builder builder, final Context context); @Override void addToBatch(final Collection<E> dataCollection); @Override Handler getHandler(); @Override SerializationStrategy<E, T> getSerializationStrategy(); @Override void flush(final boolean forced); void initialize(BatchController<E, T> batchController, Context context,
                           OnBatchReadyListener<E, T> onBatchReadyListener, Handler handler);  }