View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github.wire;
31  
32  import com.jcabi.github.RandomPort;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.request.FakeRequest;
37  import com.jcabi.http.request.JdkRequest;
38  import com.jcabi.http.response.RestResponse;
39  import java.io.IOException;
40  import java.net.HttpURLConnection;
41  import java.util.concurrent.TimeUnit;
42  import org.hamcrest.MatcherAssert;
43  import org.hamcrest.Matchers;
44  import org.junit.Rule;
45  import org.junit.Test;
46  
47  /**
48   * Test case for {@link RetryCarefulWire}.
49   * Just combines the RetryWire and CarefulWire test cases.
50   * @version $Id: 5261ede5c54680d012cff5b51d738ed886569920 $
51   * @author Chris Rebert (github@chrisrebert.com)
52   * @author Yegor Bugayenko (yegor256@gmail.com)
53   * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
54   */
55  public final class RetryCarefulWireTest {
56      /**
57       * HTTP 200 status reason.
58       */
59      private static final String OK = "OK";
60      /**
61       * Name of GitHub's number-of-requests-remaining rate limit header.
62       */
63      private static final String REMAINING_HEADER = "X-RateLimit-Remaining";
64      /**
65       * The rule for skipping test if there's BindException.
66       * @checkstyle VisibilityModifierCheck (3 lines)
67       */
68      @Rule
69      public final transient RandomPort resource = new RandomPort();
70  
71      /**
72       * RetryCarefulWire can make a few requests before giving up and
73       * can wait until the rate limit resets.
74       * @throws Exception If something goes wrong inside
75       */
76      @Test
77      public void makesMultipleRequestsAndWaitUntilReset() throws Exception {
78          final int threshold = 10;
79          // @checkstyle MagicNumber (2 lines)
80          final long reset = TimeUnit.MILLISECONDS
81              .toSeconds(System.currentTimeMillis()) + 5L;
82          final MkContainer container = new MkGrizzlyContainer()
83              .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
84              .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
85              .next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK)
86                  .withHeader(REMAINING_HEADER, "9")
87                  .withHeader("X-RateLimit-Reset", String.valueOf(reset))
88              )
89              .start(this.resource.port());
90          new JdkRequest(container.home())
91              .through(RetryCarefulWire.class, threshold)
92              .fetch()
93              .as(RestResponse.class)
94              .assertStatus(HttpURLConnection.HTTP_OK);
95          final long now = TimeUnit.MILLISECONDS
96              .toSeconds(System.currentTimeMillis());
97          MatcherAssert.assertThat(now, Matchers.greaterThanOrEqualTo(reset));
98          container.stop();
99      }
100 
101     /**
102      * RetryCarefulWire can tolerate the lack the X-RateLimit-Remaining header.
103      * @throws IOException If some problem inside
104      */
105     @Test
106     public void tolerateMissingRateLimitRemainingHeader() throws IOException {
107         final int threshold = 11;
108         // @checkstyle MagicNumber (1 lines)
109         new FakeRequest()
110             .withStatus(HttpURLConnection.HTTP_OK)
111             .withReason(OK)
112             .through(RetryCarefulWire.class, threshold)
113             .fetch();
114         MatcherAssert.assertThat(
115             "Did not crash when X-RateLimit-Remaining header was absent",
116             true
117         );
118     }
119 
120     /**
121      * RetryCarefulWire can tolerate the lack the X-RateLimit-Reset header.
122      * @throws IOException If some problem inside
123      */
124     @Test
125     public void tolerateMissingRateLimitResetHeader() throws IOException {
126         final int threshold = 8;
127         // @checkstyle MagicNumber (1 lines)
128         new FakeRequest()
129             .withStatus(HttpURLConnection.HTTP_OK)
130             .withReason(OK)
131             .withHeader(REMAINING_HEADER, "7")
132             .through(RetryCarefulWire.class, threshold)
133             .fetch();
134         MatcherAssert.assertThat(
135             "Did not crash when X-RateLimit-Reset header was absent",
136             true
137         );
138     }
139 }