Skip to content

Commit

Permalink
Feature/tests timeouts (#287)
Browse files Browse the repository at this point in the history
* Wrapped most of integration tests with awkward JUnit5 timeouts
  • Loading branch information
Mingela authored and Alexey-N-Chernyshov committed Dec 14, 2018
1 parent 6236f36 commit 48b7f68
Show file tree
Hide file tree
Showing 17 changed files with 1,463 additions and 1,263 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package integration.eth

import integration.helper.ConfigHelper
import integration.helper.IntegrationHelperUtil
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
Expand All @@ -10,6 +11,7 @@ import sidechain.iroha.CLIENT_DOMAIN
import util.getRandomString
import java.math.BigDecimal
import java.math.BigInteger
import java.time.Duration

/**
* Integration tests for deposit case.
Expand Down Expand Up @@ -41,6 +43,8 @@ class DepositIntegrationTest {
return integrationHelper.registerClient(clientIrohaAccount, listOf())
}

private val timeoutDuration = Duration.ofMinutes(ConfigHelper.timeoutMinutes)

@AfterAll
fun dropDown() {
integrationHelper.close()
Expand All @@ -56,16 +60,18 @@ class DepositIntegrationTest {
*/
@Test
fun depositOfETH() {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val amount = BigInteger.valueOf(1_234_000_000_000)
// send ETH
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val amount = BigInteger.valueOf(1_234_000_000_000)
// send ETH
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
}
}

/**
Expand All @@ -79,26 +85,28 @@ class DepositIntegrationTest {
*/
@Test
fun depositZeroETH() {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val zeroAmount = BigInteger.ZERO
val amount = BigInteger.valueOf(1_234_000_000_000)

// send 0 ETH
integrationHelper.sendEth(zeroAmount, relayWallet)

Assertions.assertEquals(
initialAmount,
integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
)

// Send again 1234000000000 Ethereum network
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val zeroAmount = BigInteger.ZERO
val amount = BigInteger.valueOf(1_234_000_000_000)

// send 0 ETH
integrationHelper.sendEth(zeroAmount, relayWallet)

Assertions.assertEquals(
initialAmount,
integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
)

// Send again 1234000000000 Ethereum network
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
}
}

/**
Expand All @@ -111,19 +119,21 @@ class DepositIntegrationTest {
*/
@Test
fun depositOfERC20() {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val amount = BigInteger.valueOf(51)

// send ETH
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val amount = BigInteger.valueOf(51)

// send ETH
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
}
}

/**
Expand All @@ -136,25 +146,29 @@ class DepositIntegrationTest {
*/
@Test
fun depositZeroOfERC20() {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val zeroAmount = BigInteger.ZERO
val amount = BigInteger.valueOf(51)

// send 0 ERC20
integrationHelper.sendERC20Token(tokenAddress, zeroAmount, relayWallet)

Assertions.assertEquals(initialAmount, integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))

// Send again
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val zeroAmount = BigInteger.ZERO
val amount = BigInteger.valueOf(51)

// send 0 ERC20
integrationHelper.sendERC20Token(tokenAddress, zeroAmount, relayWallet)

Assertions.assertEquals(
initialAmount,
integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
)

// Send again
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration.eth

import config.IrohaCredentialConfig
import config.loadEthPasswords
import integration.helper.ConfigHelper
import integration.helper.IntegrationHelperUtil
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
Expand All @@ -13,6 +14,7 @@ import sidechain.iroha.util.ModelUtil
import util.getRandomString
import java.math.BigDecimal
import java.math.BigInteger
import java.time.Duration

/**
* Integration tests with multiple notaries for deposit case.
Expand Down Expand Up @@ -44,6 +46,8 @@ class DepositMultiIntegrationTest {
/** Path to private key of 2nd instance of notary */
private val privkeyPath2 = "deploy/iroha/keys/notary1@notary.priv"

private val timeoutDuration = Duration.ofMinutes(ConfigHelper.timeoutMinutes)

init {
// run notary
integrationHelper.runEthNotary()
Expand Down Expand Up @@ -90,16 +94,18 @@ class DepositMultiIntegrationTest {
*/
@Test
fun depositMultisig() {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val amount = BigInteger.valueOf(1_234_000_000_000)
// send ETH
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId)
val amount = BigInteger.valueOf(1_234_000_000_000)
// send ETH
integrationHelper.sendEth(amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, ETH_PRECISION.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, etherAssetId))
)
}
}

