Path: blob/master/test/jdk/java/nio/MappedByteBuffer/MapSyncFail.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Test failure paths for MAP_SYNC FileChannel.map of non-DAX files25* @modules java.base/jdk.internal.misc26* @run main MapSyncFail true27* @run main MapSyncFail false28*/2930import java.io.*;31import java.nio.*;32import java.util.*;33import java.nio.channels.*;34import jdk.nio.mapmode.*;35import jdk.internal.misc.Unsafe;3637public class MapSyncFail {3839public static final int K = 1024;4041public static void main(String[] args) throws Exception {42if (args.length != 1) {43throw new Exception("Expected true or false as argument");44}45boolean is_rw = Boolean.valueOf(args[0]);46FileChannel.MapMode mode = (is_rw ? ExtendedMapMode.READ_WRITE_SYNC : ExtendedMapMode.READ_ONLY_SYNC);47// it is assumed that /tmp is not a DAX file system48File file = File.createTempFile("MapSyncFail", null);49file.deleteOnExit();50long filesize = (8 * K);51try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {52raf.setLength(filesize);53FileChannel fc = raf.getChannel();54MappedByteBuffer mbb = fc.map(mode, 0, filesize);55} catch(IOException ioe) {56// when writeback is enabled for the current os/cpu57// combination the underlying mmap should be attempted and58// the map call should fail with IOException59if (!Unsafe.isWritebackEnabled()) {60throw new Exception("IOException not expected");61}62System.out.println("caught " + ioe);63ioe.printStackTrace();64return;65} catch (UnsupportedOperationException uoe) {66// when writeback is not enabled for the current os/cpu67// combination the mmap should not be attempted and the68// map call should fail with UnsupportedOperationException6970if (Unsafe.isWritebackEnabled()) {71throw new Exception("UnsupportedOperationException not expected");72}73System.out.println("caught " + uoe);74uoe.printStackTrace();75return;76}7778throw new Exception("expected IOException or UnsupportedOperationException");79}80}818283