Example #1
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;
  }
Example #2
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);
  }