source: josm/trunk/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java@ 11241

Last change on this file since 11241 was 10717, checked in by simon04, 8 years ago

see #11390, see #12890 - Lambda can be replaced with method reference

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