6

When you make a rest request using RestAssured, it appears to wait for a response. I need to make a POST request in RestAssured and then while it waits for a response, I need to make a GET request. I'm using Java and RestAssured. I've also tried creating a second thread and it still has not worked. This is for testing purposes.

Here's where it waits:

given().auth().preemptive().basic(userCreds[0], userCreds[1]).contentType("application/json;").and().post(baseURL + resourcePath + this.act.getId() + "/run");

I would like this to run while the previous request is running as well (asynchronous request?):

given().auth().preemptive().basic(userCreds[0], userCreds[1]).when().get(baseURL + resourcePath + this.connect.getId() + "/outgoing/" + fileId);

I've also read that RestAssured supports asynchronous requests, but I've been unsuccessful to get it to work. My project is mavenized. I'm just a lowly QA guy so any help would be greatly appreciated.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
mmyers
  • 61
  • 1
  • 1
  • 3
  • I read up on this some more, I think what I'm looking for is actually considered parallel, not asynchronous. I need the POST request sent, then the GET sent just after the POST is sent/requested, so I suppose they're more like parallel requests? If it helps, I'm using TestNG as my test harness. – mmyers Sep 16 '15 at 20:12

4 Answers4

1

Completable Future can be used to make calls async.

List<CompletableFuture<Pair<String,String>>> futures =
    Arrays.stream(endpoints)
        .map(endpoint -> restCallAsync(endpoint))
        .collect(Collectors.toList());

List<Pair<String,String>> result =
    futures.stream()
        .map(CompletableFuture::join)
        .collect(Collectors.toList());
CompletableFuture<Pair<String,String>> restCallAsync(String endpoint) {
    CompletableFuture<Pair<String,String>> future = CompleteableFuture.supplyAsync(new Supplier<Pair<String,String>>() {
        @Override
        public Pair<String,String> get() {
            Response response = given()
                .relaxedHTTPSValidation()
                .get(endpoint)
                .then()
                .extract().response();
            LOG.info("Endpoint : " + endpoint + " Status : " + response.getStatusCode());
            ...
        }
    });
}
johnVonTrapp
  • 109
  • 5
Amrit
  • 89
  • 1
  • 4
  • 11
  • Hi @TT, This is code snapshot for my private repository. – Amrit Aug 14 '20 at 03:12
  • Hi Amrit. Better to post the code as text instead a picture. Pictures cannot be searched and indexed from search engines. – TT. Aug 14 '20 at 05:32
  • 1
    Sure @TT will keep this in mind going forward, but this isn't copied from anywhere. – Amrit Aug 14 '20 at 06:07
0

RestAssured support only parallel running, not asyncron running.

Gergely A.
  • 404
  • 3
  • 6
0

If I understand your question correctly, you should be able to do what you are planning to do using threads.

   ExecutorService executorService = Executors.newFixedThreadPool(<number of threads>);

    // send 1st 7 requests here
    for (int i = 0; i < 7; i++){
        executorService.execute(new Runnable() {
            @Override
            public void run(){
                try {
                    given().
                        contentType(<>).
                        header(<headers>).
                        body(body).
                    when().
                        post(<URL>);
                } catch (Exception e) {
                    LOGGER.error(e);
                }
            }
        });
    }

    // Wait for all the threads to gracefully shutdown
    try {
        executorService.awaitTermination(<Your timeout>);
    } catch (InterruptedException ie) {
        LOGGER.error("Shutdown interrupted. Will try to shutdown again.", ie);
        executorService.shutdownNow();
    }

Though this won't be asynchronous, this will do what you were planning to do.

avp
  • 2,892
  • 28
  • 34
  • Do I have to use the same number of threads in method `newFixedThreadPool()` and in `for`-loop? – Vitali Plagov Sep 07 '18 at 06:49
  • @VitaliiPlagov No, you need not. They can be different based on your use case of whether you want to run it in caller's thread or a new thread. – avp Sep 09 '18 at 05:03
0

As you mentioned you are using TestMG so write both request in separate test methods and run test in parallel using XML.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" parallel="methods" >
<test name="testGuru">
<classes>
<class name="Test_class_name">
</class>
</classes>
</test>
</suite>
Rohit Patil
  • 1,068
  • 8
  • 9