ClientResource throws exception when making PUT request (after having made another request with a different ClientResource)

5 views
Skip to first unread message

Jason Parraga

unread,
May 29, 2019, 3:17:28 PM5/29/19
to Restlet Framework (Discuss)
I wrote a unit test that used ClientResource to exercise a PUT Restlet resource. I discovered that the unit test was flaky in our continuous integration and would fail occasionally. After looking into the issue it appears that the client connector claims that the connection has been reset. It seems that anytime I try to make a PUT request after having used any ClientResource previously the request fails on the client side. Here is some trivial code to illustrate the issue I'm having. When the entire unit test suite is run testPut fails. Additionally if I use a repeat rule testPut will fail on the second execution. I can run testGet all day long and it will never fail. 

We are using Restlet 1.2.12 

<dependency>
  <groupId>org.restlet.jse</groupId>
  <artifactId>org.restlet</artifactId>
  <version>${restlet.version}</version>
</dependency>
<dependency>
  <groupId>org.restlet.jse</groupId>
  <artifactId>org.restlet.ext.simple</artifactId>
  <version>${restlet.version}</version>
</dependency>
<dependency>
  <groupId>org.restlet.jse</groupId>
  <artifactId>org.restlet.ext.slf4j</artifactId>
  <version>${restlet.version}</version>
</dependency>
<dependency>
  <groupId>org.restlet.jse</groupId>
  <artifactId>org.restlet.ext.jackson</artifactId>
  <version>${restlet.version}</version>
</dependency>



package org.projectfloodlight.db;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.projectfloodlight.test.RepeatRule;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.representation.Variant;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

public class RestletClientBug {

   
private Server server;

    @Before
    public void setup() throws Exception {
       
// Create the HTTP server and listen on port 8182
        server = new Server(Protocol.HTTP, 8182, FirstServerResource.class);
        server.start();
    }

   
@After
    public void teardown() throws Exception {
       
server.stop();
    }


   
public static class FirstServerResource extends ServerResource {

       
@Override
        @Get
        public String toString() {
           
return "hello, world";
        }

       
@Put
        public String insertDataJson(Representation entity, Variant variant) throws DBException {
           
return "put success";
        }
   
}

   
@Rule
    public final RepeatRule rule = new RepeatRule();

   //@Repeat(times=100)
    @Test
    public void testGet() throws Exception {
       
ClientResource clientResource = new ClientResource("http://localhost:8182/");
        Representation output = clientResource.get();
        System.out.println(output.getText());
        assertThat(clientResource.getStatus().getCode(), is(200));
        clientResource.release();
    }

   
//@Repeat(times=5)
    @Test
    public void testPut() throws Exception {
       
ClientResource clientResource = new ClientResource("http://localhost:8182/");
        Representation output = clientResource.put(new StringRepresentation("\"test\""));
        System.out.println(output.getText());
        assertThat(clientResource.getStatus().getCode(), is(200));
        clientResource.release();
    }

}


Jason Parraga

unread,
May 29, 2019, 3:22:51 PM5/29/19
to Restlet Framework (Discuss)
Here is a link to a slightly more readable version of the code: https://gist.github.com/Sovietaced/5acb8398410d2f2b12909a70c94b5177

Not sure why gist broke the indentation. 
Reply all
Reply to author
Forward
0 new messages