package org.jgroups.tests;

import org.jgroups.Global;
import org.jgroups.Message;
import org.jgroups.util.RetransmitTable;
import org.jgroups.util.Util;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

/** Tests {@link org.jgroups.util.RetransmitTable}
 * @author Bela Ban
 */
@Test(groups=Global.FUNCTIONAL,sequential=false)
public class RetransmitTableTest {
    static final Message MSG=new Message(null, null, "test");

    public static void testCreation() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        int size=table.size();
        assert size == 0;
        assert table.get(15) == null;
    }


    public static void testAddition() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        addAndGet(table,  0,  "0");
        addAndGet(table,  1,  "1");
        addAndGet(table,  5,  "5");
        addAndGet(table,  9,  "9");
        addAndGet(table, 10, "10");
        addAndGet(table, 11, "11");
        addAndGet(table, 19, "19");
        addAndGet(table, 20, "20");
        addAndGet(table, 29, "29");
        System.out.println("table: " + table.dump());
        assert table.size() == 9;
        assert table.size() == table.computeSize();
        assert table.capacity() == 30;
    }


    public static void testAdditionWithOffset() {
        RetransmitTable table=new RetransmitTable(3, 10, 100);
        addAndGet(table, 100, "100");
        addAndGet(table, 101, "101");
        addAndGet(table, 105, "105");
        addAndGet(table, 109, "109");
        addAndGet(table, 110, "110");
        addAndGet(table, 111, "111");
        addAndGet(table, 119, "119");
        addAndGet(table, 120, "120");
        addAndGet(table, 129, "129");
        System.out.println("table: " + table.dump());
        assert table.size() == 9;
        assert table.capacity() == 30;
    }
    

    public static void testDuplicateAddition() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        addAndGet(table,  0,  "0");
        addAndGet(table,  1,  "1");
        addAndGet(table,  5,  "5");
        addAndGet(table,  9,  "9");
        addAndGet(table, 10, "10");

        assert !table.put(5, new Message());
        assert table.get(5).getObject().equals("5");
        assert table.size() == 5;
    }


    public static void testDumpMatrix() {
        RetransmitTable table=new RetransmitTable(3, 10, 1);
        long[] seqnos={1,3,5,7,9,12,14,16,18,20,21,22,23,24};
        for(long seqno: seqnos)
            table.put(seqno, MSG);
        System.out.println("matrix:\n" + table.dumpMatrix());
    }


    public static void testMassAddition() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        final int NUM_MSGS=10005;
        final Message MSG=new Message(null, null, "hello world");
        for(int i=0; i < NUM_MSGS; i++)
            table.put(i, MSG);
        System.out.println("table = " + table);
        assert table.size() == NUM_MSGS;
        assert table.capacity() == 10010;
    }

    public static void testResize() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        assert table.capacity() == 30;
        addAndGet(table, 30, "30");
        addAndGet(table, 35, "35");
        assert table.capacity() == 40;
        addAndGet(table, 500, "500");
        assert table.capacity() == 510;

        addAndGet(table, 515, "515");
        assert table.capacity() == 520;
    }

    public void testResizeWithPurge() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        for(long i=1; i <= 100; i++)
            addAndGet(table, i, "hello-" + i);
        System.out.println("table: " + table);
        
        // now remove 60 messages
        for(long i=1; i <= 60; i++) {
            Message msg=table.remove(i);
            assert msg.getObject().equals("hello-" + i);
        }
        System.out.println("table after removal of seqno 60: " + table);

        table.purge(50);
        System.out.println("now triggering a resize() by addition of seqno=120");
        addAndGet(table, 120, "120");
        
    }


    public void testResizeWithPurgeAndGetOfNonExistingElement() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        for(long i=0; i < 50; i++)
            addAndGet(table, i, "hello-" + i);
        System.out.println("table: " + table);

        // now remove 15 messages
        for(long i=0; i <= 15; i++) {
            Message msg=table.remove(i);
            assert msg.getObject().equals("hello-" + i);
        }
        System.out.println("table after removal of seqno 15: " + table);

        table.purge(15);
        System.out.println("now triggering a resize() by addition of seqno=55");
        addAndGet(table, 55, "hello-55");

        // now we have elements 40-49 in row 1 and 55 in row 2:
        List<String> list=new ArrayList<String>(20);
        for(int i=16; i < 50; i++)
            list.add("hello-" + i);
        list.add("hello-55");

        for(long i=table.getOffset(); i < table.capacity() + table.getOffset(); i++) {
            Message msg=table.get(i);
            if(msg != null) {
                String message=(String)msg.getObject();
                System.out.println(i + ": " + message);
                list.remove(message);
            }
        }

        System.out.println("table:\n" + table.dumpMatrix());
        assert list.isEmpty() : " list: " + Util.print(list);
    }


    public void testResizeWithPurge2() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        for(long i=0; i < 50; i++)
            addAndGet(table, i, "hello-" + i);
        System.out.println("table = " + table);
        assert table.size() == 50;
        assert table.capacity() == 50;
        assert table.getHighestPurged() == 0;
        assert table.getHighest() == 49;

        table.purge(43);
        addAndGet(table, 52, "hello-52");
        assert table.get(43) == null;

        for(long i=44; i < 50; i++) {
            Message msg=table.get(i);
            assert msg != null && msg.getObject().equals("hello-" + i);
        }

        assert table.get(50) == null;
        assert table.get(51) == null;
        Message msg=table.get(52);
        assert msg != null && msg.getObject().equals("hello-52");
        assert table.get(53) == null;
    }


    public static void testMove() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        for(long i=0; i < 50; i++)
            addAndGet(table, i, "hello-" + i);
        table.purge(49);
        assert table.isEmpty();
        addAndGet(table, 50, "50");
        assert table.size() == 1;
        assert table.capacity() == 50;
    }


    public static void testPurge() {
        RetransmitTable table=new RetransmitTable(5, 10, 0);
        for(long seqno=0; seqno < 25; seqno++)
            table.put(seqno, MSG);

        long[] seqnos={30,31,32,37,38,39, 40,41,42,47,48,49};
        for(long seqno: seqnos)
            table.put(seqno, MSG);

        System.out.println("table (before remove):\n" + table.dump());
        for(long seqno=0; seqno <= 22; seqno++)
            table.remove(seqno);

        System.out.println("\ntable (after remove 22, before purge):\n" + table.dump());
        table.purge(22);
        System.out.println("\ntable: (after purge 22):\n" + table.dump());
        assert table.size() == 2 + seqnos.length;
    }


    public void testCompact() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        for(long i=0; i < 80; i++)
            addAndGet(table, i, "hello-" + i);
        assert table.size() == 80;
        table.purge(59);
        assert table.size() == 20;
        table.compact();
        assert table.size() == 20;
        assert table.capacity() == 40;
    }


    public void testCompactWithAutomaticPurging() {
        RetransmitTable table=new RetransmitTable(3, 10, 0);
        table.setAutomaticPurging(true);
        for(long i=0; i < 80; i++)
             addAndGet(table, i, "hello-" + i);
        assert table.size() == 80;
        for(long i=0; i <= 59; i++)
            table.remove(i);

        assert table.size() == 20;
        table.compact();
        assert table.size() == 20;
        assert table.capacity() == 40;
    }


    protected static void addAndGet(RetransmitTable table, long seqno, String message) {
        boolean added=table.put(seqno, new Message(null, null, message));
        assert added;
        Message msg=table.get(seqno);
        assert msg != null && msg.getObject().equals(message);
    }

}
