Esempio n. 1
0
  @Test
  public void testValueUniqueObject() throws Exception {
    OnHeapStore<Serializable, Serializable> store = newStore();

    String key = "key";
    List<String> value = new ArrayList<String>();
    value.add("value");

    store.put(key, (Serializable) value);

    // mutate the value -- should not affect cache
    value.clear();

    ValueHolder<Serializable> valueHolder = store.get(key);
    if (valueHolder.value() == value
        || !valueHolder.value().equals(Collections.singletonList("value"))) {
      throw new AssertionError();
    }
  }
Esempio n. 2
0
  @Test
  public void testKeyCopierCalledOnGetOrComputeIfAbsent() throws Exception {
    LongCopier keyCopier = new LongCopier();
    OnHeapStore<Long, Long> store =
        newStore(
            SystemTimeSource.INSTANCE,
            Expirations.noExpiration(),
            Eviction.none(),
            keyCopier,
            new SerializingCopier<Long>(
                new CompactJavaSerializer<Long>(ClassLoader.getSystemClassLoader())),
            100);

    ValueHolder<Long> computed =
        store.getOrComputeIfAbsent(
            1L,
            new Function<Long, ValueHolder<Long>>() {
              @Override
              public ValueHolder<Long> apply(final Long key) {
                return new AbstractValueHolder<Long>(-1, -1) {
                  @Override
                  public Long value() {
                    return key * 1000L;
                  }

                  @Override
                  protected TimeUnit nativeTimeUnit() {
                    return TimeUnit.MILLISECONDS;
                  }
                };
              }
            });
    assertThat(computed.value(), is(1000L));
    assertThat(keyCopier.copyForWriteCount, is(1));
    assertThat(keyCopier.copyForReadCount, is(0));
  }