Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.
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
8 changes: 8 additions & 0 deletions src/main/java/org/hyperledger/fabric/gateway/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ interface Builder {
*/
Builder discovery(boolean enabled);

/**
* <em>Optional</em> - Enable or disable force immediate shutdown of the network connection.
* forceClose is enabled by default.
* @param enabled - true to enable force immediate shutdown.
* @return The builder instance, allowing multiple configuration options to be chained.
*/
Builder forceClose(boolean enabled);

/**
* Connects to the gateway using the specified options.
* @return The connected {@link Gateway} object.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/hyperledger/fabric/gateway/impl/GatewayImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public final class GatewayImpl implements Gateway {
private final TimePeriod commitTimeout;
private final QueryHandlerFactory queryHandlerFactory;
private final boolean discovery;
private final boolean forceClose;

public static final class Builder implements Gateway.Builder {
private CommitHandlerFactory commitHandlerFactory = DefaultCommitHandlers.PREFER_MSPID_SCOPE_ALLFORTX;
Expand All @@ -71,6 +72,7 @@ public static final class Builder implements Gateway.Builder {
private Identity identity = null;
private HFClient client;
private boolean discovery = false;
private boolean forceClose = true;

private static final class ExposedByteArrayOutputStream extends ByteArrayOutputStream {
public byte[] getInternalBuffer() {
Expand Down Expand Up @@ -152,6 +154,12 @@ public Builder discovery(final boolean enabled) {
return this;
}

@Override
public Builder forceClose(final boolean enabled) {
this.forceClose = enabled;
return this;
}

public Builder client(final HFClient client) {
this.client = client;
return this;
Expand All @@ -168,6 +176,7 @@ private GatewayImpl(final Builder builder) {
this.commitTimeout = builder.commitTimeout;
this.queryHandlerFactory = builder.queryHandlerFactory;
this.discovery = builder.discovery;
this.forceClose = builder.forceClose;

if (builder.client != null) {
// Only for testing!
Expand Down Expand Up @@ -201,6 +210,7 @@ private GatewayImpl(final GatewayImpl that) {
this.commitTimeout = that.commitTimeout;
this.queryHandlerFactory = that.queryHandlerFactory;
this.discovery = that.discovery;
this.forceClose = that.forceClose;
this.networkConfig = that.networkConfig;
this.identity = that.identity;

Expand Down Expand Up @@ -281,6 +291,10 @@ public boolean isDiscoveryEnabled() {
return discovery;
}

public boolean isForceClose() {
return forceClose;
}

public GatewayImpl newInstance() {
return new GatewayImpl(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void close() {
orderedBlockSource.close();
channelBlockSource.close();

channel.shutdown(false);
channel.shutdown(gateway.isForceClose());
}

@Override
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/hyperledger/fabric/gateway/impl/GatewayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ public void testCloseGatewayClosesNetworks() {
assertThat(channel.isShutdown()).isTrue();
}

@Test
public void testCloseGatewayForceClosesNetworks() {
HFClient mockClient = testUtils.newMockClient();
Channel mockChannel = testUtils.newMockChannel("CHANNEL");
Mockito.when(mockClient.getChannel("CHANNEL")).thenReturn(mockChannel);

builder.client(mockClient);

try (Gateway gateway = builder.connect()) {
gateway.getNetwork("CHANNEL");
}

Mockito.verify(mockChannel).shutdown(true);
}

@Test
public void testCloseGatewayWithForceCloseDisabledClosesNetworks() {
HFClient mockClient = testUtils.newMockClient();
Channel mockChannel = testUtils.newMockChannel("CHANNEL");
Mockito.when(mockClient.getChannel("CHANNEL")).thenReturn(mockChannel);

builder.client(mockClient);
builder.forceClose(false);

try (Gateway gateway = builder.connect()) {
gateway.getNetwork("CHANNEL");
}

Mockito.verify(mockChannel).shutdown(false);
}

@Test
public void testNewInstanceHasSameCryptoSuite() {
final HFClient clientSpy;
Expand Down