@Override public CounterConfiguration readObject(ObjectInput input) throws IOException, ClassNotFoundException { byte flags = input.readByte(); CounterType type = decodeType(flags); Builder builder = builder(type); builder.storage(decodeStorage(flags)); builder.initialValue(input.readLong()); switch (type) { case BOUNDED_STRONG: builder.lowerBound(input.readLong()); builder.upperBound(input.readLong()); break; case WEAK: builder.concurrencyLevel(readUnsignedInt(input)); break; default: } return builder.build(); } }
/** * Implementation of abstract method processModelNode * */ @Override void processModelNode(OperationContext context, ModelNode counter, CounterConfiguration.Builder builder) throws OperationFailedException { super.processModelNode(context, counter, builder); ModelNode upperBoundModel = counter.get(ModelKeys.UPPER_BOUND); ModelNode lowerBoundModel = counter.get(ModelKeys.LOWER_BOUND); if (lowerBoundModel.isDefined()) { Long lowerBound = StrongCounterResource.LOWER_BOUND.resolveModelAttribute(context, counter).asLong(); builder.lowerBound(lowerBound); } if (upperBoundModel.isDefined()) { Long upperBound = StrongCounterResource.UPPER_BOUND.resolveModelAttribute(context, counter).asLong(); builder.upperBound(upperBound); } } }
@Override public void testUpperBoundedStrongCounter(Method method) { final Random random = generateRandom(); final String counterName = method.getName(); CounterConfiguration config = builder(CounterType.BOUNDED_STRONG) .initialValue(5) .upperBound(15) .storage(random.nextBoolean() ? Storage.VOLATILE : Storage.PERSISTENT) .build(); doCreationTest(counterName, config); }
/** * Decodes a {@link CounterConfiguration} encoded by {@link #encodeConfiguration(CounterConfiguration, Consumer, * LongConsumer, IntConsumer)}. * * @return the decoded {@link CounterConfiguration}. * @see #encodeConfiguration(CounterConfiguration, Consumer, LongConsumer, IntConsumer) */ public static CounterConfiguration decodeConfiguration(Supplier<Byte> byteSupplier, LongSupplier longSupplier, IntSupplier intSupplier) { byte flags = byteSupplier.get(); CounterType type = decodeType(flags); CounterConfiguration.Builder builder = CounterConfiguration.builder(type); builder.storage(decodeStorage(flags)); switch (type) { case WEAK: builder.concurrencyLevel(intSupplier.getAsInt()); break; case BOUNDED_STRONG: builder.lowerBound(longSupplier.getAsLong()); builder.upperBound(longSupplier.getAsLong()); break; case UNBOUNDED_STRONG: default: break; } builder.initialValue(longSupplier.getAsLong()); return builder.build(); }
@Override public void testListenerWithBounds(Method method) throws InterruptedException { final String counterName = method.getName(); final CounterManager counterManager = counterManagerSupplier.get(); assertTrue(counterManager .defineCounter(counterName, builder(BOUNDED_STRONG).initialValue(0).lowerBound(0).upperBound(20).build())); StrongCounter counter = counterManager.getStrongCounter(counterName); Handle<EventLogger> handle = counter.addListener(new EventLogger()); add(counter, 1, 1); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(20).get()); assertCounterValue(counter, 20); reset(counter); expectException(CounterOutOfBoundsException.class, () -> add(counter, -1, 0)); assertCounterValue(counter, 0); assertNextValidEvent(handle, 0, 1); assertNextEvent(handle, 1, CounterState.VALID, 20, CounterState.UPPER_BOUND_REACHED); assertNextEvent(handle, 20, CounterState.UPPER_BOUND_REACHED, 0, CounterState.VALID); assertNextEvent(handle, 0, CounterState.VALID, 0, CounterState.LOWER_BOUND_REACHED); assertNoEvents(handle); handle.remove(); }
@Override public void testBoundaries(Method method) { final String counterName = method.getName(); final CounterManager counterManager = counterManagerSupplier.get(); assertTrue(counterManager .defineCounter(counterName, builder(BOUNDED_STRONG).initialValue(1).lowerBound(0).upperBound(20).build())); StrongCounter counter = counterManager.getStrongCounter(counterName); assertCounterValue(counter, 1); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(-10).get()); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.addAndGet(30).get()); assertCounterValue(counter, 20); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, -1).get()); assertCounterValue(counter, 20); expectException(ExecutionException.class, CounterOutOfBoundsException.class, () -> counter.compareAndSet(20, 21).get()); }
public void testCompareAndSwapBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertEquals(0, counter.compareAndSwap(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, 2, 3); counter.reset(); assertEquals(0, counter.compareAndSwap(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, -2, -3); counter.reset(); assertEquals(0, counter.compareAndSwap(1, 3)); assertEquals(0, counter.compareAndSwap(1, -3)); }
public void testCompareAndSetBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertTrue(counter.compareAndSet(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, 2, 3); counter.reset(); assertTrue(counter.compareAndSet(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, -2, -3); counter.reset(); assertFalse(counter.compareAndSet(1, 3)); assertFalse(counter.compareAndSet(1, -3)); }
@Test public void testCounters() { CounterManager counterManager = RemoteCounterManagerFactory.asCounterManager(remoteCacheManager); counterManager.defineCounter("c1", CounterConfiguration.builder(CounterType.BOUNDED_STRONG) .upperBound(10) .initialValue(1) .build()); counterManager.defineCounter("c2", CounterConfiguration.builder(CounterType.WEAK) .initialValue(5) .build()); SyncStrongCounter c1 = counterManager.getStrongCounter("c1").sync(); SyncWeakCounter c2 = counterManager.getWeakCounter("c2").sync(); assertEquals(1, c1.getValue()); assertEquals(5, c2.getValue()); }
public void testSimpleThreshold(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-1).upperBound(1).build()); StrongTestCounter counter = new StrongTestCounter(counterManager.getStrongCounter(method.getName())); addAndAssertResult(counter, 1, 1); assertOutOfBoundsAdd(counter, 1, 1); addAndAssertResult(counter, -1, 0); addAndAssertResult(counter, -1, -1); assertOutOfBoundsAdd(counter, -1, -1); counter.reset(); assertOutOfBoundsAdd(counter, 2, 1); assertOutOfBoundsAdd(counter, -3, -1); }
@Override protected StrongTestCounter createCounter(CounterManager counterManager, String counterName, long initialValue) { counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(Long.MIN_VALUE) .upperBound(Long.MAX_VALUE).initialValue(initialValue).build()); return new StrongTestCounter(counterManager.getStrongCounter(counterName)); }
public void testGetConfiguration() { assertConfiguration("A", createWeakCounterProperties(), builder(CounterType.WEAK).initialValue(10).concurrencyLevel(1).build()); assertConfiguration("B", createUnboundedCounterProperties(), builder(CounterType.UNBOUNDED_STRONG).initialValue(5).build()); assertConfiguration("C", createBoundedCounterProperties(), builder(CounterType.BOUNDED_STRONG).initialValue(5).lowerBound(0).upperBound(10).build()); }
@Override public void testBoundedStrongCounter(Method method) { final Random random = generateRandom(); final String counterName = method.getName(); CounterConfiguration config = builder(CounterType.BOUNDED_STRONG) .initialValue(15) .lowerBound(5) .upperBound(20) .storage(random.nextBoolean() ? Storage.VOLATILE : Storage.PERSISTENT) .build(); doCreationTest(counterName, config); }
@Override protected List<CounterConfiguration> configurationToTest() { return Arrays.asList( CounterConfiguration.builder(CounterType.BOUNDED_STRONG).initialValue(10).lowerBound(1).build(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).initialValue(-10).upperBound(1).build(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).initialValue(1).upperBound(2).upperBound(2) .build(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).build() ); }
public void testInvalidEqualsUpperAndLowerBoundInManager() { TestingUtil.withCacheManager(DefaultCacheManager::new, cacheManager -> { CounterManager manager = asCounterManager(cacheManager); CounterConfiguration cfg = CounterConfiguration.builder(CounterType.BOUNDED_STRONG).initialValue(10) .lowerBound(10).upperBound(10).build(); Exceptions.expectException(CounterConfigurationException.class, () -> manager.defineCounter("invalid", cfg)); }); }
@Override public TestCounter getCounter(EmbeddedCacheManager manager, String counterName) { CounterManager counterManager = asCounterManager(manager); counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.BOUNDED_STRONG).upperBound(Long.MAX_VALUE) .lowerBound(Long.MIN_VALUE) .build()); return new StrongTestCounter(counterManager.getStrongCounter(counterName)); } },
public void testGetValueAndReset() { checkValueAndReset("A", builder(CounterType.WEAK).initialValue(10).build(), 20, s -> addToWeakCounter(s, 10), this::resetWeakCounter); checkValueAndReset("B", builder(CounterType.UNBOUNDED_STRONG).initialValue(-10).build(), 5, s -> addToStrongCounter(s, 15, false), this::resetStrongCounter); checkValueAndReset("C", builder(CounterType.BOUNDED_STRONG).initialValue(1).lowerBound(0).upperBound(2).build(), 2, s -> addToStrongCounter(s, 3, true), this::resetStrongCounter); }
private StrongCounter createCounter(CounterManager counterManager, String counterName, long min, long max) { counterManager.defineCounter(counterName, CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(min).upperBound(max).build()); return counterManager.getStrongCounter(counterName); } }
@Override void define(CounterManager manager, String name) { manager.defineCounter(name, builder(CounterType.BOUNDED_STRONG).initialValue(10).lowerBound(0).upperBound(20).build()); }
@Override List<CounterConfiguration> configurationsToTest() { return asList( builder(UNBOUNDED_STRONG).initialValue(5).build(), builder(BOUNDED_STRONG).initialValue(0).lowerBound(-1).upperBound(1).build() ); }