Skip to content

Instantly share code, notes, and snippets.

@ameyawebonise
Created July 27, 2015 15:45
Show Gist options
  • Save ameyawebonise/67f9a755b85147f801f2 to your computer and use it in GitHub Desktop.
Save ameyawebonise/67f9a755b85147f801f2 to your computer and use it in GitHub Desktop.
package com.magnus.newpack.webapp.tracker;
import com.magnus.foundation.spring.AppContext;
import com.magnus.newpack.model.TrackerCustomItem;
import com.magnus.newpack.webapp.action.tracker.CustomTrackerPage;
import mockit.Expectations;
import mockit.Mocked;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import sun.rmi.runtime.Log;
import java.util.*;
import java.util.concurrent.*;
/**
* Created by Webonise on 27/07/15.
*/
public class CustomTrackerPageTests {
private final static Logger LOG = Logger.getLogger(CustomTrackerPageTests.class);
@Mocked
CustomTrackerPage customTrackerPage;
@Test
public void testGetLstOnRetrivalShouldBeNotEmpty(){
int size = 50;
customTrackerPage.setLst(seedListOfSize(size));
Assert.assertEquals(customTrackerPage.getLst().size(),size);
}
@Test(expectedExceptions = ConcurrentModificationException.class)
public void getUsersTrackerItemsWithDataShouldThrowConcurrentModificationException() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(50);
//even though we call setLst the customTrackerPage.lst has empty list
customTrackerPage.setLst(seedListOfSize(50));
LOG.info("Size of the tracker custom list is " + customTrackerPage.getLst().size());
Callable<List<TrackerCustomItem>> taskOne = () -> {
Thread.sleep(500);
return customTrackerPage.getUsersTrackerItemsWithData();
};
List<Callable<List<TrackerCustomItem>>> tasksOne = Collections.nCopies(50, taskOne);
List<Future<List<TrackerCustomItem>>> futuresOne = executor.invokeAll(tasksOne);
for (Future<List<TrackerCustomItem>> future : futuresOne) {
LOG.info("Getting the value of the future");
for(int delay =0; delay < 5000; delay++) {
//purposefully adding delay
}
future.get();
}
}
private List<TrackerCustomItem> seedListOfSize(int size){
List<TrackerCustomItem> list = new ArrayList<>();
for(int index = 0 ;index < size; index++){
TrackerCustomItem item = new TrackerCustomItem();
list.add(item);
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment