Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/Buffer/ReachabilityTest.java
41149 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 8208362
26
* @summary Tests reachability from source to dependent direct byte buffers
27
* @run testng ReachabilityTest
28
*/
29
30
import org.testng.Assert;
31
import org.testng.annotations.Test;
32
33
import java.lang.ref.Reference;
34
import java.lang.ref.ReferenceQueue;
35
import java.lang.ref.WeakReference;
36
import java.nio.Buffer;
37
import java.nio.ByteBuffer;
38
import java.util.ArrayList;
39
import java.util.List;
40
import java.util.Objects;
41
import java.util.concurrent.CountDownLatch;
42
import java.util.function.UnaryOperator;
43
44
import static java.util.concurrent.TimeUnit.MILLISECONDS;
45
46
public class ReachabilityTest {
47
48
@Test
49
public void testDuplicate() {
50
testReachability(ByteBuffer.allocateDirect(16384),
51
ByteBuffer::duplicate);
52
}
53
54
@Test
55
public void testSlice() {
56
testReachability(ByteBuffer.allocateDirect(16384),
57
ByteBuffer::slice);
58
}
59
60
@Test
61
public void testViewDuplicate() {
62
testReachability(ByteBuffer.allocateDirect(16384),
63
(Buffer b) -> b instanceof ByteBuffer
64
? ((ByteBuffer) b).asIntBuffer()
65
: b.duplicate()
66
);
67
}
68
69
@Test
70
public void testViewSlice() {
71
testReachability(ByteBuffer.allocateDirect(16384),
72
(Buffer b) -> b instanceof ByteBuffer
73
? ((ByteBuffer) b).asIntBuffer()
74
: b.slice()
75
);
76
}
77
78
<T> void testReachability(T t, UnaryOperator<T> b) {
79
WeakReference<T> root = new WeakReference<>(t);
80
81
ReferenceQueue<Object> queue = new ReferenceQueue<>();
82
List<WeakReference<T>> refs = new ArrayList<>();
83
for (int i = 0; i < 1000; i++) {
84
t = b.apply(t);
85
refs.add(new WeakReference<>(t, queue));
86
}
87
t = b.apply(t);
88
89
boolean collected = false;
90
long timeoutMillis = 100L;
91
try {
92
for (int tries = 0; tries < 3 && !collected; tries++) {
93
System.gc();
94
collected = refs.stream().map(Reference::get).anyMatch(Objects::isNull);
95
if (!collected) {
96
collected = queue.remove(timeoutMillis) != null;
97
timeoutMillis *= 4;
98
}
99
}
100
} catch (InterruptedException unexpected) {
101
throw new AssertionError("unexpected InterruptedException");
102
}
103
104
// Some or all of the intermediate values must be GC'ed
105
Assert.assertTrue(collected);
106
// The root should never be GC'ed
107
Assert.assertNotNull(root.get());
108
109
Reference.reachabilityFence(t);
110
}
111
}
112
113