/**
Expand All @@ -112,19 +118,20 @@ class DepositMultiIntegrationTest {
*/
@Test
fun depositMultisigERC20() {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val amount = BigInteger.valueOf(51)

// send ETH
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
Assertions.assertTimeoutPreemptively(timeoutDuration) {
val (tokenInfo, tokenAddress) = integrationHelper.deployRandomERC20Token(2)
val assetId = "${tokenInfo.name}#ethereum"
val initialAmount = integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId)
val amount = BigInteger.valueOf(51)

// send ETH
integrationHelper.sendERC20Token(tokenAddress, amount, relayWallet)
integrationHelper.waitOneIrohaBlock()

Assertions.assertEquals(
BigDecimal(amount, tokenInfo.precision.toInt()).add(BigDecimal(initialAmount)),
BigDecimal(integrationHelper.getIrohaAccountBalance(clientIrohaAccountId, assetId))
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package integration.eth
import com.github.kittinunf.result.failure
import com.github.kittinunf.result.map
import com.google.gson.Gson
import integration.helper.ConfigHelper
import integration.helper.IntegrationHelperUtil
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.assertTimeoutPreemptively
import provider.eth.EthTokensProviderImpl
import sidechain.iroha.consumer.IrohaNetworkImpl
import token.EthTokenInfo
import token.executeTokenRegistration
import util.getRandomString
import java.io.File
import java.time.Duration

/**
* Requires Iroha is running
Expand All @@ -33,6 +36,8 @@ class ERC20TokenRegistrationTest {
tokenRegistrationConfig.irohaCredential.accountId
)

private val timeoutDuration = Duration.ofMinutes(ConfigHelper.timeoutMinutes)

@AfterEach
fun clearFile() {
File(tokensFilePath).delete()
Expand All @@ -53,22 +58,24 @@ class ERC20TokenRegistrationTest {
*/
@Test
fun testTokenRegistration() {
val tokens = createRandomTokens()
createTokensFile(tokens, tokensFilePath)
executeTokenRegistration(tokenRegistrationConfig)
ethTokensProvider.getTokens()
.map { tokensFromProvider ->
val expected = tokensFromProvider
.map { (ethAddress, name) ->
Pair(ethAddress, EthTokenInfo(name, ethTokensProvider.getTokenPrecision(name).get()))
}.sortedBy { it.first }
assertTimeoutPreemptively(timeoutDuration) {
val tokens = createRandomTokens()
createTokensFile(tokens, tokensFilePath)
executeTokenRegistration(tokenRegistrationConfig)
ethTokensProvider.getTokens()
.map { tokensFromProvider ->
val expected = tokensFromProvider
.map { (ethAddress, name) ->
Pair(ethAddress, EthTokenInfo(name, ethTokensProvider.getTokenPrecision(name).get()))
}.sortedBy { it.first }

assertEquals(
tokens.toList().sortedBy { it.first },
expected
)
}
.failure { ex -> fail("cannot fetch tokens", ex) }
assertEquals(
tokens.toList().sortedBy { it.first },
expected
)
}
.failure { ex -> fail("cannot fetch tokens", ex) }
}
}

/**
Expand All @@ -80,11 +87,13 @@ class ERC20TokenRegistrationTest {
*/
@Test
fun testTokenRegistrationEmptyTokenFile() {
createTokensFile(HashMap(), tokensFilePath)
executeTokenRegistration(tokenRegistrationConfig)
ethTokensProvider.getTokens().fold({ tokensFromProvider ->
assertTrue(tokensFromProvider.isEmpty())
}, { ex -> fail("cannot fetch tokens", ex) })
assertTimeoutPreemptively(timeoutDuration) {
createTokensFile(HashMap(), tokensFilePath)
executeTokenRegistration(tokenRegistrationConfig)
ethTokensProvider.getTokens().fold({ tokensFromProvider ->
assertTrue(tokensFromProvider.isEmpty())
}, { ex -> fail("cannot fetch tokens", ex) })
}
}

//Creates json file full of ERC20 tokens
Expand Down
Loading

0 comments on commit 48b7f68

Please sign in to comment.