-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added PersistentMNRegTestContainer (#173)
- Loading branch information
Showing
5 changed files
with
131 additions
and
18 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/testcontainers/__tests__/chains/reg_test_container/persistent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { PersistentMNRegTestContainer } from '../../../src' | ||
import waitForExpect from 'wait-for-expect' | ||
|
||
beforeEach(async () => { | ||
try { | ||
await new PersistentMNRegTestContainer().stop() | ||
} catch (ignored) { | ||
} | ||
}) | ||
|
||
afterEach(async () => { | ||
try { | ||
await new PersistentMNRegTestContainer().stop() | ||
} catch (ignored) { | ||
} | ||
}) | ||
|
||
it('should start and mint coins', async () => { | ||
const container = new PersistentMNRegTestContainer() | ||
await container.start() | ||
await container.waitForReady() | ||
|
||
await waitForExpect(async () => { | ||
const info = await container.getMintingInfo() | ||
expect(info.blocks).toBeGreaterThan(3) | ||
}) | ||
}) | ||
|
||
it('should always use the same persistent container', async () => { | ||
let container = new PersistentMNRegTestContainer() | ||
await container.start() | ||
await container.waitForReady() | ||
|
||
await waitForExpect(async () => { | ||
const info = await container.getMintingInfo() | ||
expect(info.blocks).toBeGreaterThan(3) | ||
}) | ||
|
||
container = new PersistentMNRegTestContainer() | ||
await container.start() | ||
await container.waitForReady() | ||
|
||
const info = await container.getMintingInfo() | ||
expect(info.blocks).toBeGreaterThan(3) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/testcontainers/src/chains/reg_test_container/persistent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { MasterNodeRegTestContainer } from './masternode' | ||
import { DeFiDContainer, StartOptions } from '../container' | ||
import Dockerode, { ContainerInfo } from 'dockerode' | ||
|
||
async function getContainerInfoByName (docker: Dockerode, name: string): Promise<ContainerInfo | undefined> { | ||
return await new Promise((resolve, reject) => { | ||
const opts = { limit: 1, filters: `{"name": ["${name}"]}` } | ||
docker.listContainers(opts, function (err, containers) { | ||
if (err === null && containers !== undefined) { | ||
resolve(containers[0]) | ||
} else { | ||
reject(err) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* PersistentMNRegTestContainer container is a RegTest container with MasterNode minting preconfigured. | ||
* The container configuration is persistent and can be used consistently. | ||
* If you do not stop the container, the same container can be used for all tests. | ||
* However, you must be cognizant of race conditions. | ||
* | ||
* This container should not be used for finished work, it merely a dev tool to speed up test-driven development. | ||
* Once you are done with your dev work, you should swap this out for MasterNodeRegTestContainer. | ||
*/ | ||
export class PersistentMNRegTestContainer extends MasterNodeRegTestContainer { | ||
/** | ||
* Init the required container instances for start/stop operation | ||
*/ | ||
async init (): Promise<void> { | ||
const info = await getContainerInfoByName(this.docker, this.generateName()) | ||
this.container = info?.Id !== undefined ? this.docker.getContainer(info.Id) : undefined | ||
} | ||
|
||
/** | ||
* This will only start a persistent container if it's not yet already started. | ||
* @see {generateName()} for the name of container | ||
*/ | ||
async start (startOptions: StartOptions = {}): Promise<void> { | ||
this.startOptions = Object.assign(DeFiDContainer.DefaultStartOptions, startOptions) | ||
|
||
try { | ||
await this.init() | ||
this.requireContainer() | ||
await this.waitForReady(3000) | ||
} catch (e) { | ||
// Attempt clean up before starting | ||
await super.stop() | ||
await super.start(startOptions) | ||
} | ||
} | ||
|
||
/** | ||
* @return {string} name of persistent container that is always consistent. | ||
*/ | ||
generateName (): string { | ||
return `${DeFiDContainer.PREFIX}-${this.network}-persistent` | ||
} | ||
|
||
async stop (): Promise<void> { | ||
await this.init() | ||
await super.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters