2323import com .basistech .rosette .apimodel .LanguageOptions ;
2424import com .basistech .rosette .apimodel .LanguageResponse ;
2525import com .basistech .rosette .apimodel .Response ;
26- import org .apache .http .impl .client .CloseableHttpClient ;
27- import org .apache .http .impl .client .HttpClients ;
2826import org .junit .jupiter .api .AfterEach ;
2927import org .junit .jupiter .api .BeforeEach ;
3028import org .junit .jupiter .api .Test ;
4038import java .util .Date ;
4139import java .util .List ;
4240import java .util .concurrent .ExecutionException ;
41+ import java .util .concurrent .ExecutorService ;
42+ import java .util .concurrent .Executors ;
4343import java .util .concurrent .Future ;
4444import java .util .concurrent .TimeUnit ;
4545
4646import static org .junit .jupiter .api .Assertions .assertEquals ;
4747import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
48- import static org .junit .jupiter .api .Assertions .assertNull ;
4948import static org .junit .jupiter .api .Assertions .assertTrue ;
5049
5150@ ExtendWith (MockServerExtension .class )
@@ -86,9 +85,11 @@ void successfulRequest() throws ExecutionException, InterruptedException {
8685 RosetteRequest entitiesRequest = this .api .createRosetteRequest (AbstractRosetteAPI .ENTITIES_SERVICE_PATH , entitiesRequestData , EntitiesResponse .class );
8786
8887 //testing the request
89- Future <Response > response = this .api .submitRequest (entitiesRequest );
88+ ExecutorService threadPool = Executors .newFixedThreadPool (1 );
89+ Future <Response > response = threadPool .submit (entitiesRequest );
9090 assertInstanceOf (EntitiesResponse .class , response .get ());
9191 assertEquals (response .get (), entitiesRequest .getResponse ());
92+ threadPool .shutdownNow ();
9293 }
9394
9495
@@ -107,9 +108,11 @@ void errorResponse() throws ExecutionException, InterruptedException {
107108 RosetteRequest entitiesRequest = this .api .createRosetteRequest (AbstractRosetteAPI .ENTITIES_SERVICE_PATH , entitiesRequestData , EntitiesResponse .class );
108109
109110 //testing the request
110- Future <Response > response = this .api .submitRequest (entitiesRequest );
111+ ExecutorService threadPool = Executors .newFixedThreadPool (1 );
112+ Future <Response > response = threadPool .submit (entitiesRequest );
111113 assertInstanceOf (ErrorResponse .class , response .get ());
112114 assertEquals (response .get (), entitiesRequest .getResponse ());
115+ threadPool .shutdownNow ();
113116 }
114117
115118 @ Test
@@ -144,8 +147,9 @@ void testTiming() throws ExecutionException, InterruptedException {
144147 }
145148
146149 //run requests
150+ ExecutorService threadPool = Executors .newFixedThreadPool (7 );
147151 Date d1 = new Date ();
148- List <Future <Response >> responses = this . api . submitRequests (requests );
152+ List <Future <Response >> responses = threadPool . invokeAll (requests );
149153 for (int i = 0 ; i < responses .size (); i ++) {
150154 responses .get (i ).get ();
151155 }
@@ -168,7 +172,7 @@ void testTiming() throws ExecutionException, InterruptedException {
168172
169173
170174 d1 = new Date ();
171- responses = this . api . submitRequests (requests );
175+ responses = threadPool . invokeAll (requests );
172176 for (int i = 0 ; i < responses .size (); i ++) {
173177 responses .get (i ).get ();
174178 }
@@ -178,54 +182,6 @@ void testTiming() throws ExecutionException, InterruptedException {
178182 assertTrue (d2 .getTime () - d1 .getTime () > requests .size () / concurrency * delay ); // running faster than this would suggest it exceeds the maximum concurrency
179183 }
180184
181- private RosetteRequest setupShutdownTest (int shutdownWaitSeconds , int responseDelayMillis , CloseableHttpClient client ) {
182- this .api = new HttpRosetteAPI .Builder ()
183- .url (String .format ("http://localhost:%d/rest/v1" , mockServer .getPort ()))
184- .shutdownWait (shutdownWaitSeconds )
185- .httpClient (client )
186- .build ();
187-
188- //response setup
189- String entitiesResponse = "{\" entities\" : [ { \" type\" : \" ORGANIZATION\" , \" mention\" : \" Securities and Exchange Commission\" , \" normalized\" : \" U.S. Securities and Exchange Commission\" , \" count\" : 1, \" mentionOffsets\" : [ { \" startOffset\" : 4, \" endOffset\" : 38 } ], \" entityId\" : \" Q953944\" , \" confidence\" : 0.39934742, \" linkingConfidence\" : 0.67404154 } ] }" ;
190- setupResponse ("/rest/v1/entities" , entitiesResponse , 200 , responseDelayMillis , 1 );
191-
192- //request setup
193- String entitiesTextData = "The Securities and Exchange Commission today announced the leadership of the agency’s trial unit." ;
194- DocumentRequest <EntitiesOptions > entitiesRequestData = DocumentRequest .<EntitiesOptions >builder ()
195- .content (entitiesTextData )
196- .build ();
197- return this .api .createRosetteRequest (AbstractRosetteAPI .ENTITIES_SERVICE_PATH , entitiesRequestData , EntitiesResponse .class );
198- }
199-
200- @ Test
201- void successfulShutdown () throws IOException {
202- /* creating http client to avoid closing the httpClient of HttpRosetteApi during close()
203- * which automatically closes the pending threads. This way the shutdown behaviour can be tested.*/
204- CloseableHttpClient httpClient = HttpClients .createDefault ();
205- int shutdownWait = 3 ;
206- RosetteRequest request = setupShutdownTest (shutdownWait , 1000 , httpClient );
207- this .api .submitRequest (request );
208- this .api .close ();
209- httpClient .close ();
210-
211- assertInstanceOf (EntitiesResponse .class , request .getResponse ()); //got response successfully
212- }
213-
214- @ Test
215- void timeoutShutdown () throws IOException {
216- /* creating http client to avoid closing the httpClient of HttpRosetteApi during close()
217- * which automatically closes the pending threads. This way the shutdown behaviour can be tested.*/
218- CloseableHttpClient httpClient = HttpClients .createDefault ();
219- int shutdownWait = 3 ;
220- RosetteRequest request = setupShutdownTest (shutdownWait , 5000 , httpClient );
221- this .api .submitRequest (request );
222- this .api .close ();
223-
224- httpClient .close ();
225- assertNull (request .getResponse ()); //didn't get a response, it was forcefully shutdown
226- }
227-
228-
229185 @ AfterEach
230186 void after () throws IOException {
231187 this .api .close ();
0 commit comments