]> git.basschouten.com Git - openhab-addons.git/blob
2b98162efb38936768c91ec0c2158f4deb3fbcfa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.junit.jupiter.api.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync.ExpiringCacheUpdate;
23
24 /**
25  * Tests cases for {@see ExpiringAsyncCache}
26  *
27  * @author David Graeff - Initial contribution
28  */
29 public class ExpiringCacheAsyncTest {
30     @Test
31     public void testConstructorWrongCacheTime() {
32         assertThrows(IllegalArgumentException.class, () ->
33         // Fail if cache time is <= 0
34         new ExpiringCacheAsync<>(0, () -> {
35         }));
36     }
37
38     @Test
39     public void testConstructorNoRefrehCommand() {
40         assertThrows(IllegalArgumentException.class, () -> new ExpiringCacheAsync<>(2000, null));
41     }
42
43     @Test
44     public void testFetchValue() {
45         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
46         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(2000, u);
47         assertTrue(t.isExpired());
48         // Request a value
49         @SuppressWarnings("unchecked")
50         Consumer<Double> consumer = mock(Consumer.class);
51         t.getValue(consumer);
52         // We expect a call to the updater object
53         verify(u).requestCacheUpdate();
54         // Update the value now
55         t.setValue(10.0);
56         // The value should be valid
57         assertFalse(t.isExpired());
58         // We expect a call to the consumer
59         ArgumentCaptor<Double> valueCaptor = ArgumentCaptor.forClass(Double.class);
60         verify(consumer).accept(valueCaptor.capture());
61         assertEquals(10.0, valueCaptor.getValue(), 0);
62     }
63
64     @Test
65     public void testExpiring() {
66         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
67         @SuppressWarnings("unchecked")
68         Consumer<Double> consumer = mock(Consumer.class);
69
70         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(100, u);
71         t.setValue(10.0);
72         assertFalse(t.isExpired());
73
74         // Request a value
75         t.getValue(consumer);
76         // There should be no call to update the cache
77         verify(u, times(0)).requestCacheUpdate();
78         // Wait
79         try {
80             Thread.sleep(101);
81         } catch (InterruptedException ignored) {
82             return;
83         }
84         // Request a value two times
85         t.getValue(consumer);
86         t.getValue(consumer);
87         // There should be one call to update the cache
88         verify(u, times(1)).requestCacheUpdate();
89         assertTrue(t.isExpired());
90     }
91
92     @Test
93     public void testFetchExpiredValue() {
94         ExpiringCacheUpdate u = mock(ExpiringCacheUpdate.class);
95         ExpiringCacheAsync<Double> t = new ExpiringCacheAsync<>(2000, u);
96         t.setValue(10.0);
97         // We should always be able to get the raw value, expired or not
98         assertEquals(10.0, t.getExpiredValue(), 0);
99         t.invalidateValue();
100         assertTrue(t.isExpired());
101         assertEquals(10.0, t.getExpiredValue(), 0);
102     }
103 }