Package me.jtalk.accounts.cacher

Source Code of me.jtalk.accounts.cacher.CacherMapTest

/*
* Copyright (C) 2014 Roman Nazarenko <me@jtalk.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package me.jtalk.accounts.cacher;

import org.junit.Test;
import static org.junit.Assert.*;

import me.jtalk.misc.TestTimeSource;

public class CacherMapTest {

  public CacherMapTest() {
  }

  @Test
  public void testRemoveOneEldestEntry() {
    long timeout = 20;
    TestTimeSource time = new TestTimeSource();
    CacherMap<Integer> map = new CacherMap<>(timeout, time);

    time.set(0); // All are new right now

    int KEY_OLD = 1;
    int KEY_NEW_1 = 2;
    int KEY_NEW_2 = 3;

    map.put(KEY_OLD, (long)9 * 1000);
    map.put(KEY_NEW_1, (long)30 * 1000);

    time.set(30 * 1000);

    map.put(KEY_NEW_2, (long)31 * 1000);

    assertEquals(2, map.size());
    assertTrue(map.containsKey(KEY_NEW_1));
    assertTrue(map.containsKey(KEY_NEW_2));
    assertFalse(map.containsKey(KEY_OLD));
  }

  @Test
  public void testRemoveEldestEntryAll() {
    long timeout = 20;
    TestTimeSource time = new TestTimeSource();
    CacherMap<Integer> map = new CacherMap<>(timeout, time);

    time.set(0); // All are new right now

    int KEY_OLD_1 = 1;
    int KEY_OLD_2 = 2;
    int KEY_OLD_3 = 3;

    map.put(KEY_OLD_1, (long)10);
    map.put(KEY_OLD_2, (long)11);

    time.set(33);
    map.put(KEY_OLD_3, (long)12);

    assertTrue(map.isEmpty());
  }

  @Test
  public void testRemoveZeroEldestEntries() {
    long timeout = Long.MAX_VALUE;
    TestTimeSource time = new TestTimeSource();
    CacherMap<Integer> map = new CacherMap<>(timeout, time);

    time.set(31); // All are new right now

    int KEY_NEW_1 = 1;
    int KEY_NEW_2 = 2;
    int KEY_NEW_3 = 3;

    map.put(KEY_NEW_1, (long)10);
    map.put(KEY_NEW_2, (long)11);
    map.put(KEY_NEW_3, (long)12);

    assertEquals(3, map.size());
  }
}
TOP

Related Classes of me.jtalk.accounts.cacher.CacherMapTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.