-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix BaseApiListingResource when concurrent execution
- Loading branch information
Showing
3 changed files
with
97 additions
and
2 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
73 changes: 73 additions & 0 deletions
73
modules/swagger-jaxrs/src/test/java/io/swagger/jaxrs/listing/BaseApiListingResourceTest.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,73 @@ | ||
package io.swagger.jaxrs.listing; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.UriInfo; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.swagger.models.Swagger; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
|
||
public class BaseApiListingResourceTest { | ||
|
||
@Test(description = "it should process everything concurrently") | ||
public void testProcessConcurrent() throws InterruptedException, ExecutionException { | ||
//given | ||
final Application app = mock(Application.class); | ||
final MockApiListingResource underTest = new MockApiListingResource(); | ||
final ServletContext servletContext = mock(ServletContext.class); | ||
final ServletConfig servletConfig = mock(ServletConfig.class); | ||
final HttpHeaders headers = mock(HttpHeaders.class); | ||
final UriInfo uriInfo = mock(UriInfo.class); | ||
final URI uri = URI.create("testUrl"); | ||
|
||
given(app.getClasses()).willReturn(new HashSet<Class<?>>(Arrays.asList(ConcurrentProcessSampleResource.class))); | ||
given(servletConfig.getServletContext()).willReturn(servletContext); | ||
given(uriInfo.getBaseUri()).willReturn(uri); | ||
|
||
//when | ||
ExecutorService executorService = Executors.newFixedThreadPool(2); | ||
List<Future<Swagger>> futures = executorService.invokeAll(Arrays.asList( | ||
callProcess(underTest, app, servletContext, servletConfig, headers, uriInfo), | ||
callProcess(underTest, app, servletContext, servletConfig, headers, uriInfo) | ||
)); | ||
executorService.awaitTermination(1, TimeUnit.SECONDS); | ||
|
||
//then | ||
Swagger swaggerA = futures.get(0).get(); | ||
Swagger swaggerB = futures.get(1).get(); | ||
|
||
Assert.assertNotNull(swaggerA); | ||
Assert.assertNotNull(swaggerB); | ||
Assert.assertEquals(swaggerA, swaggerB); | ||
} | ||
|
||
private Callable<Swagger> callProcess(final MockApiListingResource underTest, final Application app, final ServletContext servletContext, final ServletConfig servletConfig, final HttpHeaders headers, final UriInfo uriInfo) { | ||
return new Callable<Swagger>() { | ||
@Override | ||
public Swagger call() { | ||
return underTest.process(app, servletContext, servletConfig, headers, uriInfo); | ||
} | ||
}; | ||
} | ||
|
||
private class MockApiListingResource extends BaseApiListingResource { | ||
|
||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...swagger-jaxrs/src/test/java/io/swagger/jaxrs/listing/ConcurrentProcessSampleResource.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,22 @@ | ||
package io.swagger.jaxrs.listing; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiParam; | ||
|
||
@Api(value = "foo") | ||
@Path("foo") | ||
public class ConcurrentProcessSampleResource { | ||
@GET | ||
@ApiOperation(value = "test of sample operation") | ||
@Path("/bar") | ||
public Response operationWithReadOnly(@ApiParam(value = "test") @QueryParam("sampleParam") String sampleParam) { | ||
return Response.ok().build(); | ||
} | ||
|
||
} |