Skip to content

Commit

Permalink
2020 05 19 improve test performance (#1449)
Browse files Browse the repository at this point in the history
* Reduce number of property based tests on BIP32PathTest and add a Future to NetworkPayloadTest's property based tests so it hopefully runs faster on CI

* Run scalafmt

* Make a few more property based tests async

* Make forAllParallel, refactor some test cases to use it

* Make BlockMessageTest async

* Bump timeout
  • Loading branch information
Christewart committed May 20, 2020
1 parent 5c7585c commit 920f0c3
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 43 deletions.
Expand Up @@ -15,7 +15,7 @@ import org.bitcoins.crypto.{
ECPrivateKey,
EmptyDigitalSignature
}
import org.bitcoins.testkit.util.TransactionTestUtil
import org.bitcoins.testkit.util.{BitcoinSAsyncTest, TransactionTestUtil}
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{FlatSpec, MustMatchers}
import scodec.bits.ByteVector
Expand All @@ -25,11 +25,7 @@ import scala.concurrent.Future
/**
* Created by chris on 7/21/16.
*/
class TransactionSignatureCreatorTest
extends FlatSpec
with MustMatchers
with ScalaFutures {
private def logger = BitcoinSLogger.logger
class TransactionSignatureCreatorTest extends BitcoinSAsyncTest {

"TransactionSignatureCreator" must "create a signature for a scriptSignature in a transaction" in {
//this is a signed tx, but since TransactionSignatureSerializer removes scriptSigs, it will work for testing this
Expand Down Expand Up @@ -264,7 +260,6 @@ class TransactionSignatureCreatorTest
}

it must "be able to use a sign function that returns a Future[ECDigitalSignature] and have the sig validate" in {
import scala.concurrent.ExecutionContext.Implicits.global
val privateKey = ECPrivateKey()
val publicKey = privateKey.publicKey
val redeemScript = MultiSignatureScriptPubKey(1, Seq(publicKey))
Expand Down Expand Up @@ -312,9 +307,9 @@ class TransactionSignatureCreatorTest
PreExecutionScriptProgram(signedTxSigComponent)
}

val result = program.map(ScriptInterpreter.run(_))
whenReady(result) { r =>
r must be(ScriptOk)
val resultF = program.map(ScriptInterpreter.run(_))
resultF.map { r =>
assert(r == ScriptOk)
}
}

Expand Down
Expand Up @@ -16,7 +16,7 @@ import scala.util.{Success, Try}
class BIP32PathTest extends BitcoinSUnitTest {

implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
generatorDrivenConfigNewCode
generatorDriveConfigOldCode

behavior of "BIP32Child"

Expand Down
@@ -1,11 +1,11 @@
package org.bitcoins.core.p2p

import org.bitcoins.testkit.util.BitcoinSUnitTest
import org.bitcoins.testkit.core.gen.p2p.DataMessageGenerator
import org.bitcoins.testkit.util.BitcoinSAsyncTest

class BlockMessageTest extends BitcoinSUnitTest {
class BlockMessageTest extends BitcoinSAsyncTest {
it must "have serialization symmetry" in {
forAll(DataMessageGenerator.blockMessage) { block =>
forAllParallel(DataMessageGenerator.blockMessage) { block =>
assert(block == BlockMessage.fromBytes(block.bytes))
}
}
Expand Down
@@ -1,12 +1,12 @@
package org.bitcoins.core.p2p

import org.bitcoins.testkit.core.gen.p2p.DataMessageGenerator
import org.bitcoins.testkit.util.BitcoinSUnitTest
import org.bitcoins.testkit.util.BitcoinSAsyncTest

class HeadersMessageTest extends BitcoinSUnitTest {
class HeadersMessageTest extends BitcoinSAsyncTest {

it must "have serialization symmetry" in {
forAll(DataMessageGenerator.headersMessage) { headersMsg =>
forAllParallel(DataMessageGenerator.headersMessage) { headersMsg =>
assert(HeadersMessage(headersMsg.hex) == headersMsg)
}
}
Expand Down
Expand Up @@ -2,10 +2,12 @@ package org.bitcoins.core.p2p

import org.bitcoins.core.config.{MainNet, TestNet3}
import org.bitcoins.testkit.node.NodeTestUtil
import org.bitcoins.testkit.util.BitcoinSUnitTest
import org.bitcoins.testkit.util.{BitcoinSAsyncTest, BitcoinSUnitTest}
import org.bitcoins.testkit.core.gen.p2p.P2PGenerator

class NetworkPayloadTest extends BitcoinSUnitTest {
import scala.concurrent.Future

class NetworkPayloadTest extends BitcoinSAsyncTest {

"NetworkMessage" must "create a payload object from it's network header and the payload bytes" in {
val rawNetworkMessage = NodeTestUtil.rawNetworkMessage
Expand All @@ -25,7 +27,7 @@ class NetworkPayloadTest extends BitcoinSUnitTest {
// this tests has a bunch of messages to choose between, so we set a high config value
implicit override val generatorDrivenConfig = customGenDrivenConfig(100)
it must "parse messages based on its command name" in {
forAll(P2PGenerator.message) { p2p =>
forAllParallel(P2PGenerator.message) { p2p =>
val bytes = p2p.bytes
val parser = NetworkPayload.readers(p2p.commandName)
assert(parser(bytes) == p2p)
Expand Down

This file was deleted.

@@ -1,14 +1,14 @@
package org.bitcoins.core.protocol.blockchain

import org.bitcoins.testkit.util.BitcoinSUnitTest
import org.slf4j.LoggerFactory
import org.bitcoins.testkit.core.gen.BlockchainElementsGenerator
import org.bitcoins.testkit.util.BitcoinSAsyncTest

import scala.io.Source

/**
* Created by chris on 7/15/16.
*/
class BlockTest extends BitcoinSUnitTest {
class BlockTest extends BitcoinSAsyncTest {

def timeBlockParsing[R](block: => R): Long = {
val t0 = System.currentTimeMillis()
Expand Down Expand Up @@ -43,4 +43,12 @@ class BlockTest extends BitcoinSUnitTest {
val time = timeBlockParsing(Block.fromHex(lines))
assert(time <= 15000)
}

it must "have serialization symmetry" in {
forAllParallel(BlockchainElementsGenerator.block) { block =>
val result = Block(block.hex) == block
if (!result) logger.warn("block.hex: " + block.hex)
assert(result)
}
}
}
Expand Up @@ -42,7 +42,7 @@ import scala.concurrent.duration.DurationInt

//TODO: Need to provide generators for [[NonStandardScriptSignature]] and [[NonStandardScriptPubKey]]
sealed abstract class ScriptGenerators extends BitcoinSLogger {
val timeout = 5.seconds
val timeout = 15.seconds
val defaultMaxDepth: Int = 2

/** Since redeem scripts are pushed onto the stack, this function
Expand Down
Expand Up @@ -195,6 +195,16 @@ trait BaseAsyncTest

sequenceTestRuns(testRunFs)
}

/** Runs all property based tests in parallel. This is a convinient optimization
* for synchronous property based tests */
def forAllParallel[A](gen: Gen[A])(func: A => Assertion): Future[Assertion] = {
forAllAsync(gen) { a: A =>
Future {
func(a)
}
}
}
}

/** A trait that uses [[AsyncFlatSpec]] to execute tests
Expand Down

0 comments on commit 920f0c3

Please sign in to comment.