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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Jason Parraga
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();
    }

}


--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Reply | Threaded
Open this post in threaded view
|

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

Jason Parraga
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. 

On Wednesday, May 29, 2019 at 12:17:28 PM UTC-7, Jason Parraga wrote:
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("<a href="http://localhost:8182/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Flocalhost%3A8182%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFykZ8PN5LTiwYq3U4ROUDNDAW_-Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Flocalhost%3A8182%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFykZ8PN5LTiwYq3U4ROUDNDAW_-Q&#39;;return true;">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("<a href="http://localhost:8182/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Flocalhost%3A8182%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFykZ8PN5LTiwYq3U4ROUDNDAW_-Q&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Flocalhost%3A8182%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFykZ8PN5LTiwYq3U4ROUDNDAW_-Q&#39;;return true;">http://localhost:8182/");
        Representation output = clientResource.put(new StringRepresentation("\"test\""));
        System.out.println(output.getText());
        assertThat(clientResource.getStatus().getCode(), is(200));
        clientResource.release();
    }

}


--
You received this message because you are subscribed to the Google Groups "Restlet Framework (Discuss)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].