|
36 | 36 | import org.fisco.bcos.sdk.demo.contract.HelloWorld;
|
37 | 37 | import org.fisco.bcos.sdk.model.RetCode;
|
38 | 38 | import org.fisco.bcos.sdk.model.TransactionReceipt;
|
| 39 | +import org.fisco.bcos.sdk.test.service.GroupServiceTest; |
| 40 | +import org.fisco.bcos.sdk.transaction.model.callback.TransactionSucCallback; |
39 | 41 | import org.junit.Assert;
|
40 | 42 | import org.junit.FixMethodOrder;
|
41 | 43 | import org.junit.Test;
|
|
46 | 48 | import java.util.HashMap;
|
47 | 49 | import java.util.List;
|
48 | 50 | import java.util.Map;
|
| 51 | +import java.util.concurrent.ExecutorService; |
| 52 | +import java.util.concurrent.Executors; |
| 53 | +import java.util.concurrent.atomic.AtomicLong; |
49 | 54 |
|
50 | 55 | @FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
51 | 56 | public class PrecompiledTest
|
@@ -203,7 +208,7 @@ private void testSystemConfigService(Client client, SystemConfigService systemCo
|
203 | 208 | Assert.assertTrue(queriedValue.equals(updatedValue));
|
204 | 209 | Assert.assertTrue(queriedValue.equals(value.add(BigInteger.valueOf(1000))));
|
205 | 210 | }
|
206 |
| - |
| 211 | + // Note: Please make sure that the ut is before the permission-related ut |
207 | 212 | @Test
|
208 | 213 | public void test5CRUDService() throws ConfigException, ContractException {
|
209 | 214 | try {
|
@@ -251,6 +256,134 @@ public void test5CRUDService() throws ConfigException, ContractException {
|
251 | 256 | }
|
252 | 257 | }
|
253 | 258 |
|
| 259 | + // Note: Please make sure that the ut is before the permission-related ut |
| 260 | + @Test |
| 261 | + public void test51SyncCRUDService() throws ConfigException { |
| 262 | + try { |
| 263 | + BcosSDK sdk = new BcosSDK(configFile); |
| 264 | + Client client = sdk.getClient(Integer.valueOf(1)); |
| 265 | + CryptoInterface cryptoInterface = client.getCryptoInterface(); |
| 266 | + TableCRUDService crudService = new TableCRUDService(client, cryptoInterface); |
| 267 | + String tableName = "test_sync"; |
| 268 | + List<String> valueFiled = new ArrayList<>(); |
| 269 | + valueFiled.add("field"); |
| 270 | + RetCode retCode = crudService.createTable(tableName, "key", valueFiled); |
| 271 | + System.out.println("createResult: " + retCode.getCode() + ", message: " + retCode.getMessage()); |
| 272 | + // create a thread pool to parallel insert and select |
| 273 | + ExecutorService threadPool = Executors.newFixedThreadPool(50); |
| 274 | + |
| 275 | + BigInteger orgTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16); |
| 276 | + for(int i = 0; i < 100; i++) |
| 277 | + { |
| 278 | + final Integer index = i; |
| 279 | + threadPool.execute(new Runnable() { |
| 280 | + @Override |
| 281 | + public void run() { |
| 282 | + try { |
| 283 | + Map<String, String> value = new HashMap<>(); |
| 284 | + value.put("field", "field" + index); |
| 285 | + String valueOfKey = "key_value" + index; |
| 286 | + // insert |
| 287 | + crudService.insert(tableName, valueOfKey , new Entry(value), null); |
| 288 | + // select |
| 289 | + crudService.select(tableName, valueOfKey, null); |
| 290 | + // update |
| 291 | + value.clear(); |
| 292 | + value.put("field", "field" + index + 100); |
| 293 | + crudService.update(tableName, valueOfKey, new Entry(value), null); |
| 294 | + // remove |
| 295 | + crudService.remove(tableName, valueOfKey, null); |
| 296 | + }catch(ContractException e) |
| 297 | + { |
| 298 | + System.out.println("call crudService failed, error information: " + e.getMessage()); |
| 299 | + } |
| 300 | + } |
| 301 | + }); |
| 302 | + } |
| 303 | + GroupServiceTest.awaitAfterShutdown(threadPool); |
| 304 | + BigInteger currentTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16); |
| 305 | + System.out.println("orgTxCount: " + orgTxCount + ", currentTxCount:" + currentTxCount); |
| 306 | + Assert.assertTrue(currentTxCount.equals(orgTxCount.add(BigInteger.valueOf(300)))); |
| 307 | + }catch(ContractException e) |
| 308 | + { |
| 309 | + System.out.println("test9SyncCRUDService failed, error info: " + e.getMessage()); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + class FakeTransactionCallback extends TransactionSucCallback { |
| 314 | + public TransactionReceipt receipt; |
| 315 | + public AtomicLong receiptCount = new AtomicLong(); |
| 316 | + @Override |
| 317 | + public void onTimeout() { |
| 318 | + super.onTimeout(); |
| 319 | + } |
| 320 | + |
| 321 | + // wait until get the transactionReceipt |
| 322 | + @Override |
| 323 | + public void onResponse(TransactionReceipt receipt) { |
| 324 | + cancelTimeout(); |
| 325 | + this.receipt = receipt; |
| 326 | + receiptCount.addAndGet(1); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + public void test52AsyncCRUDService() |
| 332 | + { |
| 333 | + try { |
| 334 | + BcosSDK sdk = new BcosSDK(configFile); |
| 335 | + Client client = sdk.getClient(Integer.valueOf(1)); |
| 336 | + CryptoInterface cryptoInterface = client.getCryptoInterface(); |
| 337 | + TableCRUDService crudService = new TableCRUDService(client, cryptoInterface); |
| 338 | + // create table |
| 339 | + String tableName = "send_async"; |
| 340 | + List<String> valueFiled = new ArrayList<>(); |
| 341 | + valueFiled.add("field"); |
| 342 | + String key = "key"; |
| 343 | + crudService.createTable(tableName, key, valueFiled); |
| 344 | + // create a thread pool to parallel insert and select |
| 345 | + ExecutorService threadPool = Executors.newFixedThreadPool(50); |
| 346 | + FakeTransactionCallback callback = new FakeTransactionCallback(); |
| 347 | + BigInteger orgTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16); |
| 348 | + for(int i = 0; i < 100; i++) |
| 349 | + { |
| 350 | + final Integer index = i; |
| 351 | + threadPool.execute(new Runnable() { |
| 352 | + @Override |
| 353 | + public void run() { |
| 354 | + try { |
| 355 | + Map<String, String> value = new HashMap<>(); |
| 356 | + value.put("field", "field" + index); |
| 357 | + String valueOfKey = "key_value" + index; |
| 358 | + // insert |
| 359 | + crudService.asyncInsert(tableName, valueOfKey , new Entry(value), null, callback); |
| 360 | + // update |
| 361 | + value.clear(); |
| 362 | + value.put("field", "field" + index + 100); |
| 363 | + crudService.asyncUpdate(tableName, valueOfKey, new Entry(value), null, callback); |
| 364 | + // remove |
| 365 | + crudService.asyncRemove(tableName, valueOfKey, null, callback); |
| 366 | + }catch(ContractException e) |
| 367 | + { |
| 368 | + System.out.println("call crudService failed, error information: " + e.getMessage()); |
| 369 | + } |
| 370 | + } |
| 371 | + }); |
| 372 | + } |
| 373 | + while(callback.receiptCount.get() != 300) |
| 374 | + { |
| 375 | + Thread.sleep(1000); |
| 376 | + } |
| 377 | + GroupServiceTest.awaitAfterShutdown(threadPool); |
| 378 | + BigInteger currentTxCount = new BigInteger(client.getTotalTransactionCount().getTotalTransactionCount().getTxSum().substring(2), 16); |
| 379 | + System.out.println("orgTxCount: " + orgTxCount + ", currentTxCount:" + currentTxCount); |
| 380 | + Assert.assertTrue(currentTxCount.equals(orgTxCount.add(BigInteger.valueOf(300)))); |
| 381 | + }catch(ContractException | ConfigException | InterruptedException e) |
| 382 | + { |
| 383 | + System.out.println("test10AsyncCRUDService failed, error info: " + e.getMessage()); |
| 384 | + } |
| 385 | + } |
| 386 | + |
254 | 387 | @Test
|
255 | 388 | public void test6PermissionService() throws ConfigException, ContractException {
|
256 | 389 | try {
|
|
0 commit comments