View Javadoc
1   package org.eclipse.aether.named;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.TimeUnit;
23  import java.util.concurrent.atomic.LongAdder;
24  
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.rules.TestName;
28  
29  import static org.eclipse.aether.named.support.Retry.retry;
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.hamcrest.Matchers.equalTo;
32  import static org.hamcrest.Matchers.greaterThan;
33  
34  /**
35   * UT for {@link org.eclipse.aether.named.support.Retry}.
36   */
37  public class RetryTest
38  {
39      private static final long RETRY_SLEEP_MILLIS = 250L;
40  
41      @Rule
42      public TestName testName = new TestName();
43  
44      @Test
45      public void happy() throws InterruptedException
46      {
47          LongAdder retries = new LongAdder();
48          String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return "happy"; }, null, "notHappy" );
49          assertThat( result, equalTo( "happy" ) );
50          assertThat( retries.sum(), equalTo( 1L ) );
51      }
52  
53      @Test
54      public void notHappy() throws InterruptedException
55      {
56          LongAdder retries = new LongAdder();
57          String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return null; }, null, "notHappy" );
58          assertThat( result, equalTo( "notHappy" ) );
59          assertThat( retries.sum(), greaterThan( 1L ) );
60      }
61  
62      @Test
63      public void happyOnSomeAttempt() throws InterruptedException
64      {
65          LongAdder retries = new LongAdder();
66          String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return retries.sum() == 2 ? "got it" : null; }, null, "notHappy" );
67          assertThat( result, equalTo( "got it" ) );
68          assertThat( retries.sum(), equalTo( 2L ) );
69      }
70  }