-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the thread safety bug of finishing operation for the span named "…
…SpringCloudGateway/sendRequest" (#555)
- Loading branch information
Showing
6 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../apm/plugin/spring/cloud/gateway/v20x/define/AbstractGateway200EnhancePluginDefineV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define; | ||
|
||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.ClassInstanceMethodsEnhancePluginDefineV2; | ||
|
||
public abstract class AbstractGateway200EnhancePluginDefineV2 extends ClassInstanceMethodsEnhancePluginDefineV2 { | ||
|
||
@Override | ||
protected String[] witnessClasses() { | ||
return new String[] { | ||
"org.springframework.cloud.gateway.config.GatewayAutoConfiguration$1" | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...che/skywalking/apm/plugin/spring/cloud/gateway/v20x/HttpClientRequestInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; | ||
import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; | ||
import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; | ||
import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; | ||
import org.apache.skywalking.apm.plugin.spring.cloud.gateway.v20x.define.EnhanceCacheObject; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import reactor.ipc.netty.http.client.HttpClientResponse; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith(TracingSegmentRunner.class) | ||
public class HttpClientRequestInterceptorTest { | ||
|
||
private HttpClientRequestInterceptor httpClientRequestInterceptor = new HttpClientRequestInterceptor(); | ||
|
||
@Rule | ||
public AgentServiceRule serviceRule = new AgentServiceRule(); | ||
@Rule | ||
public MockitoRule rule = MockitoJUnit.rule(); | ||
|
||
@SegmentStoragePoint | ||
private SegmentStorage segmentStorage; | ||
|
||
private HttpClientResponse httpClientResponse; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
|
||
httpClientResponse = Mockito.mock(HttpClientResponse.class); | ||
HttpResponseStatus httpResponseStatus = Mockito.mock(HttpResponseStatus.class); | ||
|
||
Mockito.when(httpResponseStatus.code()).thenReturn(200); | ||
Mockito.when(httpClientResponse.status()).thenReturn(httpResponseStatus); | ||
} | ||
|
||
@Test | ||
public void testDoAfterSuccessOrError() { | ||
AbstractSpan filterSpan = ContextManager.createLocalSpan("mockFilterSpan"); | ||
filterSpan.prepareForAsync(); | ||
ContextManager.stopSpan(filterSpan); | ||
|
||
AbstractSpan sendSpan = ContextManager.createExitSpan("SpringCloudGateway/sendRequest", "http://127.0.0.1:80"); | ||
sendSpan.prepareForAsync(); | ||
ContextManager.stopSpan(sendSpan); | ||
|
||
EnhanceCacheObject enhanceCacheObject = new EnhanceCacheObject(filterSpan, sendSpan); | ||
enhanceCacheObject = spy(enhanceCacheObject); | ||
|
||
//Test the ContextManager is inactive. | ||
httpClientRequestInterceptor.doAfterSuccessOrError(httpClientResponse, null, null); | ||
verify(enhanceCacheObject, Mockito.times(0)).setSpanFinish(true); | ||
|
||
//Test normal scenario. | ||
httpClientRequestInterceptor.doAfterSuccessOrError(httpClientResponse, null, enhanceCacheObject); | ||
verify(enhanceCacheObject, Mockito.times(1)).setSpanFinish(true); | ||
|
||
//Test the doAfterSuccessOrError method is executed multiple times. | ||
httpClientRequestInterceptor.doAfterSuccessOrError(httpClientResponse, null, enhanceCacheObject); | ||
verify(enhanceCacheObject, Mockito.times(1)).setSpanFinish(true); | ||
} | ||
|
||
} |