Skip to content

Commit a37f131

Browse files
committed
Added some tests
1 parent a173f7a commit a37f131

File tree

3 files changed

+253
-0
lines changed

3 files changed

+253
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Alen Turkovic
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.alturkovic.lock.retry;
26+
27+
import com.github.alturkovic.lock.Lock;
28+
import com.github.alturkovic.lock.Locked;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.mockito.Mock;
32+
import org.mockito.junit.MockitoJUnitRunner;
33+
import org.springframework.retry.support.RetryTemplate;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.mockito.ArgumentMatchers.eq;
37+
import static org.mockito.Mockito.when;
38+
39+
@RunWith(MockitoJUnitRunner.class)
40+
public class DefaultRetriableLockFactoryTest {
41+
42+
@Mock
43+
private RetryTemplateConverter retryTemplateConverter;
44+
45+
@Mock
46+
private Lock lock;
47+
48+
@Mock
49+
private Locked locked;
50+
51+
@Mock
52+
private RetryTemplate retryTemplate;
53+
54+
@Test
55+
public void shouldGenerateRetriableLock() {
56+
when(retryTemplateConverter.construct(eq(locked))).thenReturn(retryTemplate);
57+
58+
final var factory = new DefaultRetriableLockFactory(retryTemplateConverter);
59+
final var retriableLock = factory.generate(lock, locked);
60+
61+
assertThat(retriableLock.getLock()).isEqualTo(lock);
62+
assertThat(retriableLock.getRetryTemplate()).isEqualTo(retryTemplate);
63+
}
64+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Alen Turkovic
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.alturkovic.lock.retry;
26+
27+
import com.github.alturkovic.lock.Interval;
28+
import com.github.alturkovic.lock.Locked;
29+
import com.github.alturkovic.lock.interval.BeanFactoryAwareIntervalConverter;
30+
import com.github.alturkovic.lock.interval.IntervalConverter;
31+
import org.junit.Test;
32+
import org.springframework.beans.PropertyAccessorFactory;
33+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
34+
import org.springframework.retry.RetryPolicy;
35+
import org.springframework.retry.backoff.FixedBackOffPolicy;
36+
import org.springframework.retry.policy.CompositeRetryPolicy;
37+
import org.springframework.retry.policy.SimpleRetryPolicy;
38+
import org.springframework.retry.policy.TimeoutRetryPolicy;
39+
import org.springframework.retry.support.RetryTemplate;
40+
41+
import static org.assertj.core.api.Assertions.assertThat;
42+
43+
public class DefaultRetryTemplateConverterTest {
44+
45+
private IntervalConverter intervalConverter = new BeanFactoryAwareIntervalConverter(new DefaultListableBeanFactory());
46+
47+
@Test
48+
@Locked
49+
public void shouldConstructDefaultRetryTemplate() {
50+
final var locked = new Object() {}.getClass().getEnclosingMethod().getAnnotation(Locked.class);
51+
final var converter = new DefaultRetryTemplateConverter(intervalConverter);
52+
final var retryTemplate = converter.construct(locked);
53+
54+
assertRetryTemplateConstruction(retryTemplate, 1000L, 50L);
55+
}
56+
57+
@Test
58+
@Locked(retry = @Interval("100"), timeout = @Interval("2000"))
59+
public void shouldConstructCustomizedRetryTemplate() {
60+
final var locked = new Object() {}.getClass().getEnclosingMethod().getAnnotation(Locked.class);
61+
final var converter = new DefaultRetryTemplateConverter(intervalConverter);
62+
final var retryTemplate = converter.construct(locked);
63+
64+
assertRetryTemplateConstruction(retryTemplate, 2000L, 100L);
65+
}
66+
67+
// is there a better way to test the RetryTemplate construction?
68+
private void assertRetryTemplateConstruction(final RetryTemplate retryTemplate, final long timeout, final long backOff) {
69+
final var wrapper = PropertyAccessorFactory.forDirectFieldAccess(retryTemplate);
70+
71+
assertThat(wrapper.getPropertyValue("retryPolicy")).isInstanceOf(CompositeRetryPolicy.class);
72+
assertThat((RetryPolicy[]) wrapper.getPropertyValue("retryPolicy.policies"))
73+
.hasSize(2)
74+
.anyMatch(policy1 -> {
75+
if (policy1 instanceof TimeoutRetryPolicy) {
76+
final var timeoutRetryPolicy = (TimeoutRetryPolicy) policy1;
77+
assertThat(timeoutRetryPolicy.getTimeout()).isEqualTo(timeout);
78+
return true;
79+
}
80+
return false;
81+
})
82+
.anyMatch(policy2 -> {
83+
if (policy2 instanceof SimpleRetryPolicy) {
84+
final var simpleRetryPolicy = (SimpleRetryPolicy) policy2;
85+
assertThat(simpleRetryPolicy.getMaxAttempts()).isEqualTo(Integer.MAX_VALUE);
86+
return true;
87+
}
88+
return false;
89+
});
90+
91+
assertThat(wrapper.getPropertyValue("backOffPolicy")).isInstanceOf(FixedBackOffPolicy.class);
92+
assertThat(((FixedBackOffPolicy) wrapper.getPropertyValue("backOffPolicy")).getBackOffPeriod()).isEqualTo(backOff);
93+
}
94+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Alen Turkovic
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.github.alturkovic.lock.retry;
26+
27+
import com.github.alturkovic.lock.Lock;
28+
import com.github.alturkovic.lock.exception.LockNotAvailableException;
29+
import java.util.List;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.mockito.Mock;
33+
import org.mockito.junit.MockitoJUnitRunner;
34+
import org.springframework.retry.policy.NeverRetryPolicy;
35+
import org.springframework.retry.policy.SimpleRetryPolicy;
36+
import org.springframework.retry.support.RetryTemplate;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.ArgumentMatchers.anyList;
40+
import static org.mockito.ArgumentMatchers.anyLong;
41+
import static org.mockito.ArgumentMatchers.anyString;
42+
import static org.mockito.Mockito.times;
43+
import static org.mockito.Mockito.verify;
44+
import static org.mockito.Mockito.when;
45+
46+
@RunWith(MockitoJUnitRunner.class)
47+
public class RetriableLockTest {
48+
49+
@Mock
50+
private Lock lock;
51+
52+
@Test
53+
public void shouldNotRetryWhenFirstAttemptIsSuccessful() {
54+
when(lock.acquire(anyList(), anyString(), anyLong()))
55+
.thenReturn("abc");
56+
57+
final var retryTemplate = new RetryTemplate();
58+
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
59+
60+
final var retriableLock = new RetriableLock(lock, retryTemplate);
61+
final var token = retriableLock.acquire(List.of("key"), "defaultStore", 1000L);
62+
63+
assertThat(token).isEqualTo("abc");
64+
}
65+
66+
@Test
67+
public void shouldRetryWhenFirstAttemptIsNotSuccessful() {
68+
when(lock.acquire(anyList(), anyString(), anyLong()))
69+
.thenReturn(null)
70+
.thenReturn("abc");
71+
72+
final var retryTemplate = new RetryTemplate();
73+
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
74+
75+
final var retriableLock = new RetriableLock(lock, retryTemplate);
76+
final var token = retriableLock.acquire(List.of("key"), "defaultStore", 1000L);
77+
78+
assertThat(token).isEqualTo("abc");
79+
verify(lock, times(2)).acquire(anyList(), anyString(), anyLong());
80+
}
81+
82+
@Test(expected = LockNotAvailableException.class)
83+
public void shouldFailRetryWhenFirstAttemptIsNotSuccessful() {
84+
when(lock.acquire(anyList(), anyString(), anyLong()))
85+
.thenReturn(null);
86+
87+
final var retryTemplate = new RetryTemplate();
88+
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
89+
90+
final var retriableLock = new RetriableLock(lock, retryTemplate);
91+
final var token = retriableLock.acquire(List.of("key"), "defaultStore", 1000L);
92+
93+
assertThat(token).isEqualTo("abc");
94+
}
95+
}

0 commit comments

Comments
 (0)