Skip to content

Commit

Permalink
HSEARCH-3759 Simplify stubbing of IndexWorkspace
Browse files Browse the repository at this point in the history
We know there will always be one and only one index name, so there's no
need to store the index names in a collection.
  • Loading branch information
yrodiere committed Mar 6, 2020
1 parent 4be91de commit fe2b4e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
Expand Up @@ -170,13 +170,9 @@ public IndexScaleWorkCallListContext expectIndexScaleWorks(String indexName) {
}

public IndexScaleWorkCallListContext expectIndexScaleWorks(String indexName, String tenantId) {
return expectIndexScaleWorks( Collections.singletonList( indexName ), tenantId );
}

public IndexScaleWorkCallListContext expectIndexScaleWorks(Collection<String> indexNames, String tenantId) {
CallQueue<IndexScaleWorkCalls> callQueue = behaviorMock.getIndexScaleWorkCalls();
CallQueue<IndexScaleWorkCall> callQueue = behaviorMock.getIndexScaleWorkCalls();
return new IndexScaleWorkCallListContext(
new LinkedHashSet<>( indexNames ), tenantId,
indexName, tenantId,
callQueue::expectInOrder
);
}
Expand Down Expand Up @@ -327,14 +323,14 @@ public BackendMock discarded() {
}

