1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertSame;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
15 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
16 | import org.openstreetmap.josm.tools.MemoryManager.MemoryHandle;
|
---|
17 | import org.openstreetmap.josm.tools.MemoryManager.NotEnoughMemoryException;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Tests the {@link MemoryManager} class.
|
---|
23 | * @author Michael Zangl
|
---|
24 | */
|
---|
25 | public class MemoryManagerTest {
|
---|
26 | /**
|
---|
27 | * Base test environment
|
---|
28 | */
|
---|
29 | @RegisterExtension
|
---|
30 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
31 | public JOSMTestRules test = new JOSMTestRules().memoryManagerLeaks();
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Test {@link MemoryManager#allocateMemory(String, long, java.util.function.Supplier)}
|
---|
35 | * @throws NotEnoughMemoryException if there is not enough memory
|
---|
36 | */
|
---|
37 | @Test
|
---|
38 | void testUseMemory() throws NotEnoughMemoryException {
|
---|
39 | MemoryManager manager = MemoryManager.getInstance();
|
---|
40 | long available = manager.getAvailableMemory();
|
---|
41 | assertTrue(available < Runtime.getRuntime().maxMemory());
|
---|
42 | assertEquals(available, manager.getMaxMemory());
|
---|
43 |
|
---|
44 | Object o1 = new Object();
|
---|
45 | MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, () -> o1);
|
---|
46 | assertEquals(available - 10, manager.getAvailableMemory());
|
---|
47 | assertSame(o1, testMemory.get());
|
---|
48 | assertEquals(10, testMemory.getSize());
|
---|
49 | assertTrue(testMemory.toString().startsWith("MemoryHandle"));
|
---|
50 |
|
---|
51 | manager.allocateMemory("test2", 10, Object::new);
|
---|
52 | assertEquals(available - 20, manager.getAvailableMemory());
|
---|
53 |
|
---|
54 | testMemory.free();
|
---|
55 | assertEquals(available - 10, manager.getAvailableMemory());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Test that {@link MemoryHandle#get()} checks for use after free.
|
---|
60 | * @throws NotEnoughMemoryException if there is not enough memory
|
---|
61 | */
|
---|
62 | @Test
|
---|
63 | void testUseAfterFree() throws NotEnoughMemoryException {
|
---|
64 | MemoryManager manager = MemoryManager.getInstance();
|
---|
65 | MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
|
---|
66 | testMemory.free();
|
---|
67 | assertThrows(IllegalStateException.class, () -> testMemory.get());
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Test that {@link MemoryHandle#get()} checks for free after free.
|
---|
72 | * @throws NotEnoughMemoryException if there is not enough memory
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | void testFreeAfterFree() throws NotEnoughMemoryException {
|
---|
76 | MemoryManager manager = MemoryManager.getInstance();
|
---|
77 | MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
|
---|
78 | testMemory.free();
|
---|
79 | assertThrows(IllegalStateException.class, () -> testMemory.free());
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Test that too big allocations fail
|
---|
84 | * @throws NotEnoughMemoryException always
|
---|
85 | */
|
---|
86 | @Test
|
---|
87 | void testAllocationFails() throws NotEnoughMemoryException {
|
---|
88 | MemoryManager manager = MemoryManager.getInstance();
|
---|
89 | long available = manager.getAvailableMemory();
|
---|
90 |
|
---|
91 | assertThrows(NotEnoughMemoryException.class, () -> manager.allocateMemory("test", available + 1, () -> {
|
---|
92 | fail("Should not reach");
|
---|
93 | return null;
|
---|
94 | }));
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Test that allocations with null object fail
|
---|
99 | * @throws NotEnoughMemoryException never
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | void testSupplierFails() throws NotEnoughMemoryException {
|
---|
103 | MemoryManager manager = MemoryManager.getInstance();
|
---|
104 |
|
---|
105 | assertThrows(IllegalArgumentException.class, () -> manager.allocateMemory("test", 1, () -> null));
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Test {@link MemoryManager#isAvailable(long)}
|
---|
110 | */
|
---|
111 | @Test
|
---|
112 | void testIsAvailable() {
|
---|
113 | MemoryManager manager = MemoryManager.getInstance();
|
---|
114 | assertTrue(manager.isAvailable(10));
|
---|
115 | assertTrue(manager.isAvailable(100));
|
---|
116 | assertTrue(manager.isAvailable(10));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Test {@link MemoryManager#isAvailable(long)} for negative number
|
---|
121 | * @throws NotEnoughMemoryException never
|
---|
122 | */
|
---|
123 | @Test
|
---|
124 | void testIsAvailableFails() throws NotEnoughMemoryException {
|
---|
125 | MemoryManager manager = MemoryManager.getInstance();
|
---|
126 |
|
---|
127 | assertThrows(IllegalArgumentException.class, () -> manager.isAvailable(-10));
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Test {@link MemoryManager#resetState()}
|
---|
132 | * @throws NotEnoughMemoryException if there is not enough memory
|
---|
133 | */
|
---|
134 | @Test
|
---|
135 | void testResetState() throws NotEnoughMemoryException {
|
---|
136 | MemoryManager manager = MemoryManager.getInstance();
|
---|
137 | assertTrue(manager.resetState().isEmpty());
|
---|
138 |
|
---|
139 | manager.allocateMemory("test", 10, Object::new);
|
---|
140 | manager.allocateMemory("test2", 10, Object::new);
|
---|
141 | assertEquals(2, manager.resetState().size());
|
---|
142 |
|
---|
143 | assertTrue(manager.resetState().isEmpty());
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Test {@link MemoryManager#resetState()}
|
---|
148 | * @throws NotEnoughMemoryException if there is not enough memory
|
---|
149 | */
|
---|
150 | @Test
|
---|
151 | void testResetStateUseAfterFree() throws NotEnoughMemoryException {
|
---|
152 | MemoryManager manager = MemoryManager.getInstance();
|
---|
153 | MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
|
---|
154 |
|
---|
155 | assertFalse(manager.resetState().isEmpty());
|
---|
156 | assertThrows(IllegalStateException.class, () -> testMemory.get());
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Reset the state of the memory manager
|
---|
161 | * @param allowMemoryManagerLeaks If this is set, no exception is thrown if there were leaking entries.
|
---|
162 | */
|
---|
163 | public static void resetState(boolean allowMemoryManagerLeaks) {
|
---|
164 | List<MemoryHandle<?>> hadLeaks = MemoryManager.getInstance().resetState();
|
---|
165 | if (!allowMemoryManagerLeaks) {
|
---|
166 | assertTrue(hadLeaks.isEmpty(), "Memory manager had leaking memory: " + hadLeaks);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|