Skip to content

Commit 36e4faf

Browse files
committed
Start tests for the sticky concurrency limited channel.
1 parent 6e8cc51 commit 36e4faf

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

Diff for: dialogue-core/src/main/java/com/palantir/dialogue/core/StickyConcurrencyLimitedChannel.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.dialogue.core;
1818

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

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

34-
private final LimitedChannel delegate;
35+
private final NeverThrowLimitedChannel delegate;
3536
private final CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter;
3637
private final String channelNameForLogging;
3738

38-
StickyConcurrencyLimitedChannel(LimitedChannel delegate, String channelNameForLogging) {
39-
this.delegate = delegate;
40-
this.limiter = new CautiousIncreaseAggressiveDecreaseConcurrencyLimiter(Behavior.STICKY);
39+
@VisibleForTesting
40+
StickyConcurrencyLimitedChannel(
41+
LimitedChannel delegate,
42+
CautiousIncreaseAggressiveDecreaseConcurrencyLimiter limiter,
43+
String channelNameForLogging) {
44+
this.delegate = new NeverThrowLimitedChannel(delegate);
45+
this.limiter = limiter;
4146
this.channelNameForLogging = channelNameForLogging;
4247
}
4348

@@ -58,7 +63,7 @@ public Optional<ListenableFuture<Response>> maybeExecute(
5863
Optional<ListenableFuture<Response>> result = delegate.maybeExecute(
5964
endpoint,
6065
request,
61-
permit.isOnlyInFlight()
66+
permit.isOnlyInFlight() || limitEnforcement == LimitEnforcement.DANGEROUS_BYPASS_LIMITS
6267
? LimitEnforcement.DANGEROUS_BYPASS_LIMITS
6368
: LimitEnforcement.DEFAULT_ENABLED);
6469
if (result.isPresent()) {
@@ -75,7 +80,8 @@ public Optional<ListenableFuture<Response>> maybeExecute(
7580
}
7681

7782
static LimitedChannel create(LimitedChannel channel, String channelName) {
78-
return new StickyConcurrencyLimitedChannel(channel, channelName);
83+
return new StickyConcurrencyLimitedChannel(
84+
channel, new CautiousIncreaseAggressiveDecreaseConcurrencyLimiter(Behavior.STICKY), channelName);
7985
}
8086

8187
private void logPermitAcquired() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.dialogue.core;
18+
19+
import com.google.common.util.concurrent.SettableFuture;
20+
import com.palantir.dialogue.Endpoint;
21+
import com.palantir.dialogue.Request;
22+
import com.palantir.dialogue.Response;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.jupiter.MockitoExtension;
28+
29+
@ExtendWith(MockitoExtension.class)
30+
public final class StickyConcurrencyLimitedChannelTest {
31+
32+
@Mock
33+
private Endpoint endpoint;
34+
35+
@Mock
36+
private Request request;
37+
38+
@Mock
39+
private LimitedChannel delegate;
40+
41+
@Mock
42+
private CautiousIncreaseAggressiveDecreaseConcurrencyLimiter mockLimiter;
43+
44+
@Mock
45+
private Response response;
46+
47+
private LimitedChannel channel;
48+
private SettableFuture<Response> responseFuture;
49+
50+
@BeforeEach
51+
public void before() {
52+
channel = new StickyConcurrencyLimitedChannel(delegate, mockLimiter, "test");
53+
}
54+
55+
@Test
56+
public void testLimiterAvailable_successfulRequest_host() {}
57+
}

0 commit comments

Comments
 (0)