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

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

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

    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() == 1;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage, atLeast(2)).load(42);
    verifyNoMoreInteractions(storage);
    verify(calculatable).calculate("123", 42);
    verifyNoMoreInteractions(calculatable);
  }
Пример #2
0
  public void testClear() {
    ObjectObjectStorage storage = mock(ObjectObjectStorage.class);

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

    cache.clear();

    verify(storage).clear();
    verifyNoMoreInteractions(storage);
  }
Пример #3
0
  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);
  }