Пример #1
0
  public void testSetDuringDependencyNodeOperations() {
    ObjectStorage storage = mock(ObjectStorage.class);

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

    ByteCalculatable calculatable = mock(ByteCalculatable.class);
    MxResource r = mock(MxResource.class);
    when(calculatable.calculate("123")).thenThrow(new ResourceOccupied(r));

    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);

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

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

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

    verify(storage, atLeast(2)).load();
    verifyNoMoreInteractions(storage);
    verify(calculatable).calculate("123");
    verifyNoMoreInteractions(calculatable);
  }
Пример #2
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);
  }
Пример #3
0
  public void testResetStat() {
    ObjectStorage storage = mock(ObjectStorage.class);

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

    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);

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

    assert cache.getOrCreate() == (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();
    verify(storage).save((byte) 42);
    verifyNoMoreInteractions(storage);
  }