Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/log/LogTest.java
38828 views
/*1* Copyright (c) 2005, 2012, 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/*24* @test25* @bug 465292226*27* @summary synopsis: ReliableLog.update should pad records to 4-byte28* boundaries29* @author Ann Wollrath30*31* @run main/othervm/timeout=240 LogTest32*/3334import sun.rmi.log.LogHandler;35import sun.rmi.log.ReliableLog;36import java.io.FileDescriptor;37import java.io.FileNotFoundException;38import java.io.IOException;39import java.io.Serializable;4041public class LogTest {4243private static int crashPoint = 0;44private static boolean spansBoundary = false;45private static ReliableLog log = null;46private static Counter counter = new Counter();4748public static void main(String[] args) throws Exception {4950System.err.println("\nRegression test for bug 4652922\n");5152System.setProperty("sun.rmi.log.class", MyLogFile.class.getName());53//System.setProperty("sun.rmi.log.debug", "true");5455log = new ReliableLog(".", new TestLogHandler(counter), false);5657writeUpdatesCrashAndRecover(10, 1, false, 10);58writeUpdatesCrashAndRecover(10, 1, true, 20);59writeUpdatesCrashAndRecover(10, 2, true, 30);60writeUpdatesCrashAndRecover(9, 2, false, 40);61writeUpdatesCrashAndRecover(9, 3, true, 50);62log.close();63}6465private static void writeUpdatesCrashAndRecover(int updates,66int crashValue,67boolean spans,68int expectedCount)69throws IOException70{71/*72* Write updates73*/74System.err.println("\nwrite updates: " + updates);75for (int i = 0; i < updates; i++) {76counter.increment();77log.update(counter);78}7980/*81* Crash82*/83crashPoint = crashValue;84spansBoundary = spans;85System.err.println("crash during next update on sync #" +86crashPoint +87" (spansBoundary = " + spansBoundary + ")");88try {89System.err.println(90"write one update (update record should " +91((counter.value() + 1 == expectedCount) ? "" : "not ") +92"be complete)");93counter.increment();94log.update(counter);95throw new RuntimeException("failed to reach crashpoint " +96crashPoint);97} catch (IOException e) {98System.err.println("caught IOException; message: " +99e.getMessage());100log.close();101}102103/*104* Recover105*/106log = new ReliableLog(".", new TestLogHandler(null), false);107try {108System.err.println("recover log");109counter = (Counter) log.recover();110System.err.println("recovered counter value: " + counter.value());111if (counter.value() != expectedCount) {112throw new RuntimeException("unexpected counter value " +113counter.value());114}115System.err.println("log recovery successful");116117} catch (IOException e) {118System.err.println("log should recover after crash point");119e.printStackTrace();120throw new RuntimeException(121"log should recover after crash point");122}123124}125126private static class Counter implements Serializable {127private static long serialVersionUID = 1;128private int count = 0;129130Counter() {}131132int increment() {133return ++count;134}135136int value() {137return count;138}139140void update(Counter value) {141if (value.value() < count) {142throw new IllegalStateException(143"bad update (count = " + count + ", value = " + value + ")");144} else {145count = value.value();146}147}148}149150static class TestLogHandler extends LogHandler {151152private final Counter initialState;153154TestLogHandler(Counter initialState) {155this.initialState = initialState;156}157158public Object initialSnapshot() {159if (initialState == null) {160throw new IllegalStateException(161"attempting initialSnapshot with null");162}163return initialState;164}165166public Object applyUpdate(Object update, Object state) {167((Counter) state).update((Counter) update);168return state;169}170}171172public static class MyLogFile extends ReliableLog.LogFile {173174public MyLogFile(String name, String mode)175throws FileNotFoundException, IOException176{177super(name, mode);178}179180protected void sync() throws IOException {181if (crashPoint != 0) {182if (--crashPoint == 0) {183throw new IOException("crash point reached");184}185}186super.sync();187}188189protected boolean checkSpansBoundary(long fp) {190return191crashPoint > 0 ? spansBoundary : super.checkSpansBoundary(fp);192}193}194}195196197