package org.mapdb;


import org.junit.Test;

import java.io.File;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public class QueuesTest {



    @Test public void stack_persisted(){
        File f = UtilsTest.tempDbFile();
        DB db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().make();
        Queue<Object> stack = db.getStack("test");
        stack.add("1");
        stack.add("2");
        stack.add("3");
        stack.add("4");

        db.close();
        db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().deleteFilesAfterClose().make();
        stack = db.getStack("test");

        assertEquals("4",stack.poll());
        assertEquals("3",stack.poll());
        assertEquals("2",stack.poll());
        assertEquals("1",stack.poll());
        assertNull(stack.poll());
        db.close();
    }


    @Test public void queue_persisted(){
        File f = UtilsTest.tempDbFile();
        DB db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().make();
        Queue<Object> queue = db.getQueue("test");
        queue.add("1");
        queue.add("2");
        queue.add("3");
        queue.add("4");

        db.close();
        db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().deleteFilesAfterClose().make();
        queue = db.getQueue("test");

        assertEquals("1", queue.poll());
        assertEquals("2", queue.poll());
        assertEquals("3", queue.poll());
        assertEquals("4", queue.poll());
        assertNull(queue.poll());
        db.close();
    }

    @Test public void circular_queue_persisted(){
        //i put disk limit 4 objects ,
        File f = UtilsTest.tempDbFile();
        DB db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().make();
        Queue queue = db.createCircularQueue("test",null, 4);
        //when i put 6 objects to queue
        queue.add(0);
        queue.add(1);
        queue.add(2);
        queue.add(3);
        //now deletes 0 on first
        queue.add(4);
        //now deletes 1
        queue.add(5);

        db.close();
        db = DBMaker.newFileDB(f).transactionDisable().cacheDisable().deleteFilesAfterClose().make();
        queue = db.getCircularQueue("test");

        assertEquals(2, queue.poll());
        assertEquals(3, queue.poll());
        assertEquals(4, queue.poll());
        assertEquals(5, queue.poll());
        assertNull(queue.poll());
        db.close();

    }

    @Test
    public void testMapDb() throws InterruptedException {
        DB database = DBMaker.newMemoryDB().make();
        BlockingQueue<String> queue = database.getQueue( "test-queue" );
        queue.put( "test-value" );
        database.commit();
        assertThat( queue.take(), is( "test-value" ) );
        database.commit();
        database.close();
    }
}
@@ -59,16 +59,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -3007,10 +3007,1080 @@
 );%0A    %7D
+%0A%0A    @Test(timeout=1000)%0A    public void queueTakeRollback() throws IOException, InterruptedException %7B%0A        File f = File.createTempFile(%22mapdb%22,%22aa%22);%0A        %7B%0A            DB db = DBMaker.newFileDB(f).make();%0A            boolean newQueue = !db.exists(%22test%22);%0A            BlockingQueue queue = db.getQueue(%22test%22);%0A            if (newQueue) %7B%0A                queue.add(%22abc%22);%0A                db.commit();%0A            %7D%0A            Object x = queue.take();%0A            db.rollback();%0A            x = queue.take();%0A%0A            System.out.println(%22got it%22);%0A            db.close();%0A        %7D%0A%0A        %7B%0A            DB db = DBMaker.newFileDB(f).make();%0A            boolean newQueue = !db.exists(%22test%22);%0A            BlockingQueue queue = db.getQueue(%22test%22);%0A            if (newQueue) %7B%0A                queue.add(%22abc%22);%0A                db.commit();%0A            %7D%0A            Object x = queue.take();%0A            db.rollback();%0A            x = queue.take();%0A%0A            System.out.println(%22got it%22);%0A            db.commit();%0A            db.close();%0A        %7D%0A    %7D
 %0A%7D

add simple test case from mailing list.