001package org.eclipse.aether.named; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.concurrent.TimeUnit; 023import java.util.concurrent.atomic.LongAdder; 024 025import org.junit.Rule; 026import org.junit.Test; 027import org.junit.rules.TestName; 028 029import static org.eclipse.aether.named.support.Retry.retry; 030import static org.hamcrest.MatcherAssert.assertThat; 031import static org.hamcrest.Matchers.equalTo; 032import static org.hamcrest.Matchers.greaterThan; 033 034/** 035 * UT for {@link org.eclipse.aether.named.support.Retry}. 036 */ 037public class RetryTest 038{ 039 private static final long RETRY_SLEEP_MILLIS = 250L; 040 041 @Rule 042 public TestName testName = new TestName(); 043 044 @Test 045 public void happy() throws InterruptedException 046 { 047 LongAdder retries = new LongAdder(); 048 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return "happy"; }, null, "notHappy" ); 049 assertThat( result, equalTo( "happy" ) ); 050 assertThat( retries.sum(), equalTo( 1L ) ); 051 } 052 053 @Test 054 public void notHappy() throws InterruptedException 055 { 056 LongAdder retries = new LongAdder(); 057 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return null; }, null, "notHappy" ); 058 assertThat( result, equalTo( "notHappy" ) ); 059 assertThat( retries.sum(), greaterThan( 1L ) ); 060 } 061 062 @Test 063 public void happyAfterSomeTime() throws InterruptedException 064 { 065 LongAdder retries = new LongAdder(); 066 String result = retry( 1L, TimeUnit.SECONDS, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return retries.sum() == 2 ? "got it" : null; }, null, "notHappy" ); 067 assertThat( result, equalTo( "got it" ) ); 068 assertThat( retries.sum(), equalTo( 2L ) ); 069 } 070 071 @Test 072 public void happyFirstAttempt() throws InterruptedException 073 { 074 LongAdder retries = new LongAdder(); 075 String result = retry( 5, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return "happy"; }, null, "notHappy" ); 076 assertThat( result, equalTo( "happy" ) ); 077 assertThat( retries.sum(), equalTo( 1L ) ); 078 } 079 080 @Test 081 public void notHappyAnyAttempt() throws InterruptedException 082 { 083 LongAdder retries = new LongAdder(); 084 String result = retry( 5, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return null; }, null, "notHappy" ); 085 assertThat( result, equalTo( "notHappy" ) ); 086 assertThat( retries.sum(), equalTo( 5L ) ); 087 } 088 089 @Test 090 public void happyAfterSomeAttempt() throws InterruptedException 091 { 092 LongAdder retries = new LongAdder(); 093 String result = retry( 5, RETRY_SLEEP_MILLIS, () -> { retries.increment(); return retries.sum() == 3 ? "got it" : null; }, null, "notHappy" ); 094 assertThat( result, equalTo( "got it" ) ); 095 assertThat( retries.sum(), equalTo( 3L ) ); 096 } 097}