Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix search test NPE #11749

Merged
merged 6 commits into from
Sep 13, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/search/DatabaseSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public List<BibEntry> getMatches() {

if (!query.isValid()) {
LOGGER.warn("Search failed: invalid search expression");
luceneManager.closeAndWait();
return Collections.emptyList();
}
List<BibEntry> matchEntries = luceneManager.search(query)
.getMatchedEntries()
.stream()
.map(entryId -> databaseContext.getDatabase().getEntryById(entryId))
.toList();
luceneManager.close();
luceneManager.closeAndWait();
return BibDatabases.purgeEmptyEntries(matchEntries);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/logic/search/LuceneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public void close() {
databaseContext.getDatabase().postEvent(new IndexClosedEvent());
}

public void closeAndWait() {
bibFieldsIndexer.closeAndWait();
shouldIndexLinkedFiles.removeListener(preferencesListener);
linkedFilesIndexer.closeAndWait();
databaseContext.getDatabase().postEvent(new IndexClosedEvent());
}

public AutoCloseable blockLinkedFileIndexer() {
LOGGER.debug("Blocking linked files indexer");
isLinkedFilesIndexerBlocked.set(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public void close() {
HeadlessExecutorService.INSTANCE.execute(this::closeIndex);
}

@Override
public void closeAndWait() {
HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex);
}

private void closeIndex() {
try {
LOGGER.debug("Closing bib fields index");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ public void close() {
HeadlessExecutorService.INSTANCE.execute(this::closeIndex);
}

@Override
public void closeAndWait() {
HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pass this dependency in the constructor as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check this commit 81a30ff (#11749)? added a boolean parameter to the constructor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry, I did not clarfiy. I meant the Executor Service, that would partially also improve the test siutation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: Refactor DefaultLinkedFilesIndexer NOT to depend on HeadlessExecutorService.INSTANCE, but to have an ExecutorService passed using the constructor.

--> follow-up PR

}

private void closeIndex() {
try {
LOGGER.debug("Closing linked files index");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void close() {
HeadlessExecutorService.INSTANCE.execute(this::closeIndex);
}

@Override
public void closeAndWait() {
HeadlessExecutorService.INSTANCE.executeAndWait(this::closeIndex);
}

private void closeIndex() {
try {
searcherManager.close();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/model/search/LuceneIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface LuceneIndexer {
SearcherManager getSearcherManager();

void close();

void closeAndWait();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.logic.search;

import java.io.IOException;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -18,24 +19,29 @@
import org.jabref.preferences.FilePreferences;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class DatabaseSearcherTest {
private static final TaskExecutor TASK_EXECUTOR = new CurrentThreadTaskExecutor();
private BibDatabaseContext databaseContext;
private final FilePreferences filePreferences = mock(FilePreferences.class);
@TempDir
private Path indexDir;

@BeforeEach
void setUp() {
when(filePreferences.shouldFulltextIndexLinkedFiles()).thenReturn(false);
when(filePreferences.fulltextIndexLinkedFilesProperty()).thenReturn(mock(BooleanProperty.class));
databaseContext = new BibDatabaseContext();
databaseContext = spy(new BibDatabaseContext());
when(databaseContext.getFulltextIndexPath()).thenReturn(indexDir);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class DatabaseSearcherWithBibFilesTest {
Expand Down Expand Up @@ -77,7 +78,8 @@ private BibDatabaseContext initializeDatabaseFromPath(String testFile) throws Ex

private BibDatabaseContext initializeDatabaseFromPath(Path testFile) throws Exception {
ParserResult result = new BibtexImporter(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS), new DummyFileUpdateMonitor()).importDatabase(testFile);
BibDatabaseContext databaseContext = result.getDatabaseContext();
BibDatabaseContext databaseContext = spy(result.getDatabaseContext());
when(databaseContext.getFulltextIndexPath()).thenReturn(indexDir);

when(filePreferences.shouldFulltextIndexLinkedFiles()).thenReturn(true);
when(filePreferences.fulltextIndexLinkedFilesProperty()).thenReturn(new SimpleBooleanProperty(true));
Expand Down