Skip to content

Commit

Permalink
HSEARCH-644 Enable Byteman for unit tests, introduce a test for HSEAR…
Browse files Browse the repository at this point in the history
…CH-625
  • Loading branch information
Sanne authored and hferentschik committed Dec 9, 2010
1 parent 4d83d95 commit 685d232
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
1 change: 1 addition & 0 deletions hibernate-search/pom.xml
Expand Up @@ -142,6 +142,7 @@
<configuration>
<forkMode>once</forkMode>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<argLine>-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</argLine>
<systemProperties>
<property>
<name>com.sun.management.jmxremote</name>
Expand Down
@@ -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() );
}

}
@@ -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
9 changes: 8 additions & 1 deletion pom.xml
Expand Up @@ -133,6 +133,7 @@
<hibernateVersion>3.6.0.Final</hibernateVersion>
<hibernateCommonsAnnotationVersion>3.2.0.Final</hibernateCommonsAnnotationVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bytemanVersion>1.4.1</bytemanVersion>
</properties>

<dependencies>
Expand Down Expand Up @@ -270,6 +271,12 @@
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.jboss.byteman</groupId>
<artifactId>byteman</artifactId>
<version>${bytemanVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -400,7 +407,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<version>2.6</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
Expand Down

0 comments on commit 685d232

Please sign in to comment.