Пример #1
0
  public void testHit() {
    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);

    when(storage.load(42)).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(42) == (byte) 42;

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

    verify(storage).size();
    verify(storage, atLeast(2)).load(42);
    verifyNoMoreInteractions(storage);
  }
Пример #2
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);
  }
Пример #3
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);
  }