* simultaneous files without loose any callbacks. This test made the native
* layer to crash at some point during development
*/
public void testTwoFiles() throws Exception
{
final AsynchronousFileImpl controller = new AsynchronousFileImpl(executor, pollerExecutor);
final AsynchronousFileImpl controller2 = new AsynchronousFileImpl(executor, pollerExecutor);
controller.open(FILE_NAME + ".1", 10000);
controller2.open(FILE_NAME + ".2", 10000);
int numberOfLines = 1000;
int size = 1024;
ArrayList<Integer> listResult1 = new ArrayList<Integer>();
ArrayList<Integer> listResult2 = new ArrayList<Integer>();
AtomicInteger errors = new AtomicInteger(0);
ByteBuffer buffer = null;
try
{
CountDownLatch latchDone = new CountDownLatch(numberOfLines);
CountDownLatch latchDone2 = new CountDownLatch(numberOfLines);
buffer = AsynchronousFileImpl.newBuffer(size);
encodeBufer(buffer);
preAlloc(controller, numberOfLines * size);
preAlloc(controller2, numberOfLines * size);
ArrayList<CountDownCallback> list = new ArrayList<CountDownCallback>();
ArrayList<CountDownCallback> list2 = new ArrayList<CountDownCallback>();
for (int i = 0; i < numberOfLines; i++)
{
list.add(new CountDownCallback(latchDone, errors, listResult1, i));
list2.add(new CountDownCallback(latchDone2, errors, listResult2, i));
}
int counter = 0;
Iterator<CountDownCallback> iter2 = list2.iterator();
for (CountDownCallback cb1 : list)
{
CountDownCallback cb2 = iter2.next();
controller.write(counter * size, size, buffer, cb1);
controller2.write(counter * size, size, buffer, cb2);
++counter;
}
latchDone.await();
latchDone2.await();
CountDownCallback.checkResults(numberOfLines, listResult1);
CountDownCallback.checkResults(numberOfLines, listResult2);
for (CountDownCallback callback : list)
{
Assert.assertEquals(1, callback.timesDoneCalled.get());
Assert.assertTrue(callback.doneCalled);
}
for (CountDownCallback callback : list2)
{
Assert.assertEquals(1, callback.timesDoneCalled.get());
Assert.assertTrue(callback.doneCalled);
}
Assert.assertEquals(0, errors.get());
controller.close();
}
finally
{
AsynchronousFileImpl.destroyBuffer(buffer);
try
{
controller.close();
}
catch (Exception ignored)
{
}
try
{
controller2.close();
}
catch (Exception ignored)
{
}
}