public class IndexScaleWorkCallListContext {
private final Set<String> indexNames;
private final String indexName;
private final String tenantIdentifier;
private final Consumer<IndexScaleWorkCalls> expectationConsumer;
private final Consumer<IndexScaleWorkCall> expectationConsumer;

private IndexScaleWorkCallListContext(Set<String> indexNames,
private IndexScaleWorkCallListContext(String indexName,
String tenantIdentifier,
Consumer<IndexScaleWorkCalls> expectationConsumer) {
this.indexNames = indexNames;
Consumer<IndexScaleWorkCall> expectationConsumer) {
this.indexName = indexName;
this.tenantIdentifier = tenantIdentifier;
this.expectationConsumer = expectationConsumer;
}
Expand Down Expand Up @@ -397,7 +393,7 @@ public IndexScaleWorkCallListContext indexScaleWork(StubIndexScaleWork.Type type
.tenantIdentifier( tenantIdentifier )
.routingKeys( routingKeys )
.build();
expectationConsumer.accept( new IndexScaleWorkCalls( indexNames, work, future ) );
expectationConsumer.accept( new IndexScaleWorkCall( indexName, work, future ) );
return this;
}
}
Expand Down
Expand Up @@ -7,33 +7,32 @@
package org.hibernate.search.util.impl.integrationtest.common.rule;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import org.hibernate.search.util.impl.integrationtest.common.assertion.StubIndexScaleWorkAssert;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.StubIndexScaleWork;

class IndexScaleWorkCalls extends Call<IndexScaleWorkCalls> {
class IndexScaleWorkCall extends Call<IndexScaleWorkCall> {

private final Set<String> indexNames;
private final String indexName;
private final StubIndexScaleWork work;
private final CompletableFuture<?> completableFuture;

IndexScaleWorkCalls(Set<String> indexNames, StubIndexScaleWork work,
IndexScaleWorkCall(String indexName, StubIndexScaleWork work,
CompletableFuture<?> completableFuture) {
this.indexNames = indexNames;
this.indexName = indexName;
this.work = work;
this.completableFuture = completableFuture;
}

IndexScaleWorkCalls(Set<String> indexNames, StubIndexScaleWork work) {
this.indexNames = indexNames;
IndexScaleWorkCall(String indexName, StubIndexScaleWork work) {
this.indexName = indexName;
this.work = work;
this.completableFuture = null;
}

public CallBehavior<CompletableFuture<?>> verify(IndexScaleWorkCalls actualCall) {
String whenThisWorkWasExpected = "when an index-scope work on indexes '" + indexNames
public CallBehavior<CompletableFuture<?>> verify(IndexScaleWorkCall actualCall) {
String whenThisWorkWasExpected = "when an index-scope work on index '" + indexName
+ "' was expected";
StubIndexScaleWorkAssert.assertThat( actualCall.work )
.as( "Incorrect work " + whenThisWorkWasExpected + ":\n" )
Expand All @@ -42,14 +41,13 @@ public CallBehavior<CompletableFuture<?>> verify(IndexScaleWorkCalls actualCall)
}

@Override
protected boolean isSimilarTo(IndexScaleWorkCalls other) {
return Objects.equals( indexNames, other.indexNames )
protected boolean isSimilarTo(IndexScaleWorkCall other) {
return Objects.equals( indexName, other.indexName )
&& Objects.equals( work.getTenantIdentifier(), other.work.getTenantIdentifier() );
}

@Override
public String toString() {
return "index-scope work on indexes " + indexNames
+ "'; work = " + work;
return "index-scope work on index " + indexName + "'; work = " + work;
}
}
Expand Up @@ -44,7 +44,7 @@ class VerifyingStubBackendBehavior extends StubBackendBehavior {

private final Map<String, CallQueue<DocumentWorkCall>> documentWorkCalls = new HashMap<>();

private final CallQueue<IndexScaleWorkCalls> indexScaleWorkCalls = new CallQueue<>();
private final CallQueue<IndexScaleWorkCall> indexScaleWorkCalls = new CallQueue<>();

private final CallQueue<SearchWorkCall<?>> searchCalls = new CallQueue<>();

Expand Down Expand Up @@ -76,7 +76,7 @@ CallQueue<DocumentWorkCall> getDocumentWorkCalls(String indexName) {
return documentWorkCalls.computeIfAbsent( indexName, ignored -> new CallQueue<>() );
}

CallQueue<IndexScaleWorkCalls> getIndexScaleWorkCalls() {
CallQueue<IndexScaleWorkCall> getIndexScaleWorkCalls() {
return indexScaleWorkCalls;
}

Expand Down Expand Up @@ -195,10 +195,10 @@ public <T> SearchResult<T> executeSearchWork(Set<String> indexNames, StubSearchW
}

@Override
public CompletableFuture<?> executeIndexScaleWork(Set<String> indexNames, StubIndexScaleWork work) {
public CompletableFuture<?> executeIndexScaleWork(String indexName, StubIndexScaleWork work) {
return indexScaleWorkCalls.verify(
new IndexScaleWorkCalls( indexNames, work ),
IndexScaleWorkCalls::verify,
new IndexScaleWorkCall( indexName, work ),
IndexScaleWorkCall::verify,
noExpectationsBehavior( () -> CompletableFuture.completedFuture( null ) )
);
}
Expand Down
Expand Up @@ -79,9 +79,9 @@ public <T> SearchResult<T> executeSearchWork(Set<String> indexNames, StubSearchW
}

@Override
public CompletableFuture<?> executeIndexScaleWork(Set<String> indexNames, StubIndexScaleWork work) {
throw new IllegalStateException( "The stub backend behavior was not set during execution of an index-scale work for indexes "
+ indexNames + ": " + work );
public CompletableFuture<?> executeIndexScaleWork(String indexName, StubIndexScaleWork work) {
throw new IllegalStateException( "The stub backend behavior was not set during execution of an index-scale work for index "
+ indexName + ": " + work );
}

@Override
Expand Down Expand Up @@ -128,7 +128,7 @@ public abstract <T> SearchResult<T> executeSearchWork(Set<String> indexNames, St
StubSearchProjectionContext projectionContext,
LoadingContext<?, ?> loadingContext, StubSearchProjection<T> rootProjection);

public abstract CompletableFuture<?> executeIndexScaleWork(Set<String> indexNames, StubIndexScaleWork work);
public abstract CompletableFuture<?> executeIndexScaleWork(String indexName, StubIndexScaleWork work);

public abstract long executeCountWork(Set<String> indexNames);
}
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.impl;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

Expand All @@ -31,7 +30,7 @@ class StubIndexWorkspace implements IndexWorkspace {
@Override
public CompletableFuture<?> mergeSegments() {
StubIndexScaleWork work = StubIndexScaleWork.builder( StubIndexScaleWork.Type.MERGE_SEGMENTS ).build();
return behavior.executeIndexScaleWork( Collections.singleton( indexName ), work );
return behavior.executeIndexScaleWork( indexName, work );
}

@Override
Expand All @@ -40,18 +39,18 @@ public CompletableFuture<?> purge(Set<String> routingKeys) {
.tenantIdentifier( sessionContext.getTenantIdentifier() )
.routingKeys( routingKeys )
.build();
return behavior.executeIndexScaleWork( Collections.singleton( indexName ), work );
return behavior.executeIndexScaleWork( indexName, work );
}

@Override
public CompletableFuture<?> flush() {
StubIndexScaleWork work = StubIndexScaleWork.builder( StubIndexScaleWork.Type.FLUSH ).build();
return behavior.executeIndexScaleWork( Collections.singleton( indexName ), work );
return behavior.executeIndexScaleWork( indexName, work );
}

@Override
public CompletableFuture<?> refresh() {
StubIndexScaleWork work = StubIndexScaleWork.builder( StubIndexScaleWork.Type.REFRESH ).build();
return behavior.executeIndexScaleWork( Collections.singleton( indexName ), work );
return behavior.executeIndexScaleWork( indexName, work );
}
}

0 comments on commit fe2b4e3

Please sign in to comment.