Skip to content

tests: fix global variable corruption, make tests execution order-independent#646

Merged
DeckerSU merged 34 commits intodevfrom
patch-fix-unit-tests
Mar 23, 2025
Merged

tests: fix global variable corruption, make tests execution order-independent#646
DeckerSU merged 34 commits intodevfrom
patch-fix-unit-tests

Conversation

@DeckerSU
Copy link
Copy Markdown

@DeckerSU DeckerSU commented Mar 14, 2025

As described here, it turns out that the success of unit test execution depended on the order in which they were run. For example, a test sequence of 1,2,3 might complete successfully, but 1,3,2 could fail. Additionally, running these tests individually, such as just 1 or just 3, showed that they worked correctly.

The root cause was that some tests modified global variables, which affected the execution of other tests. Most of the issues related to modifying global variables within the tests have now been fixed, so the results should no longer depend on the execution order.

p.s. Google Test allows you to randomize the order of test execution. To do this, you can use the command-line flag --gtest_shuffle. If you want to be able to reproduce the test order, you can also specify the flag --gtest_random_seed with a specific number. For example:

./komodo-test --gtest_shuffle --gtest_random_seed=123

This way, the tests will run in a random order, and by specifying a seed, you can reproduce the same order in the future.

So, unit tests should always be run with --gtest_shuffle to ensure that all tests pass successfully, regardless of their execution order.

p.p.s. Instruction for testers/reviewers: #646 (comment) .

DeckerSU added 16 commits March 14, 2025 19:47
Windows specifics: The OS more strictly locks files if they are in use, even if their usage has been completed in the code. We should not leave the komodoevents file descriptor open.

TODO: The **signedmasks** descriptor is also handled during komodod / komodo-test execution and is never closed (!)
last.MoM could be modified by other tests, so we need a workaround.
IS_KOMODO_NOTARY changes critical for LegacyEvents tests.
@DeckerSU DeckerSU changed the title tests: tests: fix global variable corruption, make tests execution order-independent Mar 14, 2025
@DeckerSU
Copy link
Copy Markdown
Author

TODO: need to fix test_block set, bcz with --gtest_random_seed=42354 it FAIL.

@DeckerSU
Copy link
Copy Markdown
Author

DeckerSU commented Mar 15, 2025

TODO: need to fix Mempool.SproutNegativeVersionTxWhenOverwinterActive segfault, bcz with --gtest_random_seed=5602.

Mempool tests expects: chainActive.vChain.size() == 0.

prevent crash in GetMedianTimePast calculation
@DeckerSU
Copy link
Copy Markdown
Author

DeckerSU commented Mar 15, 2025

TODO: Fix error with -gtest_random_seed=99425:

[ RUN      ] TestEvalNotarisation.test_komodo_notarysinit
test-komodo/test_eval_notarisation.cpp:240: Failure
      Expected: Pubkeys
      Which is: 0x616d5c826840
To be equal to: nullptr
      Which is: 8-byte object <00-00 00-00 00-00 00-00>
test-komodo/test_eval_notarisation.cpp:246: Failure
      Expected: Pubkeys[0].numnotaries
      Which is: 0
To be equal to: 35
Pubkeys[1].height 2000 < 1 hwmheight, origheight.0
Pubkeys[1].height 2000 < 2001 hwmheight, origheight.0
[  FAILED  ] TestEvalNotarisation.test_komodo_notarysinit (0 ms)

@DeckerSU
Copy link
Copy Markdown
Author

Task for Testers:

  1. Build the komodo-test binary using:
    ./zcutil/build.sh -j$(nproc)
  2. Run the test suite with randomized execution order:
    ./src/komodo-test --gtest_shuffle
  3. At the beginning of the execution, it will output a message like:
    Note: 'Randomizing tests' orders with a seed of xxxxx
    
  4. If any test fails, report:
    • The test name that failed
    • The seed value from step (3)

This will help reproduce and debug any order-dependent test failures.

@DeckerSU DeckerSU mentioned this pull request Mar 15, 2025
static hwmheight can be non-zero after komodo_init(...) after some
other tests ... so we should clear it once again.
@DeckerSU
Copy link
Copy Markdown
Author

