]> git.basschouten.com Git - openhab-addons.git/blob
671b7c1ee1df1ad52b771e9c2575a840c9845e77
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.network.internal.toberemoved.cache;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.Mockito.*;
17
18 import java.util.function.Consumer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync.ExpiringCacheUpdate;
24
25 /**
26  * Tests cases for {@see ExpiringAsyncCache}
27  *
28  * @author David Graeff - Initial contribution
29  */
30 @NonNullByDefault
31 public class ExpiringCacheAsyncTest {
32     @Test
33     public void testConstructorWrongCacheTime() {
34         assertThrows(IllegalArgumentException.class, () ->
35         // Fail if cache time is <= 0
36         new ExpiringCacheAsync<>(0, () -> {
37         }));
38     }
39
40     @Test
41     public void testConstructorNoRefrehCommand() {
42         assertThrows(IllegalArgumentException.class, () -> new ExpiringCacheAsync<>(2000, null));
43     }
44
45     @Test
46     public void testFetchValue() {
47         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
48         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(2000, u);
49         assertTrue(t.isExpired());
50         // Request a value
51         @SuppressWarnings("unchecked")
52         Consumer<Double> consumer = mock(Consumer.class);
53         t.getValue(consumer);
54         // We expect a call to the updater object
55         verify(u).requestCacheUpdate();
56         // Update the value now
57         t.setValue(10.0);
58         // The value should be valid
59         assertFalse(t.isExpired());
60         // We expect a call to the consumer
61         ArgumentCaptor<Double> valueCaptor = ArgumentCaptor.forClass(Double.class);
62         verify(consumer).accept(valueCaptor.capture());
63         assertEquals(10.0, valueCaptor.getValue(), 0);
64     }
65
66     @Test
67     public void testExpiring() {
68         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
69         @SuppressWarnings("unchecked")
70         Consumer<Double> consumer = mock(Consumer.class);
71
72         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(100, u);
73         t.setValue(10.0);
74         assertFalse(t.isExpired());
75
76         // Request a value
77         t.getValue(consumer);
78         // There should be no call to update the cache
79         verify(u, times(0)).requestCacheUpdate();
80         // Wait
81         try {
82             Thread.sleep(101);
83         } catch (InterruptedException ignored) {
84             return;
85         }
86         // Request a value two times
87         t.getValue(consumer);
88         t.getValue(consumer);
89         // There should be one call to update the cache
90         verify(u, times(1)).requestCacheUpdate();
91         assertTrue(t.isExpired());
92     }
93
94     @Test
95     public void testFetchExpiredValue() {
96         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
97         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(2000, u);
98         t.setValue(10.0);
99         // We should always be able to get the raw value, expired or not
100         assertEquals(10.0, t.getExpiredValue(), 0);
101         t.invalidateValue();
102         assertTrue(t.isExpired());
103         assertEquals(10.0, t.getExpiredValue(), 0);
104     }
105 }