Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
/*
* 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.query.timeout;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.search.annotations.Field;
Expand All @@ -13,32 +35,56 @@
@Entity
@Indexed
public class Clock {
public Clock() {}

public Clock(String model, String brand, Long durability) {
private Long id;
private String model;
private String brand;
private Long durability;

public Clock() {
}

public Clock(Long id, String model, String brand, Long durability) {
this.id = id;
this.model = model;
this.brand = brand;
this.durability = durability;
}

@Id
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
private Long id;
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}


@Field
public String getModel() { return model; }
public void setModel(String model) { this.model = model; }
private String model;
public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}


@Field
public String getBrand() { return brand; }
public void setBrand(String brand) { this.brand = brand; }
private String brand;
public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

@Field
public Long getDurability() { return durability; }
public void setDurability(Long durability) { this.durability = durability; }
private Long durability;
public Long getDurability() {
return durability;
}

public void setDurability(Long durability) {
this.durability = durability;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
/*
* 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.query.timeout;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.persistence.QueryTimeoutException;

import org.apache.lucene.search.Query;
import org.jboss.byteman.contrib.bmunit.BMRule;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.hibernate.search.test.jpa.JPATestCase;
import org.jboss.byteman.contrib.bmunit.BMRule;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author Emmanuel Bernard
Expand All @@ -25,40 +48,41 @@ public class JPATimeoutTest extends JPATestCase {

@Test
@BMRule(targetClass = "QueryHits",
targetMethod = "updateTopDocs",
helper = "org.hibernate.search.test.util.BytemanHelper",
action = "sleepASecond();",
name = "Enable QueryHits slow down")
targetMethod = "updateTopDocs",
helper = "org.hibernate.search.test.util.BytemanHelper",
action = "sleepASecond();",
name = "Enable QueryHits slow down")
public void testQueryTimeoutException() throws Exception {
FullTextEntityManager em = Search.getFullTextEntityManager( factory.createEntityManager() );
em.getTransaction().begin();
for ( int i = 0 ; i < 1000 ; i++ ) {
Clock clock = new Clock("Model cat A" + i, (i % 2 == 0) ? "Seiko" : "Swatch", Long.valueOf( 2000 + i ) ) ;
for ( long i = 0; i < 1000; i++ ) {
Clock clock = new Clock( i, "Model cat A" + i, ( i % 2 == 0 ) ? "Seiko" : "Swatch", 2000 + i );
em.persist( clock );
}
em.getTransaction().commit();

em.clear();

em.getTransaction().begin();
final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity( Clock.class ).get();
Query query = builder.keyword().onField( "brand" ).matching( "Seiko" ).createQuery();
FullTextQuery hibernateQuery = em.createFullTextQuery( query, Clock.class );

hibernateQuery.setHint( "javax.persistence.query.timeout", 100 ); //not too low or we can't reproduce it consistently
hibernateQuery.setHint(
"javax.persistence.query.timeout",
100
); //not too low or we can't reproduce it consistently
try {
hibernateQuery.getResultSize();
fail("timeout exception should happen");
fail( "timeout exception should happen" );
}
catch ( QueryTimeoutException e ) {
//good
e.printStackTrace( );
e.printStackTrace();
}
catch ( Exception e ) {
fail("Expected a QueryTimeoutException");
fail( "Expected a QueryTimeoutException" );
}
em.getTransaction().commit();

em.clear();

em.getTransaction().begin();
Expand All @@ -72,12 +96,11 @@ public void testQueryTimeoutException() throws Exception {
public void testLimitFetchingTime() {
FullTextEntityManager em = Search.getFullTextEntityManager( factory.createEntityManager() );
em.getTransaction().begin();
for ( int i = 0 ; i < 1000 ; i++ ) {
Clock clock = new Clock("Model cat A" + i, (i % 2 == 0) ? "Seiko" : "Swatch", Long.valueOf( 2000 + i ) ) ;
for ( long i = 0; i < 1000; i++ ) {
Clock clock = new Clock( i, "Model cat A" + i, ( i % 2 == 0 ) ? "Seiko" : "Swatch", 2000 + i );
em.persist( clock );
}
em.getTransaction().commit();

em.clear();

em.getTransaction().begin();
Expand All @@ -93,9 +116,9 @@ public void testLimitFetchingTime() {
hibernateQuery = em.createFullTextQuery( query, Clock.class );
hibernateQuery.limitExecutionTimeTo( 1, TimeUnit.NANOSECONDS );
List result = hibernateQuery.getResultList();
System.out.println("Result size early: " + result.size() );
assertEquals("Test early failure, before the number of results are even fetched", 0, result.size() );
if ( result.size() == 0) {
System.out.println( "Result size early: " + result.size() );
assertEquals( "Test early failure, before the number of results are even fetched", 0, result.size() );
if ( result.size() == 0 ) {
//sometimes, this
assertTrue( hibernateQuery.hasPartialResults() );
}
Expand All @@ -116,7 +139,7 @@ public void testLimitFetchingTime() {
hibernateQuery = em.createFullTextQuery( query, Clock.class );
hibernateQuery.limitExecutionTimeTo( 30, TimeUnit.SECONDS );
results = hibernateQuery.getResultList();
assertEquals("Test below limit termination", 500, results.size() );
assertEquals( "Test below limit termination", 500, results.size() );
assertFalse( hibernateQuery.hasPartialResults() );

em.getTransaction().commit();
Expand All @@ -128,11 +151,15 @@ public void testLimitFetchingTime() {
em.getTransaction().commit();

em.close();

}

@Override
public Class[] getAnnotatedClasses() {
return new Class[] { Clock.class };
}

@Override
protected void configure(Map cfg) {
cfg.put( "hibernate.jdbc.batch_size", "1000" );
}
}
Loading