DeckerSU commented Mar 15, 2025

Found other one error:

TODO: --gtest_random_seed=91821 in TestBet, testMakePayoutCond.

TestBet.testMakePayoutCond launched with (1) --gtest_filter=TestBet.testMakePayoutCond - PASSED, but the same test launched with the series of tests before like (2) --gtest_shuffle --gtest_random_seed=91821 - FAILED. Also there is a feeling that after execute CCShowStructure the value of ebet.SessionTx().GetHash() in (2) changed :

TEST_F(TestBet, testMakePayoutCond)
{
    CC *payoutCond = ebet.PayoutCond();
    EXPECT_EQ("(1 of (3 of 5,5,5),(2 of (1 of 5,5,5),15))", CCShowStructure(payoutCond));
    EXPECT_EQ(0, memcmp(payoutCond->subconditions[1]->subconditions[1]->code+1,
                        ebet.SessionTx().GetHash().begin(), 32));
}

@DeckerSU
Copy link
Copy Markdown
Author

TODO: --gtest_random_seed=82057, crash in GMPArithTests.RewardsTest:

[ RUN      ] GMPArithTests.RewardsTest
amount.-31536 duration.-10000000 APR.1000000 reward.1
amount.-31536 duration.-10000000 APR.2000000 reward.2
amount.-9223372036854775808 duration.-2 APR.1000000 reward.0
amount.-9223372036854775808 duration.-315360000000 APR.1000000 reward.0
Opened LevelDB successfully
Opened LevelDB successfully
fAddressIndex.0/0 fSpentIndex.0/0
Initializing databases...
Pre-allocating up to position 0x1000000 in blk00000.dat
e46e999d6de5ef95427ea51b8242ef7949de22b340b6a484ff154b9991ec9452 hash vs 0f0f0f0000000000000000000000000000000000000000000000000000000000 ht.16843010 special.0 special2.0 flag.0 notaryid.-1 mod.30 error
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb6 <- pubkey
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb6 <- origpubkey
e46e999d6de5ef95427ea51b8242ef7949de22b340b6a484ff154b9991ec9452 failed hash ht.0
ERROR: CheckBlock: proof of work failed
InvalidChainFound: invalid block=e46e999d6de5ef95427ea51b8242ef7949de22b340b6a484ff154b9991ec9452  height=0  log2_work=4.0874628  date=2011-02-02 23:16:42
komodo-test: main.cpp:2626: void InvalidChainFound(CBlockIndex*): Assertion `tip' failed.
Aborted (core dumped)

@DeckerSU DeckerSU force-pushed the patch-fix-unit-tests branch from 2980f7d to bd677f8 Compare March 18, 2025 00:07
@DeckerSU
Copy link
Copy Markdown
Author

Small script for tests automation:

#!/bin/bash
counter=1
while true; do
  echo "Now launching tests ... $counter"
  output=$(./src/komodo-test --gtest_shuffle 2>&1)
  ret=$?
  seed=$(echo "$output" | grep -oP 'seed of \K[0-9]+')
  if [ $ret -ne 0 ]; then
    echo -e "\033[31mSeed: $seed\033[0m"
    break
  fi
  counter=$((counter + 1))
done

@DeckerSU
Copy link
Copy Markdown
Author

So far, I’ve completed more than 1500 test iterations with randomized test order, so I believe the fixes are ready to be merged. I will also assign reviewers, but I don’t expect them to review the test logic in detail (I understand that could be difficult at this stage).

Instead, I just need confirmation that ./src/komodo-test --gtest_shuffle runs successfully without any failures.

Instructions on how to run the tests with randomized order are available here: #646 (comment).

@DeckerSU DeckerSU requested review from gcharang and smk762 March 19, 2025 07:51
@smk762
Copy link
Copy Markdown

smk762 commented Mar 19, 2025

Caught an error here with seed 63697:

test-komodo/test_eval_notarisation.cpp:368: Failure
      Expected: num_found
      Which is: 35
To be equal to: 1
test-komodo/test_eval_notarisation.cpp:369: Failure
      Expected: keys[0][0]
      Which is: '\x3' (3)
To be equal to: 0x00
      Which is: 0
test-komodo/test_eval_notarisation.cpp:370: Failure
      Expected: keys[0][1]
      Which is: '\xB7' (183)
To be equal to: 0x01
      Which is: 1
[  FAILED  ] TestEvalNotarisation.test_komodo_notaries (32 ms)

Same error throw with seed 82429

@DeckerSU
Copy link
Copy Markdown
Author

DeckerSU commented Mar 19, 2025

@smk762:

Caught an error here with seed 63697

Good catch. Fixed in 4e1a27d.

Same error throw with seed 82429

The last fix should have addressed this as well.

Copy link
Copy Markdown

@smk762 smk762 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed previous failing seeds are now functional. Re-ran for another 1000 random seed iterations and encountered no further failing tests.

Copy link
Copy Markdown

@gcharang gcharang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ran the test script for more than 1700 iterations, no errors found.

@DeckerSU DeckerSU merged commit 9c84836 into dev Mar 23, 2025
17 of 18 checks passed
TheComputerGenie pushed a commit to ComputerGenieCo/komodo that referenced this pull request Sep 12, 2025
…ependent (GLEECBTC#646)

* tests, debug: add debug printout for Global Variables

* tests: fix fTxIndex set after exit from GMPArithTests.RewardsTest

DeckerSU/KomodoOcean#95 (comment)

* tests: fix LegacyEventsTests removing temp directory, close komodoevents

Windows specifics: The OS more strictly locks files if they are in use, even if their usage has been completed in the code. We should not leave the komodoevents file descriptor open.

TODO: The **signedmasks** descriptor is also handled during komodod / komodo-test execution and is never closed (!)

* tests: prevent ASSETCHAINS_MAGIC corruption in ParseArgumentsTests

* tests: prevent ASSETCHAINS_CCDISABLES corruption in ParseArgumentsTests

* tests: move deleteIfUsedBefore to testutils.h

* tests: fix TestCoinImport global variables set/unset

* debug: add assetchain output stream operator<<

* tests: disable GLEEC example, as an old GLEEC chain now require -datadir

* tests: fix TestNotary.* tests set, now it's tied to NUM_KMD_SEASONS

* tests: fix global fTxIndex state in TestChain

* tests: fix TestParseNotarisation.FilePaths test

* test: fix consensus params after chainparams_commandline call

* tests: fix TestAlerts.AlertNotify test

* tests: fix TestParseNotarisation.test_prevMoMheight tests

last.MoM could be modified by other tests, so we need a workaround.

* tests: fix TestParseNotarisation tests

IS_KOMODO_NOTARY changes critical for LegacyEvents tests.

* tests: check IS_KOMODO_NOTARY == false condition, for LegacyEventsTests

* tests: fix IsInitialBlockDownload() debug output in LegacyEventsTests

we want to output value, not address ...

* tests: prevent ASSETCHAINS_CC corruption

* tests: prevent ASSETCHAINS_COMMISSION corruption

* tests: add test_block suite comments

* tests: make Mempool tests independent from chainActive state

prevent crash in GetMedianTimePast calculation

* tests: fix TestEvalNotarisation.test_komodo_notarysinit test

* test: fix TestEvalNotarisation.test_komodo_notaries test

* tests: temp fix for TestNotary.KomodoNotaries test

* tests: fix TestEvalNotarisation.test_komodo_notarysinit

static hwmheight can be non-zero after komodo_init(...) after some
other tests ... so we should clear it once again.

* CC: fix CCShowStructure behavior if nullptr is passed

* tests: fix all TestBet.* set (undefined behavior inside komodo_nextheight)

* tests: move GTEST_COUT_COLOR to testutils.h

* tests: add GetNetworkByIdStr function (id_str -> network)

* tests: correct restore old network in Mempool.* and PoW.* tests

* tests: fix GMPArithTests.RewardsTest (should use REGTEST)

* tests: fix TestParseNotarisation.prevMoMheight test

* tests: fix accessing real ~/.komodo directory instead set -datadir

because of path caching ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants