Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add helper method for overriding env variables #3022

Merged
merged 28 commits into from
Oct 11, 2024

Conversation

victor-yanev
Copy link
Contributor

@victor-yanev victor-yanev commented Sep 20, 2024

Description:

In the current setup, there is no straightforward way to temporarily override environment variables for the duration of a test suite. This can lead to issues where tests inadvertently affect each other due to shared environment settings, making it difficult to isolate and reproduce test failures.

Additionally, in many tests environment variables are overridden without being reverted, propagating this behavior to other tests and making the tests dependent on their order and leading to chain-failures in cases where only one of the tests actually fails.

Changes:

This PR adds new helper methods overrideEnvs and withOverriddenEnvs which provide a mechanism to temporarily set environment variables for the duration of a test or a group of tests.

ℹ️ These methods ensure that the original environment settings are restored after the tests have run, maintaining the integrity of the global environment.

  • overrideEnvs: Temporarily sets environment variables before a test and restores them afterward.
  • withOverriddenEnvs: A higher-level function that uses overrideEnvs to set environment variables for a group of tests defined within a callback function.

Related issue(s):

Fixes #3029

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

@victor-yanev victor-yanev added enhancement New feature or request Technical Debt Issue which resolves technical debt labels Sep 20, 2024
@victor-yanev victor-yanev self-assigned this Sep 20, 2024
Copy link

github-actions bot commented Sep 20, 2024

Acceptance Tests

  18 files  244 suites   30m 7s ⏱️
601 tests 594 ✔️ 4 💤 3
695 runs  686 ✔️ 6 💤 3

Results for commit 358fff3.

♻️ This comment has been updated with latest results.

Signed-off-by: Victor Yanev <[email protected]>
Signed-off-by: Victor Yanev <[email protected]>
Copy link

github-actions bot commented Sep 25, 2024

Tests

       3 files     372 suites   18s ⏱️
1 367 tests 1 366 ✔️ 1 💤 0
1 376 runs  1 375 ✔️ 1 💤 0

Results for commit 358fff3.

♻️ This comment has been updated with latest results.

Copy link
Collaborator

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

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

LG, final comment is around method naming to make things obvious and avoid wasted troubleshooting time form this improvement

packages/relay/tests/helpers.ts Outdated Show resolved Hide resolved
@ebadiere ebadiere modified the milestones: 0.57.0, 0.58.0 Sep 30, 2024
Copy link
Collaborator

@ebadiere ebadiere left a comment

Choose a reason for hiding this comment

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

LG, but now there is a merge conflict.

Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/src/lib/services/ethService/ethFilterService/index.ts
#	packages/relay/tests/lib/clients/localLRUCache.spec.ts
#	packages/relay/tests/lib/eth/eth_call.spec.ts
#	packages/relay/tests/lib/eth/eth_estimateGas.spec.ts
#	packages/relay/tests/lib/eth/eth_feeHistory.spec.ts
#	packages/relay/tests/lib/eth/eth_gasPrice.spec.ts
#	packages/relay/tests/lib/eth/eth_getBalance.spec.ts
#	packages/relay/tests/lib/eth/eth_getBlockByHash.spec.ts
#	packages/relay/tests/lib/eth/eth_getBlockByNumber.spec.ts
#	packages/relay/tests/lib/eth/eth_getCode.spec.ts
#	packages/relay/tests/lib/eth/eth_getLogs.spec.ts
#	packages/relay/tests/lib/eth/eth_getStorageAt.spec.ts
#	packages/relay/tests/lib/eth/eth_getTransactionCount.spec.ts
#	packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts
#	packages/relay/tests/lib/hapiService.spec.ts
#	packages/relay/tests/lib/hbarLimiter.spec.ts
#	packages/relay/tests/lib/mirrorNodeClient.spec.ts
#	packages/relay/tests/lib/precheck.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/ipAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/sdkClient.spec.ts
#	packages/relay/tests/lib/services/cacheService/cacheService.spec.ts
#	packages/relay/tests/lib/services/debugService/debug.spec.ts
#	packages/relay/tests/lib/services/eth/filter.spec.ts
#	packages/relay/tests/lib/services/metricService/metricService.spec.ts
#	packages/relay/tests/lib/web3.spec.ts
#	packages/server/tests/acceptance/cacheService.spec.ts
#	packages/server/tests/acceptance/hbarLimiter.spec.ts
#	packages/server/tests/acceptance/rpc_batch3.spec.ts
#	packages/ws-server/tests/acceptance/subscribeNewHeads.spec.ts
#	packages/ws-server/tests/unit/utils.spec.ts
#	packages/ws-server/tests/unit/validations.spec.ts
Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts
Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts
#	packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts
konstantinabl
konstantinabl previously approved these changes Oct 10, 2024
Copy link

