Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/nio/file/Files/InterruptCopy.java
66646 views
1
/*
2
* Copyright (c) 2008, 2021, 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 4313887 6993267
26
* @summary Unit test for Sun-specific ExtendedCopyOption.INTERRUPTIBLE option
27
* @modules jdk.unsupported
28
* @library ..
29
*/
30
31
import java.io.IOException;
32
import java.io.OutputStream;
33
import java.nio.file.Files;
34
import java.nio.file.FileStore;
35
import java.nio.file.Path;
36
import java.nio.file.StandardCopyOption;
37
import java.util.concurrent.Callable;
38
import java.util.concurrent.CountDownLatch;
39
import java.util.concurrent.Executors;
40
import java.util.concurrent.Future;
41
import java.util.concurrent.ScheduledExecutorService;
42
import com.sun.nio.file.ExtendedCopyOption;
43
44
public class InterruptCopy {
45
46
private static final long FILE_SIZE_TO_COPY = 1024L * 1024L * 1024L;
47
private static final int INTERRUPT_DELAY_IN_MS = 50;
48
private static final int CANCEL_DELAY_IN_MS = 10;
49
50
public static void main(String[] args) throws Exception {
51
Path dir = TestUtil.createTemporaryDirectory();
52
try {
53
FileStore store = Files.getFileStore(dir);
54
System.out.format("Checking space (%s)\n", store);
55
long usableSpace = store.getUsableSpace();
56
if (usableSpace < 2*FILE_SIZE_TO_COPY) {
57
System.out.println("Insufficient disk space to run test.");
58
return;
59
}
60
doTest(dir);
61
} finally {
62
TestUtil.removeAll(dir);
63
}
64
}
65
66
static void doTest(Path dir) throws Exception {
67
final Path source = dir.resolve("foo");
68
final Path target = dir.resolve("bar");
69
70
// create source file (don't create it as sparse file because we
71
// require the copy to take a long time)
72
System.out.println("Creating source file...");
73
byte[] buf = new byte[32*1024];
74
long total = 0;
75
try (OutputStream out = Files.newOutputStream(source)) {
76
do {
77
out.write(buf);
78
total += buf.length;
79
} while (total < FILE_SIZE_TO_COPY);
80
}
81
System.out.println("Source file created.");
82
83
ScheduledExecutorService pool =
84
Executors.newSingleThreadScheduledExecutor();
85
86
try {
87
// copy source to target in main thread, interrupting it
88
// after a delay
89
final Thread me = Thread.currentThread();
90
final CountDownLatch interruptLatch = new CountDownLatch(1);
91
Future<?> wakeup = pool.submit(new Runnable() {
92
public void run() {
93
try {
94
interruptLatch.await();
95
Thread.sleep(INTERRUPT_DELAY_IN_MS);
96
} catch (InterruptedException ignore) {
97
}
98
System.out.printf("Interrupting at %d ms...%n",
99
System.currentTimeMillis());
100
me.interrupt();
101
}
102
});
103
try {
104
interruptLatch.countDown();
105
long theBeginning = System.currentTimeMillis();
106
System.out.printf("Copying file at %d ms...%n", theBeginning);
107
Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE);
108
long theEnd = System.currentTimeMillis();
109
System.out.printf("Done copying at %d ms...%n", theEnd);
110
long duration = theEnd - theBeginning;
111
112
// If the copy was interrupted the target file should have been
113
// deleted, so if the file does not exist, then the copy must
114
// have been interrupted without throwing an exception; if the
115
// file exists, then the copy finished before being interrupted
116
// so not throwing an exception is not considered a failure
117
if (Files.notExists(target))
118
throw new RuntimeException("Copy was not interrupted in " +
119
duration + " ms");
120
} catch (IOException e) {
121
boolean interrupted = Thread.interrupted();
122
if (!interrupted)
123
throw new RuntimeException("Interrupt status was not set");
124
System.out.println("Copy failed (this is expected).");
125
}
126
try {
127
wakeup.get();
128
} catch (InterruptedException ignore) { }
129
Thread.interrupted();
130
131
// copy source to target via task in thread pool, interrupting it
132
// after a delay using cancel(true)
133
CountDownLatch cancelLatch = new CountDownLatch(1);
134
Future<Void> result = pool.submit(new Callable<Void>() {
135
public Void call() throws IOException {
136
cancelLatch.countDown();
137
System.out.printf("Copying file at %d ms...%n",
138
System.currentTimeMillis());
139
Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,
140
StandardCopyOption.REPLACE_EXISTING);
141
System.out.printf("Done copying at %d ms...%n",
142
System.currentTimeMillis());
143
return null;
144
}
145
});
146
try {
147
cancelLatch.await();
148
Thread.sleep(CANCEL_DELAY_IN_MS);
149
} catch (InterruptedException ignore) {
150
}
151
if (result.isDone())
152
throw new RuntimeException("Copy finished before cancellation");
153
System.out.printf("Cancelling at %d ms...%n",
154
System.currentTimeMillis());
155
boolean cancelled = result.cancel(true);
156
if (cancelled)
157
System.out.println("Copy cancelled.");
158
else {
159
result.get();
160
throw new RuntimeException("Copy was not cancelled");
161
}
162
} finally {
163
pool.shutdown();
164
}
165
}
166
}
167
168