VM tests
TestABCIService
TestABCIServiceTestABCIService unit test in the service_test.go file. Overall, the TestABCIService unit test is testing several methods of the virtual machine service and verifying that they behave as expected.
Here's a breakdown of what the test is doing:
The
TestABCIServicefunction starts by calling themustNewKVTestVmfunction to create a new instance of the virtual machine and virtual machine service, and assigns them to thevmandservicevariables.The test then runs several subtests using the
t.Runmethod, each of which tests a different method of the virtual machine service.
Subtests
ABCIInfo, tests theABCIInfomethod of the virtual machine service. It creates a newctypes.ResultABCIInfovariable and passes it to theABCIInfomethod. It then asserts that there is no error returned by the method, and that the response contains the expected values for the app version, last block height, and last block app hash.ABCIQuery, tests theABCIQuerymethod of the virtual machine service. It creates a new key-value transaction using theMakeTxKVfunction, broadcasts the transaction using theBroadcastTxSyncmethod of the virtual machine service, builds and accepts a new block using the virtual machine, and then queries the virtual machine service for the value of the key using theABCIQuerymethod. If the query is successful, the subtest asserts that the value returned by the method is equal to the expected value.BroadcastTxCommit, tests theBroadcastTxCommitmethod of the virtual machine service. It creates a new key-value transaction using theMakeTxKVfunction, broadcasts the transaction using theBroadcastTxCommitmethod of the virtual machine service, and then asserts that theCheckTxandDeliverTxfields of the response containtrue, indicating that the transaction was successfully checked and committed to the blockchain.BroadcastTxAsync, tests theBroadcastTxAsyncmethod of the virtual machine service. It creates a new key-value transaction using theMakeTxKVfunction, broadcasts the transaction using theBroadcastTxAsyncmethod of the virtual machine service, and then asserts that theHashfield of the response is notnil, indicating that the transaction was successfully added to the mempool.BroadcastTxSync, tests theBroadcastTxSyncmethod of the virtual machine service. It creates a new key-value transaction using theMakeTxKVfunction, broadcasts the transaction using theBroadcastTxSyncmethod of the virtual machine service, and then asserts that theCodefield of the response is equal toatypes.CodeTypeOK, indicating that the transaction was successfully checked and committed to the blockchain.
TestHistoryService
TestHistoryServiceThe TestHistoryService unit test in the service_test.go file. Overall, the TestHistoryService unit test is testing several methods of the virtual machine service related to blockchain history and verifying that they behave as expected. Here's a breakdown of what the test is doing:
The
TestHistoryServicefunction starts by calling themustNewCounterTestVmfunction to create a new instance of the virtual machine and virtual machine service, and assigns them to thevmandservicevariables.The test then broadcasts a new transaction using the
BroadcastTxSyncmethod of the virtual machine service and verifies that the response code isatypes.CodeTypeOK.The test then builds and accepts a new block using the virtual machine.
The test then runs two subtests using the
t.Runmethod, each of which tests a different method of the virtual machine service.
Subtests
BlockchainInfotests theBlockchainInfomethod of the virtual machine service. It creates a newctypes.ResultBlockchainInfovariable and passes it to theBlockchainInfomethod with astartheight of1and anendheight of100. It then asserts that there is no error returned by the method and that theLastHeightfield of the response is equal to1.Genesistests theGenesismethod of the virtual machine service. It creates a newctypes.ResultGenesisvariable and passes it to theGenesismethod. It then asserts that there is no error returned by the method and that theGenesisfield of the response is equal to the expected value from the virtual machine's genesis configuration.
TestNetworkService
TestNetworkServiceThe TestNetworkService unit test in the service_test.go file. Overall, the TestNetworkService unit test is testing several methods of the virtual machine service related to networking and consensus and verifying that they behave as expected.
Here's a breakdown of what the test is doing:
The
TestNetworkServicefunction starts by calling themustNewCounterTestVmfunction to create a new instance of the virtual machine and virtual machine service, and assigns them to thevmandservicevariables.The test then runs five subtests using the
t.Runmethod, each of which tests a different method of the virtual machine service related to networking and consensus.
Subtests
NetInfotests theNetInfomethod of the virtual machine service. It creates a newctypes.ResultNetInfovariable and passes it to theNetInfomethod. It then asserts that there is no error returned by the method.DumpConsensusStatetests theDumpConsensusStatemethod of the virtual machine service. It creates a newctypes.ResultDumpConsensusStatevariable and passes it to theDumpConsensusStatemethod. It then asserts that there is no error returned by the method.ConsensusStatetests theConsensusStatemethod of the virtual machine service. It creates a newctypes.ResultConsensusStatevariable and passes it to theConsensusStatemethod. It then asserts that there is no error returned by the method.ConsensusParamstests theConsensusParamsmethod of the virtual machine service. It creates a newctypes.ResultConsensusParamsvariable and passes it to theConsensusParamsmethod. It then asserts that there is no error returned by the method and that theBlockHeightfield of the response is equal to0. It then broadcasts a new transaction using theBroadcastTxSyncmethod of the virtual machine service, builds and accepts a new block using the virtual machine, and asserts that theBlockHeightfield of theConsensusParamsresponse is equal to1.Healthtests theHealthmethod of the virtual machine service. It creates a newctypes.ResultHealthvariable and passes it to theHealthmethod. It then asserts that there is no error returned by the method.
TestSignService
TestSignServiceSure, I can help you document the TestSignService unit test in the service_test.go file. Here's a breakdown of what the test is doing:
The
TestSignServicefunction starts by creating a new key-value transaction (tx) and creating a new instance of the virtual machine and virtual machine service, which it assigns to thevmandservicevariables.The test then performs several subtests using the
t.Runmethod, each of which tests a different method of the virtual machine service related to signing and verification of transactions.TheTestSignServicehas the following subtests:
Subtests
Blockwhich tests theBlockmethod of the virtual machine service with a valid block height. It creates two newctypes.ResultBlockvariables and passes them to theBlockmethod with aHeightparameter of the block height. It then asserts that both methods return anilerror and that theHeightfield of the response is equal to the expected block height.BlockByHashwhich tests theBlockByHashmethod of the virtual machine service with a valid block hash. It creates two newctypes.ResultBlockvariables and passes them to theBlockByHashmethod with aHashparameter of the block hash. It then asserts that both methods return anilerror and that theHashfield of the response is equal to the expected block hash.BlockResultswhich tests theBlockResultsmethod of the virtual machine service with a valid block height. It creates two newctypes.ResultBlockResultsvariables and passes them to theBlockResultsmethod with aHeightparameter of the block height. It then asserts that both methods return anilerror and that theHeightfield of the response is equal to the expected block height.Txwhich tests theTxmethod of the virtual machine service with a valid transaction hash. It creates a newctypes.ResultTxvariable and passes it to theTxmethod with aHashparameter of the transaction hash. It then asserts that the method returns anilerror and that theHashandTxfields of the response are equal to the expected transaction hash and transaction, respectively.
Overall, the TestSignService unit test is testing several methods of the virtual machine service related to signing and verification of transactions and verifying that they behave as expected.
TestStatusService
TestStatusServiceThe TestStatusService unit test in the service_test.go file. Here's a breakdown of what the test is doing:
The
TestStatusServicefunction starts by creating a new instance of the virtual machine and virtual machine service, which it assigns to thevmandservicevariables.The test then attempts to build a new block from the virtual machine with no pending transactions. It expects this operation to fail with an
errNoPendingTxserror and anilblock.Next, the test creates a new key-value transaction (
tx) and uses theBroadcastTxSyncmethod of the virtual machine service to broadcast the transaction. It asserts that the method returns no error and that theCodefield of the response is equal toatypes.CodeTypeOK.The test then creates a new subtest using the
t.Runmethod, which tests theStatusmethod of the virtual machine service with two different blocks. It creates two newctypes.ResultStatusvariables and passes them to theStatusmethod. The first call is made with no arguments and expects the response to contain aLatestBlockHeightfield with a value of0. The second call is made after successfully building and accepting a new block and expects the response to contain aLatestBlockHeightfield with a value of1.
Overall, the TestStatusService unit test is testing the Status method of the virtual machine service with two different blocks and verifying that the expected block height is returned in the response.
TestMempoolService
TestMempoolServiceThis unit test suite tests the functionalities of the MempoolService struct.
Sub-tests:
UnconfirmedTxsDescription: Tests the functionality of retrieving unconfirmed transactions in the mempool.
Steps:
Build a transaction and broadcast it to the mempool.
Call the
UnconfirmedTxsfunction of theMempoolService.
Expected Results: The function should return the single transaction that was broadcasted to the mempool.
NumUnconfirmedTxsDescription: Tests the functionality of retrieving the number of unconfirmed transactions in the mempool.
Steps:
Build a transaction and broadcast it to the mempool.
Call the
NumUnconfirmedTxsfunction of theMempoolService.
Expected Results: The function should return a count of 1, indicating the single transaction that was broadcasted to the mempool.
CheckTxDescription: Tests the functionality of checking if a transaction is valid and can be accepted into the mempool.
Steps:
Build a transaction and broadcast it to the mempool.
Call the
CheckTxfunction of theMempoolServicewith the same transaction.Build a block and accept it.
Call the
CheckTxfunction of theMempoolServicewith the same transaction.
Expected Results:
The first
CheckTxfunction should return no errors, indicating the transaction is valid and can be accepted into the mempool.The second
CheckTxfunction should return an error, indicating the transaction has already been accepted into the blockchain and cannot be accepted into the mempool again.
Last updated
Was this helpful?