Skip to content

Commit

Permalink
#279 fix VerboseCommand for cases of none-caching implementation of M…
Browse files Browse the repository at this point in the history
…utableBucketEntry#get
  • Loading branch information
vladimir-bukhtoyarov committed Jul 25, 2022
1 parent 04b39c5 commit 4c3ea89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ public CommandResult<RemoteVerboseResult<T>> execute(MutableBucketEntry mutableE
}

RemoteBucketState state = mutableEntry.get();
CommandResult<T> result = targetCommand.execute(mutableEntry, currentTimeNanos);
RemoteVerboseResult<T> verboseResult = new RemoteVerboseResult<>(currentTimeNanos, result.getResultTypeId(), result.getData(), state);
BucketEntryWrapper entryWrapper = new BucketEntryWrapper(state);
CommandResult<T> result = targetCommand.execute(entryWrapper, currentTimeNanos);
if (entryWrapper.isStateModified()) {
mutableEntry.set(entryWrapper.get());
}

RemoteVerboseResult<T> verboseResult = new RemoteVerboseResult<>(currentTimeNanos, result.getResultTypeId(), result.getData(), entryWrapper.get());
return CommandResult.success(verboseResult, RemoteVerboseResult.SERIALIZATION_HANDLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.*;
import java.util.function.Function;
Expand Down Expand Up @@ -293,4 +294,29 @@ public void testBucketWithNotLazyConfiguration() {
assertFalse(bucket.tryConsume(1));
}

// https://github.com/bucket4j/bucket4j/issues/279
@Test
public void testVerboseBucket() {
int MIN_CAPACITY = 4;
int MAX_CAPACITY = 10;
BucketConfiguration configuration = BucketConfiguration.builder()
.addLimit(Bandwidth.classic(MIN_CAPACITY, Refill.intervally(4, Duration.ofMinutes(20))))
.addLimit(Bandwidth.classic(MAX_CAPACITY, Refill.intervally(10, Duration.ofMinutes(60))))
.build();

K key = generateRandomKey();
Bucket bucket = proxyManager.builder().build(key, configuration);

for (int i = 1; i <= 4; i++) {
VerboseResult<ConsumptionProbe> verboseResult = bucket.asVerbose().tryConsumeAndReturnRemaining(1);
ConsumptionProbe probe = verboseResult.getValue();
long[] availableTokensPerEachBandwidth = verboseResult.getDiagnostics().getAvailableTokensPerEachBandwidth();
System.out.println("Remaining tokens = " + probe.getRemainingTokens());
System.out.println("Tokens per bandwidth = " + Arrays.toString(availableTokensPerEachBandwidth));
assertEquals(MIN_CAPACITY - i, probe.getRemainingTokens());
assertEquals(MIN_CAPACITY - i, availableTokensPerEachBandwidth[0]);
assertEquals(MAX_CAPACITY - i, availableTokensPerEachBandwidth[1]);
}
}

}

0 comments on commit 4c3ea89

Please sign in to comment.