Skip to content

Commit

Permalink
add dotenv for handling test env
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Jan 12, 2025
1 parent 9db796b commit 88a58d2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This is a template for using a .env file to configure your test setup
#
# $ cp .env.template .env

ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT=20
ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT=1000
# ENTROPY_SDK_TESTS_DONT_KILL=true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
docs
dist
.env

# Created by https://www.toptal.com/developers/gitignore/api/macos,node
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,node
Expand Down Expand Up @@ -193,4 +194,4 @@ prepTests.sh
*.sublime-workspace

# Testing npm package
bootstrap/
bootstrap/
35 changes: 27 additions & 8 deletions dev/testing-utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ import { fileURLToPath } from 'url';
import { execFileSync } from 'child_process';
import { WebSocket } from 'ws';
import { promisify } from 'util'
import 'dotenv/config'

const __dirname = dirname(fileURLToPath(import.meta.url));
const moduleRoot = join(__dirname, '..')

const {
GITHUB_WORKSPACE,
ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT,
ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT
} = process.env

const SECONDS = 1000
const DEFAULT_RETRY_COUNT = (
parseInt(ENTROPY_SDK_TESTS_RETRY_COUNT_DEFAULT) ||
GITHUB_WORKSPACE && 60 ||
20
)
const DEFAULT_RETRY_TIMEOUT = (
parseInt(ENTROPY_SDK_TESTS_RETRY_TIMEOUT_DEFAULT) ||
1 * SECONDS
)

// NOTE: you need to edit your /etc/hosts file to use these. See dev/README.md

Expand All @@ -31,26 +48,28 @@ export async function spinNetworkUp (networkType = 'four-nodes') {

async function retryUntil (fn, isSuccess = Boolean, opts = {}) {
const {
triesRemaining = process.env.GITHUB_WORKSPACE ? 60 : 20,
timeout = 1 * SECONDS
retryCount = DEFAULT_RETRY_COUNT,
retryTimeout = DEFAULT_RETRY_TIMEOUT
} = opts

return fn()
.then(result => {
if (isSuccess(result)) return result
else throw Error('retry failed')
})
.catch(async (err) => {
// out of tries, do not recurse
if (triesRemaining === 1) throw err
await promisify(setTimeout)(timeout)
if (retryCount === 1) throw err
await promisify(setTimeout)(retryTimeout)

return retryUntil(fn, isSuccess, {
triesRemaining: triesRemaining - 1,
timeout
retryCount: retryCount - 1,
retryTimeout
})
})
}


async function isWebSocketReady (endpoint) {
return new Promise((resolve, reject) => {
try {
Expand Down Expand Up @@ -136,8 +155,8 @@ total-block-time: ${headersSenseStart} blocks
}

export async function spinNetworkDown (networkType = 'four-nodes') {
if (process.env.ENTROPY_DONT_KILL) {
console.warn('$ENTROPY_DONT_KILL is set not spinning the network down')
if (process.env.ENTROPY_SDK_TESTS_DONT_KILL) {
console.warn('$ENTROPY_SDK_TESTS_DONT_KILL is set, so not spinning the network down')
return false
}
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"depcheck": "^1.4.7",
"dotenv": "^16.4.7",
"eslint": "^8.57.0",
"husky": "^9.0.11",
"lint-staged": ">=10",
Expand Down
8 changes: 2 additions & 6 deletions tests/four-nodes.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import test from 'tape'
import { readFileSync } from 'fs'

import Entropy, { wasmGlobalsReady } from '../src'
import Keyring from '../src/keys'
import * as util from '@polkadot/util'

import {
sleep,
promiseRunner,
spinNetworkUp,
jumpStartNetwork,
eveSeed,
eveAddress,
// eveAddress,
spinNetworkDown,
} from './testing-utils'

Expand All @@ -21,8 +19,6 @@ test('test the four-nodes docker script', async (t) => {
// context: all run does is checks that it runs
await run('network up', spinNetworkUp(networkType))

await sleep(process.env.GITHUB_WORKSPACE ? 30_000 * 2 : 5_000 * 2)

// this gets called after all tests are run
t.teardown(async () => {
await entropy.close()
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotenv@^16.4.7:
version "16.4.7"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.7.tgz#0e20c5b82950140aa99be360a8a5f52335f53c26"
integrity sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==

dotignore@^0.1.2:
version "0.1.2"
resolved "https://registry.npmjs.org/dotignore/-/dotignore-0.1.2.tgz"
Expand Down

0 comments on commit 88a58d2

Please sign in to comment.