public void testHit() {
    DoubleObjectStorage storage = mock(DoubleObjectStorage.class);

    DoubleCharacterCache cache =
        (DoubleCharacterCache)
            Wrapping.getFactory(
                    new Signature(double.class, Object.class),
                    new Signature(double.class, char.class),
                    false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    when(storage.load(42d)).thenReturn('*');
    when(storage.size()).thenReturn(1);

    assert cache.getSize() == 1;
    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate(42d) == '*';

    assert cache.getStatistics().getHits() == 1;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage).size();
    verify(storage, atLeast(2)).load(42d);
    verifyNoMoreInteractions(storage);
  }
  public void testSetDuringDependencyNodeOperations() {
    DoubleObjectStorage storage = mock(DoubleObjectStorage.class);

    when(storage.load(42d)).thenReturn(Storage.UNDEFINED, '*');

    DoubleCharacterCalculatable calculatable = mock(DoubleCharacterCalculatable.class);
    MxResource r = mock(MxResource.class);
    when(calculatable.calculate("123", 42d)).thenThrow(new ResourceOccupied(r));

    DoubleCharacterCache cache =
        (DoubleCharacterCache)
            Wrapping.getFactory(
                    new Signature(double.class, Object.class),
                    new Signature(double.class, char.class),
                    false)
                .wrap("123", calculatable, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate(42d) == '*';

    assert cache.getStatistics().getHits() == 1;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage, atLeast(2)).load(42d);
    verifyNoMoreInteractions(storage);
    verify(calculatable).calculate("123", 42d);
    verifyNoMoreInteractions(calculatable);
  }
Esempio n. 3
0
  public void testHit() {
    ObjectStorage storage = mock(ObjectStorage.class);

    ByteCache cache =
        (ByteCache)
            Wrapping.getFactory(
                    new Signature(null, Object.class), new Signature(null, byte.class), false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    when(storage.load()).thenReturn((byte) 42);
    when(storage.size()).thenReturn(1);

    assert cache.getSize() == 1;
    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate() == (byte) 42;

    verify(storage).size();
    verify(storage, atLeast(1)).load();
    verifyNoMoreInteractions(storage);

    assert cache.getStatistics().getHits() == 1;
    assert cache.getStatistics().getMisses() == 0;
  }
  public void testMiss() {
    CharacterObjectStorage storage = mock(CharacterObjectStorage.class);

    when(storage.load('*')).thenReturn(Storage.UNDEFINED);
    when(storage.size()).thenReturn(0);

    CharacterIntCache cache =
        (CharacterIntCache)
            Wrapping.getFactory(
                    new Signature(char.class, Object.class),
                    new Signature(char.class, int.class),
                    false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    assert cache.getSize() == 0;
    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate('*') == 42;

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 1;

    verify(storage).size();
    verify(storage, atLeast(1)).load('*');
    verify(storage).save('*', 42);
    verifyNoMoreInteractions(storage);
  }
  public void testResetStat() {
    ObjectObjectStorage storage = mock(ObjectObjectStorage.class);

    when(storage.load(42)).thenReturn(Storage.UNDEFINED);

    IntByteCache cache =
        (IntByteCache)
            Wrapping.getFactory(
                    new Signature(Object.class, Object.class),
                    new Signature(int.class, byte.class),
                    false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate(42) == (byte) 42;

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 1;

    cache.getStatistics().reset();

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage, atLeast(1)).load(42);
    verify(storage).save(42, (byte) 42);
    verifyNoMoreInteractions(storage);
  }
Esempio n. 6
0
  public void testClear() {
    ObjectStorage storage = mock(ObjectStorage.class);

    ByteCache cache =
        (ByteCache)
            Wrapping.getFactory(
                    new Signature(null, Object.class), new Signature(null, byte.class), false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    cache.clear();

    verify(storage).clear();
    verifyNoMoreInteractions(storage);
  }