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