sonarcloud bot commented Oct 11, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
12.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

Copy link

🚨 Memory Leak Detected 🚨

A potential memory leak has been detected in the test titled validates enforcement of request id. This may impact the application's performance and stability.

Details

📊 Memory Leak Detection Report 📊

GC Type: MarkSweepCompact
Cost: 24,265.6 ms

Heap Statistics (before vs after executing the test):

  • Total Heap Size: increased with 1.72 MB
  • Total Heap Size Executable: no changes
  • Total Physical Size: decreased with 344.06 KB
  • Total Available Size: increased with 2.13 MB
  • Total Global Handles Size: no changes
  • Used Global Handles Size: decreased with 64.00 bytes
  • Used Heap Size: decreased with 3.28 MB
  • Heap Size Limit: no changes
  • Malloced Memory: no changes
  • External Memory: no changes
  • Peak Malloced Memory: no changes

Heap Space Statistics (before vs after executing the test):

  • Old Space:

    • Space Size: increased with 2.10 MB
    • Space Used Size: increased with 2.08 MB
    • Space Available Size: decreased with 506.37 KB
    • Physical Space Size: increased with 2.10 MB
  • Large Object Space:

    • Space Size: increased with 835.58 KB
    • Space Used Size: increased with 813.50 KB
    • Space Available Size: no changes
    • Physical Space Size: increased with 835.58 KB

Recommendations

Please investigate the memory allocations in this test, focusing on objects that are not being properly deallocated.

@victor-yanev victor-yanev merged commit 2e2fc07 into main Oct 11, 2024
42 of 43 checks passed
@victor-yanev victor-yanev deleted the add-helper-method-for-overriding-env-variables branch October 11, 2024 14:11
Copy link

codecov bot commented Oct 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.54%. Comparing base (4a69be9) to head (358fff3).
Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3022      +/-   ##
==========================================
- Coverage   84.88%   82.54%   -2.34%     
==========================================
  Files          43       61      +18     
  Lines        3216     4063     +847     
  Branches      649      806     +157     
==========================================
+ Hits         2730     3354     +624     
- Misses        283      471     +188     
- Partials      203      238      +35     
Flag Coverage Δ
relay 84.95% <ø> (+0.06%) ⬆️
server 83.43% <ø> (?)
ws-server 33.91% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../lib/services/ethService/ethFilterService/index.ts 79.51% <ø> (ø)

... and 20 files with indirect coverage changes

konstantinabl pushed a commit that referenced this pull request Oct 15, 2024
* test: add helper method for overriding env variables

Signed-off-by: Victor Yanev <[email protected]>

* chore: improve readability

Signed-off-by: Victor Yanev <[email protected]>

* fix: usages

Signed-off-by: Victor Yanev <[email protected]>

* fix: cacheService.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: utils.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: relay.spec.ts and sdkClient.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: eth_call.spec.ts, eth_estimateGas.spec.ts, ethGetBlockBy.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: use helper method to override envs in acceptance test specs

Signed-off-by: Victor Yanev <[email protected]>

* fix: hapiService.spec.ts and precheck.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: sdkClient.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: cacheService.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: metricService.spec.ts and subscriptionController.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: rateLimiter.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: failing tests

Signed-off-by: Victor Yanev <[email protected]>

* chore: revert some changes in rpc_batch3.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: subscribe.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* chore: add docs with examples to `overrideEnvs` and `withOverriddenEnvs`

Signed-off-by: Victor Yanev <[email protected]>

* chore: address comments

Signed-off-by: Victor Yanev <[email protected]>

* fix: build image test

Signed-off-by: Victor Yanev <[email protected]>

* Merge branch 'main' into add-helper-method-for-overriding-env-variables

Signed-off-by: Victor Yanev <[email protected]>

* chore: address comments + fix conflicts after merge

Signed-off-by: Victor Yanev <[email protected]>

* chore: optimize TTL tests in localLRUCache.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* fix: failing unit test after merge from main

Signed-off-by: Victor Yanev <[email protected]>

* chore: optimize imports after merge from main

Signed-off-by: Victor Yanev <[email protected]>

---------

Signed-off-by: Victor Yanev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Technical Debt Issue which resolves technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Helper Methods for Overriding Environment Variables in Tests
6 participants