Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/GetMacAddress.java
38811 views
1
/*
2
* Copyright (c) 2017, 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
/*
25
* @test
26
* @bug 8182672
27
* @summary Java 8u121 on Linux intermittently returns null for MAC address
28
*/
29
30
import java.net.NetworkInterface;
31
import java.util.ArrayList;
32
import java.util.Collections;
33
import java.util.List;
34
import java.util.concurrent.Callable;
35
import java.util.concurrent.ExecutorService;
36
import java.util.concurrent.Executors;
37
import java.util.concurrent.Future;
38
import java.util.concurrent.Phaser;
39
import java.util.function.Predicate;
40
import java.util.stream.Collectors;
41
import java.util.stream.Stream;
42
43
public class GetMacAddress implements Callable<Exception> {
44
static final int NUM_THREADS = 5;
45
static final int NUM_ITERS = 100;
46
static volatile boolean failed; // false
47
48
final String threadName;
49
final NetworkInterface ni;
50
final Phaser startingGate;
51
52
public GetMacAddress(NetworkInterface ni, String name, Phaser phaser) {
53
this.ni = ni;
54
this.threadName = name;
55
this.startingGate = phaser;
56
}
57
58
@Override
59
public Exception call() {
60
int count = 0;
61
startingGate.arriveAndAwaitAdvance();
62
try {
63
for (int i = 0; i < NUM_ITERS; i++) {
64
ni.getMTU();
65
byte[] addr = ni.getHardwareAddress();
66
if (addr == null) {
67
System.out.println(threadName + ". mac id is null");
68
failed = true;
69
}
70
count = count + 1;
71
if (count % 100 == 0) {
72
System.out.println(threadName + ". count is " + count);
73
}
74
}
75
} catch (Exception ex) {
76
System.out.println(threadName + ". Not expecting exception:" + ex.getMessage());
77
failed = true;
78
return ex;
79
}
80
return null;
81
}
82
83
static final Predicate<NetworkInterface> hasHardwareAddress = ni -> {
84
try {
85
if (ni.getHardwareAddress() == null) {
86
System.out.println("Not testing null addr: " + ni.getName());
87
return false;
88
}
89
} catch (Exception ex) {
90
System.out.println("Not testing: " + ni.getName() +
91
" " + ex.getMessage());
92
return false;
93
}
94
return true;
95
};
96
97
public static Stream<NetworkInterface> getNetworkInterfacesAsStream() throws Exception {
98
// JDK 9 and later
99
//return NetworkInterface.networkInterfaces();
100
// pre JDK 9
101
return Collections.list(NetworkInterface.getNetworkInterfaces()).stream();
102
}
103
104
public static void main(String[] args) throws Exception {
105
List<NetworkInterface> toTest = getNetworkInterfacesAsStream()
106
.filter(hasHardwareAddress)
107
.collect(Collectors.toList());
108
109
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
110
111
for (NetworkInterface ni : toTest) {
112
Phaser startingGate = new Phaser(NUM_THREADS);
113
System.out.println("Testing: " + ni.getName());
114
List<Callable<Exception>> list = new ArrayList<>();
115
for (int i = 0; i < NUM_THREADS; i++)
116
list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate));
117
List<Future<Exception>> futures = executor.invokeAll(list);
118
for (Future<Exception> f : futures) {
119
if (f.get() != null)
120
f.get().printStackTrace(System.out);
121
}
122
if (failed)
123
break;
124
}
125
executor.shutdownNow();
126
if (!failed) {
127
System.out.println("PASSED - Finished all threads");
128
} else {
129
throw new RuntimeException("Failed");
130
}
131
}
132
}
133
134