Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public class DividePlugin extends AbstractShenyuPlugin {

private static final String SHORTEST_RESPONSE = "shortestResponse";

private Long beginTime;

@Override
protected String getRawPath(final ServerWebExchange exchange) {
return RequestUrlUtils.getRewrittenRawPath(exchange);
Expand Down Expand Up @@ -134,8 +132,8 @@ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlu
return chain.execute(exchange).doOnSuccess(e -> responseTrigger(upstream
)).doOnError(throwable -> responseTrigger(upstream));
} else if (ruleHandle.getLoadBalance().equals(SHORTEST_RESPONSE)) {
beginTime = System.currentTimeMillis();
return chain.execute(exchange).doOnSuccess(e -> successResponseTrigger(upstream
long beginTime = System.currentTimeMillis();
return chain.execute(exchange).doOnSuccess(e -> successResponseTrigger(upstream, beginTime
));
}
return chain.execute(exchange);
Expand Down Expand Up @@ -193,7 +191,7 @@ private void responseTrigger(final Upstream upstream) {
upstream.setLag(lag);
}

private void successResponseTrigger(final Upstream upstream) {
private void successResponseTrigger(final Upstream upstream, final long beginTime) {
upstream.getSucceededElapsed().addAndGet(System.currentTimeMillis() - beginTime);
upstream.getSucceeded().incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -163,6 +162,7 @@ public void doExecuteTest() {
loadBalancerFactoryMockedStatic.when(() -> LoadBalancerFactory.selector(any(), any(), any()))
.thenReturn(null);
dividePlugin.doExecute(exchange, chain, selectorData, ruleData);
loadBalancerFactoryMockedStatic.close();
// hit `Objects.requireNonNull(shenyuContext)`
exchange.getAttributes().remove(Constants.CONTEXT);
assertThrows(NullPointerException.class, () -> dividePlugin.doExecute(exchange, chain, selectorData, ruleData));
Expand Down Expand Up @@ -233,15 +233,12 @@ public void responseTriggerTest() throws NoSuchMethodException, InstantiationExc
@Test
public void successResponseTriggerTest() throws Exception {
dividePlugin = DividePlugin.class.newInstance();
Field field = DividePlugin.class.getDeclaredField("beginTime");
field.setAccessible(true);
field.set(dividePlugin, 0L);
Method method = DividePlugin.class.getDeclaredMethod("successResponseTrigger", Upstream.class);
Method method = DividePlugin.class.getDeclaredMethod("successResponseTrigger", Upstream.class, long.class);
method.setAccessible(true);
Upstream upstream = Upstream.builder()
.url("upstream")
.build();
method.invoke(dividePlugin, upstream);
method.invoke(dividePlugin, upstream, 0L);
assertEquals(1, upstream.getSucceeded().get());
}

Expand Down
Loading