From 685d2323350b284e0a617d5cec56f75b1033143d Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 9 Dec 2010 10:40:47 +0000 Subject: [PATCH] HSEARCH-644 Enable Byteman for unit tests, introduce a test for HSEARCH-625 --- hibernate-search/pom.xml | 1 + .../ConcurrentMergeErrorHandledTest.java | 87 +++++++++++++++++++ .../ConcurrentMergeErrorTest.bytemanrules | 48 ++++++++++ pom.xml | 9 +- 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 hibernate-search/src/test/java/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorHandledTest.java create mode 100644 hibernate-search/src/test/resources/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorTest.bytemanrules diff --git a/hibernate-search/pom.xml b/hibernate-search/pom.xml index 34f7541c054..58fb4260c6a 100644 --- a/hibernate-search/pom.xml +++ b/hibernate-search/pom.xml @@ -142,6 +142,7 @@ once true + -Djava.net.preferIPv4Stack=true -Djgroups.bind_addr=127.0.0.1 -javaagent:${settings.localRepository}/org/jboss/byteman/byteman/${bytemanVersion}/byteman-${bytemanVersion}.jar=script:${project.build.testOutputDirectory}/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorTest.bytemanrules com.sun.management.jmxremote diff --git a/hibernate-search/src/test/java/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorHandledTest.java b/hibernate-search/src/test/java/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorHandledTest.java new file mode 100644 index 00000000000..42c9f6e38ff --- /dev/null +++ b/hibernate-search/src/test/java/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorHandledTest.java @@ -0,0 +1,87 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat, Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.search.test.errorhandling; + +import junit.framework.Assert; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.search.Environment; +import org.hibernate.search.FullTextSession; +import org.hibernate.search.Search; +import org.hibernate.search.SearchFactory; +import org.hibernate.search.engine.SearchFactoryImplementor; +import org.hibernate.search.exception.ErrorHandler; +import org.hibernate.search.test.Document; +import org.hibernate.search.test.SearchTestCase; + +/** + * This test uses Byteman. Byteman is activated at the invocation of the test + * in this class, and it will have the IndexWriter fail during segments merge, + * which means the commit on the index from our part. + * The tricky issue is that the merger works in a separate thread and some + * inner private classes are involved. + * + * The Byteman rules are defined in a resources file ConcurrentMergeErrorTest.bytemanrules + * + * The goal of the test is to make sure we can catch and report the errors + * thrown by the merger via wathever is configured as Environment.ERROR_HANDLER. + * + * @see Environment#ERROR_HANDLER + * @author Sanne Grinovero + */ +public class ConcurrentMergeErrorHandledTest extends SearchTestCase { + + public void testLuceneMergerErrorHandling() { + SearchFactoryImplementor searchFactory = getSearchFactoryImpl(); + ErrorHandler errorHandler = searchFactory.getErrorHandler(); + Assert.assertTrue( errorHandler instanceof MockErrorHandler ); + MockErrorHandler mockErrorHandler = (MockErrorHandler)errorHandler; + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + session.persist( new Document( + "Byteman Programmers Guider", "Version 1.5.0 Draft", "contains general guidelines to use Byteman" ) ); + transaction.commit(); + session.close(); + String errorMessage = mockErrorHandler.getErrorMessage(); + Assert.assertEquals( "Exception occurred java.io.IOException: Byteman said: your disk is full!\n", errorMessage ); + } + + protected Class[] getAnnotatedClasses() { + return new Class[] { Document.class }; + } + + protected SearchFactoryImplementor getSearchFactoryImpl() { + FullTextSession s = Search.getFullTextSession( openSession() ); + s.close(); + SearchFactory searchFactory = s.getSearchFactory(); + return (SearchFactoryImplementor) searchFactory; + } + + protected void configure(org.hibernate.cfg.Configuration cfg) { + super.configure( cfg ); + cfg.setProperty( Environment.ERROR_HANDLER, MockErrorHandler.class.getName() ); + } + +} diff --git a/hibernate-search/src/test/resources/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorTest.bytemanrules b/hibernate-search/src/test/resources/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorTest.bytemanrules new file mode 100644 index 00000000000..f9ad19073d1 --- /dev/null +++ b/hibernate-search/src/test/resources/org/hibernate/search/test/errorhandling/ConcurrentMergeErrorTest.bytemanrules @@ -0,0 +1,48 @@ +######################################################################## +# JBoss, Home of Professional Open Source +# Copyright 2009-2010, Red Hat and individual contributors +# by the @authors tag. See the copyright.txt in the distribution for a +# full listing of individual contributors. +# +# This is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 2.1 of +# the License, or (at your option) any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this software; if not, write to the Free +# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA, or see the FSF site: http://www.fsf.org. +# +# @authors Sanne Grinovero +# +# ConcurrentMergeErrorTest +# +# Using Byteman we alter IndexWriter at runtime to throw an IOException during the merge operation, this might happen +# in a secondary thread. +# The flag "createIndexWriterIOException" is checked to make sure we only raise the exception when requested, +# i.e. when we are running the ConcurrentMergeErrorHandledTest + +RULE org.apache.lucene.index.ConcurrentMergeScheduler throws IOException in service thread +CLASS org.apache.lucene.index.IndexWriter +METHOD checkpoint +IF flagged("createIndexWriterIOException") + DO + clear("createIndexWriterIOException"); + traceln( "This invocation of checkpoint() is going to throw an IOException\n" ); + throw new IOException("Byteman said: your disk is full!"); +ENDRULE + +RULE ConcurrentMergeErrorHandledTest +CLASS org.hibernate.search.test.errorhandling.ConcurrentMergeErrorHandledTest +METHOD testLuceneMergerErrorHandling +IF TRUE + DO + traceln( "\n *** Byteman rules deployed! Next IndexWriter.checkpoint invocation will fail. ***\n" ); + flag("createIndexWriterIOException") +ENDRULE diff --git a/pom.xml b/pom.xml index b0a06da400d..0dcbade3f24 100644 --- a/pom.xml +++ b/pom.xml @@ -133,6 +133,7 @@ 3.6.0.Final 3.2.0.Final UTF-8 + 1.4.1 @@ -270,6 +271,12 @@ junit 3.8.1 + + org.jboss.byteman + byteman + ${bytemanVersion} + test + @@ -400,7 +407,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.4.3 + 2.6 true