Skip to content

Commit

Permalink
Start tests for the sticky concurrency limited channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkozlowski committed Jul 20, 2021
1 parent 6e8cc51 commit 36e4faf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.dialogue.core;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.Request;
Expand All @@ -31,13 +32,17 @@ final class StickyConcurrencyLimitedChannel implements LimitedChannel {

private static final Logger log = LoggerFactory.getLogger(StickyConcurrencyLimitedChannel.class);

private final LimitedChannel delegate;
private final NeverThrowLimitedChannel delegate;
private final CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter;
private final String channelNameForLogging;

StickyConcurrencyLimitedChannel(LimitedChannel delegate, String channelNameForLogging) {
this.delegate = delegate;
this.limiter = new CautiousIncreaseAggressiveDecreaseConcurrencyLimiter(Behavior.STICKY);
@VisibleForTesting
StickyConcurrencyLimitedChannel(
LimitedChannel delegate,
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter,
String channelNameForLogging) {
this.delegate = new NeverThrowLimitedChannel(delegate);
this.limiter = limiter;
this.channelNameForLogging = channelNameForLogging;
}

Expand All @@ -58,7 +63,7 @@ public Optional<ListenableFuture<Response>> maybeExecute(
Optional<ListenableFuture<Response>> result = delegate.maybeExecute(
endpoint,
request,
permit.isOnlyInFlight()
permit.isOnlyInFlight() || limitEnforcement == LimitEnforcement.DANGEROUS_BYPASS_LIMITS
? LimitEnforcement.DANGEROUS_BYPASS_LIMITS
: LimitEnforcement.DEFAULT_ENABLED);
if (result.isPresent()) {
Expand All @@ -75,7 +80,8 @@ public Optional<ListenableFuture<Response>> maybeExecute(
}

static LimitedChannel create(LimitedChannel channel, String channelName) {
return new StickyConcurrencyLimitedChannel(channel, channelName);
return new StickyConcurrencyLimitedChannel(
channel, new CautiousIncreaseAggressiveDecreaseConcurrencyLimiter(Behavior.STICKY), channelName);
}

private void logPermitAcquired() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.dialogue.core;

import com.google.common.util.concurrent.SettableFuture;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.Request;
import com.palantir.dialogue.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public final class StickyConcurrencyLimitedChannelTest {

@Mock
private Endpoint endpoint;

@Mock
private Request request;

@Mock
private LimitedChannel delegate;

@Mock
private CautiousIncreaseAggressiveDecreaseConcurrencyLimiter mockLimiter;

@Mock
private Response response;

private LimitedChannel channel;
private SettableFuture<Response> responseFuture;

@BeforeEach
public void before() {
channel = new StickyConcurrencyLimitedChannel(delegate, mockLimiter, "test");
}

@Test
public void testLimiterAvailable_successfulRequest_host() {}
}

0 comments on commit 36e4faf

Please sign in to comment.