|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 8344882 |
| 27 | + * @summary Deallocation failure for temporary buffers |
| 28 | + * @run junit/othervm -XX:MaxDirectMemorySize=32768 UnmeteredTempBuffers |
| 29 | + */ |
| 30 | +import java.io.InputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.ByteBuffer; |
| 33 | +import java.nio.channels.FileChannel; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import static java.nio.file.StandardOpenOption.*; |
| 37 | + |
| 38 | +import org.junit.jupiter.params.ParameterizedTest; |
| 39 | +import org.junit.jupiter.params.provider.ValueSource; |
| 40 | +import static org.junit.jupiter.api.Assertions.*; |
| 41 | + |
| 42 | +public class UnmeteredTempBuffers { |
| 43 | + @ParameterizedTest |
| 44 | + @ValueSource(ints = {16384, 32768, 32769, 65536}) |
| 45 | + void testFileChannel(int cap) throws IOException { |
| 46 | + Path file = Files.createTempFile("prefix", "suffix"); |
| 47 | + try (FileChannel ch = FileChannel.open(file, WRITE, DELETE_ON_CLOSE)) { |
| 48 | + ByteBuffer buf = ByteBuffer.wrap(new byte[cap]); |
| 49 | + try { |
| 50 | + ch.write(buf); |
| 51 | + } catch (OutOfMemoryError oome) { |
| 52 | + throw new RuntimeException(oome); |
| 53 | + } |
| 54 | + } finally { |
| 55 | + Files.deleteIfExists(file); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest |
| 60 | + @ValueSource(ints = {16384, 32768, 32769, 65536}) |
| 61 | + void testInputStream(int cap) throws IOException { |
| 62 | + Path file = Files.createTempFile("prefix", "suffix"); |
| 63 | + try { |
| 64 | + byte[] bytes = new byte[cap]; |
| 65 | + Files.write(file, bytes); |
| 66 | + try (InputStream in = Files.newInputStream(file)) { |
| 67 | + in.read(bytes); |
| 68 | + } catch (OutOfMemoryError oome) { |
| 69 | + throw new RuntimeException(oome); |
| 70 | + } |
| 71 | + } finally { |
| 72 | + Files.delete(file); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments