public JsonRpc2_0Web3j(Web3jService web3jService) { this(web3jService, DEFAULT_BLOCK_TIME, Async.defaultExecutorService()); }
/** * Perform request asynchronously with a future. * * @return a future containing our function */ public CompletableFuture<T> sendAsync() { return Async.run(this::send); }
/** * Provide a new ScheduledExecutorService instance. * * <p>A shutdown hook is created to terminate the thread pool on application termination. * * @return new ScheduledExecutorService */ public static ScheduledExecutorService defaultExecutorService() { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(getCpuCount()); Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdown(scheduledExecutorService))); return scheduledExecutorService; }
@Override public <T extends Response> CompletableFuture<T> sendAsync( Request jsonRpc20Request, Class<T> responseType) { return Async.run(() -> send(jsonRpc20Request, responseType)); }
public QueuingTransactionReceiptProcessor( Web3j web3j, Callback callback, int pollingAttemptsPerTxHash, long pollingFrequency) { super(web3j); this.scheduledExecutorService = Async.defaultExecutorService(); this.callback = callback; this.pendingTransactions = new LinkedBlockingQueue<>(); this.pollingAttemptsPerTxHash = pollingAttemptsPerTxHash; scheduledExecutorService.scheduleAtFixedRate( this::sendTransactionReceiptRequests, pollingFrequency, pollingFrequency, TimeUnit.MILLISECONDS); }
@Test(expected = ExecutionException.class) public void testRunException() throws Exception { Async.run(() -> { throw new RuntimeException(""); }).get(); } }
private JsonRequestFactories jsonRequestFactories() { if (jsonRequestFactories == null) { final Web3jService web3jService = jsonRpcBaseUrl() .map(url -> new HttpService(url)) .orElse(new HttpService("http://" + LOCALHOST + ":8545")); jsonRequestFactories = new JsonRequestFactories( new JsonRpc2_0Web3j(web3jService, 2000, Async.defaultExecutorService()), new CliqueJsonRpcRequestFactory(web3jService), new IbftJsonRpcRequestFactory(web3jService), new PermissioningJsonRpcRequestFactory(web3jService), new AdminJsonRpcRequestFactory(web3jService)); } return jsonRequestFactories; }
@Test public void testRun() throws Exception { assertThat(Async.run(() -> "").get(), is("")); }
@Test(expected = RuntimeException.class) @SuppressWarnings("unchecked") public void testInvalidTransactionResponse() throws Throwable { prepareNonceRequest(); EthSendTransaction ethSendTransaction = new EthSendTransaction(); ethSendTransaction.setError(new Response.Error(1, "Invalid transaction")); Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class); when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction)); when(web3j.ethSendRawTransaction(any(String.class))) .thenReturn((Request) rawTransactionRequest); testErrorScenario(); }
@Test(expected = RuntimeException.class) @SuppressWarnings("unchecked") public void testInvalidTransactionReceipt() throws Throwable { prepareNonceRequest(); prepareTransactionRequest(); EthGetTransactionReceipt ethGetTransactionReceipt = new EthGetTransactionReceipt(); ethGetTransactionReceipt.setError(new Response.Error(1, "Invalid transaction receipt")); Request<?, EthGetTransactionReceipt> getTransactionReceiptRequest = mock(Request.class); when(getTransactionReceiptRequest.sendAsync()) .thenReturn(Async.run(() -> ethGetTransactionReceipt)); when(web3j.ethGetTransactionReceipt(TRANSACTION_HASH)) .thenReturn((Request) getTransactionReceiptRequest); testErrorScenario(); }