Skip to content

Commit

Permalink
FABJ-404 Application set Executor Service
Browse files Browse the repository at this point in the history
Change-Id: Ic33e97243e3aec20e618b40c51f007dbada09487
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Mar 22, 2019
1 parent d9608f2 commit 1865ed8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 33 deletions.
54 changes: 36 additions & 18 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class HFClient {
private static final Config config = Config.getConfig(); // never remove this! config needs to load first.

private CryptoSuite cryptoSuite;
protected final ExecutorService executorService;
protected ExecutorService executorService;

static {

Expand All @@ -67,6 +67,21 @@ public class HFClient {
}

ExecutorService getExecutorService() {
if (null == executorService) {
synchronized (this) { //was null so lets get a lock for safe update.
if (null == executorService) { // no other thread has done it ...
executorService = new ThreadPoolExecutor(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE, CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE,
CLIENT_THREAD_EXECUTOR_KEEPALIVETIME, CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT,
new SynchronousQueue<Runnable>(),
r -> {
Thread t = threadFactory.newThread(r);
t.setDaemon(true);
return t;
});
}
}

}
return executorService;
}

Expand All @@ -88,16 +103,6 @@ public User getUserContext() {
private static final TimeUnit CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT = config.getClientThreadExecutorKeepAliveTimeUnit();

private HFClient() {

executorService = new ThreadPoolExecutor(CLIENT_THREAD_EXECUTOR_COREPOOLSIZE, CLIENT_THREAD_EXECUTOR_MAXIMUMPOOLSIZE,
CLIENT_THREAD_EXECUTOR_KEEPALIVETIME, CLIENT_THREAD_EXECUTOR_KEEPALIVETIMEUNIT,
new SynchronousQueue<Runnable>(),
r -> {
Thread t = threadFactory.newThread(r);
t.setDaemon(true);
return t;
});

}

public CryptoSuite getCryptoSuite() {
Expand All @@ -112,18 +117,31 @@ public void setCryptoSuite(CryptoSuite cryptoSuite) throws CryptoException, Inva
throw new InvalidArgumentException("CryptoSuite may only be set once.");

}
// if (cryptoSuiteFactory == null) {
// cryptoSuiteFactory = cryptoSuite.getCryptoSuiteFactory();
// } else {
// if (cryptoSuiteFactory != cryptoSuite.getCryptoSuiteFactory()) {
// throw new InvalidArgumentException("CryptoSuite is not derivied from cryptosuite factory");
// }
// }

this.cryptoSuite = cryptoSuite;

}

/**
* Set executor service Applications need to set the executor service prior to doing any other operations on the client.
*
* @param executorService The executor service the application wants to use.
* @throws InvalidArgumentException if executor service has been set already.
*/

public synchronized void setExecutorService(ExecutorService executorService) throws InvalidArgumentException {

if (executorService == null) {
throw new InvalidArgumentException("Executor service can not be null.");
}
if (this.executorService != null && this.executorService != executorService) {
throw new InvalidArgumentException("Executor service has already been set.");
}

this.executorService = executorService;

}

/**
* createNewInstance create a new instance of the HFClient
*
Expand Down
106 changes: 92 additions & 14 deletions src/test/java/org/hyperledger/fabric/sdk/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package org.hyperledger.fabric.sdk;

import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.helper.Config;
Expand All @@ -26,6 +30,7 @@
import org.junit.Test;

import static org.hyperledger.fabric.sdk.testutils.TestUtils.resetConfig;
import static org.junit.Assert.assertSame;

public class ClientTest {
private static final String CHANNEL_NAME = "channel1";
Expand All @@ -34,7 +39,6 @@ public class ClientTest {
private static final String USER_NAME = "MockMe";
private static final String USER_MSP_ID = "MockMSPID";


@BeforeClass
public static void setupClient() throws Exception {
try {
Expand All @@ -60,7 +64,7 @@ public void testNewChannel() {
}
}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testSetNullChannel() throws InvalidArgumentException {
hfclient.newChannel(null);
Assert.fail("Expected null channel to throw exception.");
Expand All @@ -77,7 +81,7 @@ public void testNewPeer() {
}
}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadURL() throws InvalidArgumentException {
hfclient.newPeer("peer_", " ");
Assert.fail("Expected peer with no channel throw exception");
Expand All @@ -94,13 +98,13 @@ public void testNewOrderer() {
}
}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadAddress() throws InvalidArgumentException {
hfclient.newOrderer("xx", "xxxxxx");
Assert.fail("Orderer allowed setting bad URL.");
}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadCryptoSuite() throws InvalidArgumentException {
HFClient.createNewInstance()
.newOrderer("xx", "xxxxxx");
Expand All @@ -117,7 +121,7 @@ public void testGoodMockUser() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadUserContextNull() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -126,7 +130,7 @@ public void testBadUserContextNull() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadUserNameNull() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -137,7 +141,7 @@ public void testBadUserNameNull() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadUserNameEmpty() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -148,7 +152,7 @@ public void testBadUserNameEmpty() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadUserMSPIDNull() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -159,7 +163,7 @@ public void testBadUserMSPIDNull() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadUserMSPIDEmpty() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -170,7 +174,7 @@ public void testBadUserMSPIDEmpty() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadEnrollmentNull() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -182,7 +186,7 @@ public void testBadEnrollmentNull() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadEnrollmentBadCert() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
Expand All @@ -196,12 +200,11 @@ public void testBadEnrollmentBadCert() throws Exception {

}

@Test(expected = InvalidArgumentException.class)
@Test (expected = InvalidArgumentException.class)
public void testBadEnrollmentBadKey() throws Exception {
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());


MockUser mockUser = TestUtils.getMockUser(USER_NAME, USER_MSP_ID);

Enrollment mockEnrollment = TestUtils.getMockEnrollment(null, "mockCert");
Expand All @@ -211,6 +214,81 @@ public void testBadEnrollmentBadKey() throws Exception {

}

@Test
public void testExecutorset() throws Exception {

hfclient = TestHFClient.newInstance();
// ThreadPoolExecutor threadPoolExecutor = ThreadPoolExecutor()
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100,
40, TimeUnit.valueOf("MILLISECONDS"),
new SynchronousQueue<Runnable>(),
r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});

hfclient.setExecutorService(threadPoolExecutor);
assertSame(threadPoolExecutor, hfclient.getExecutorService());
Channel mychannel = hfclient.newChannel("mychannel");
assertSame(threadPoolExecutor, mychannel.getExecutorService());

}

@Test (expected = InvalidArgumentException.class)
public void testExecutorsetAgain() throws Exception {

hfclient = TestHFClient.newInstance();

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100,
40, TimeUnit.valueOf("MILLISECONDS"),
new SynchronousQueue<Runnable>(),
r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});

hfclient.setExecutorService(threadPoolExecutor);
assertSame(threadPoolExecutor, hfclient.getExecutorService());
ThreadPoolExecutor threadPoolExecutor2 = new ThreadPoolExecutor(10, 100,
40, TimeUnit.valueOf("MILLISECONDS"),
new SynchronousQueue<Runnable>(),
r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});
hfclient.setExecutorService(threadPoolExecutor2);
}

@Test (expected = InvalidArgumentException.class)
public void testExecutorDefaultSet() throws Exception {

hfclient = TestHFClient.newInstance();

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100,
40, TimeUnit.valueOf("MILLISECONDS"),
new SynchronousQueue<Runnable>(),
r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});

Channel badisme = hfclient.newChannel("badisme");
badisme.getExecutorService();
hfclient.setExecutorService(threadPoolExecutor);
}

@Test (expected = InvalidArgumentException.class)
public void testExecutorsetNULL() throws Exception {

hfclient = TestHFClient.newInstance();

hfclient.setExecutorService(null);
}

@Test //(expected = InvalidArgumentException.class)
@Ignore
public void testCryptoFactory() throws Exception {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/hyperledger/fabric/sdk/TestHFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static HFClient newInstance() throws Exception {
}

public static void setupClient(HFClient hfclient) throws Exception {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();

File tempFile = File.createTempFile("teststore", "properties");
tempFile.deleteOnExit();
Expand Down

0 comments on commit 1865ed8

Please sign in to comment.