-
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.
@defichain/testcontainers
MasternodeGroup (#470)
* multi nodes * refactor and add ability to run multiple container in a group * added randomly generated network name * fix restart to reset cachedRpcUrl * fixed updated tests Co-authored-by: Fuxing Loh <[email protected]>
- Loading branch information
1 parent
4a26526
commit 7b48f0f
Showing
10 changed files
with
227 additions
and
63 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
packages/testcontainers/__tests__/chains/container_restart.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
38 changes: 38 additions & 0 deletions
38
packages/testcontainers/__tests__/chains/reg_test_container/masternode_group.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,38 @@ | ||
import { GenesisKeys, MasternodeGroup, MasterNodeRegTestContainer } from '../../../src' | ||
|
||
const group = new MasternodeGroup([ | ||
new MasterNodeRegTestContainer(GenesisKeys[0]), | ||
new MasterNodeRegTestContainer(GenesisKeys[1]) | ||
]) | ||
|
||
beforeAll(async () => { | ||
await group.start() | ||
await group.get(0).waitForWalletCoinbaseMaturity() | ||
}) | ||
|
||
afterAll(async () => { | ||
await group.stop() | ||
}) | ||
|
||
it('send balance and wait for sync for balance to reach', async () => { | ||
const before = await group.get(1).call('getbalance') | ||
|
||
const address = await group.get(1).getNewAddress() | ||
await group.get(0).call('sendtoaddress', [address, 314]) | ||
await group.get(0).generate(1) | ||
await group.waitForSync() | ||
|
||
const after = await group.get(1).call('getbalance') | ||
expect(after - before).toStrictEqual(314) | ||
}) | ||
|
||
it('should add another container to already running group', async () => { | ||
const container = new MasterNodeRegTestContainer() | ||
await container.start() | ||
|
||
await group.add(container) | ||
await group.waitForSync() | ||
|
||
const count = await group.get(2).getBlockCount() | ||
expect(count).toBeGreaterThan(100) | ||
}) |
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
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
91 changes: 91 additions & 0 deletions
91
packages/testcontainers/src/chains/reg_test_container/masternode_group.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,91 @@ | ||
import Dockerode, { DockerOptions, Network } from 'dockerode' | ||
import { waitForCondition } from '../../wait_for_condition' | ||
import { MasterNodeRegTestContainer } from '../../chains/reg_test_container/masternode' | ||
|
||
export class MasternodeGroup { | ||
protected readonly docker: Dockerode | ||
protected network?: Network | ||
|
||
public constructor ( | ||
protected readonly containers: MasterNodeRegTestContainer[] = [], | ||
protected readonly name = `testcontainers-${Math.floor(Math.random() * 10000000)}`, | ||
options?: DockerOptions | ||
) { | ||
this.docker = new Dockerode(options) | ||
} | ||
|
||
get (i: number): MasterNodeRegTestContainer { | ||
return this.containers[i] | ||
} | ||
|
||
async start (): Promise<void> { | ||
this.network = await new Promise((resolve, reject) => { | ||
return this.docker.createNetwork({ | ||
Name: this.name, | ||
IPAM: { | ||
Driver: 'default', | ||
Config: [] | ||
} | ||
}, (err, data) => { | ||
if (err instanceof Error) { | ||
return reject(err) | ||
} | ||
return resolve(data) | ||
}) | ||
}) | ||
|
||
// Removing all predefined containers and adding it to group | ||
for (const container of this.containers.splice(0)) { | ||
await container.start() | ||
await this.add(container) | ||
} | ||
} | ||
|
||
/** | ||
* Require network, else error exceptionally. | ||
* Not a clean design, but it keep the complexity of this implementation low. | ||
*/ | ||
protected requireNetwork (): Network { | ||
if (this.network !== undefined) { | ||
return this.network | ||
} | ||
throw new Error('network not yet started') | ||
} | ||
|
||
/** | ||
* @param {DeFiDContainer} container to add into container group with addnode | ||
*/ | ||
async add (container: MasterNodeRegTestContainer): Promise<void> { | ||
await this.requireNetwork().connect({ Container: container.id }) | ||
|
||
for (const each of this.containers) { | ||
await container.addNode(await each.getIp(this.name)) | ||
} | ||
|
||
this.containers.push(container) | ||
} | ||
|
||
/** | ||
* Wait for all container to sync up | ||
* @param {number} [timeout=150000] in millis | ||
*/ | ||
async waitForSync (timeout: number = 15000): Promise<void> { | ||
await waitForCondition(async () => { | ||
const hashes = await Promise.all(Object.values(this.containers).map(async container => { | ||
return await container.getBestBlockHash() | ||
})) | ||
|
||
return hashes.every(value => value === hashes[0]) | ||
}, timeout) | ||
} | ||
|
||
/** | ||
* Stop container group and all containers associated with it | ||
*/ | ||
async stop (): Promise<void> { | ||
for (const container of this.containers) { | ||
await container.stop() | ||
} | ||
await this.requireNetwork().remove() | ||
} | ||
} |
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
Oops, something went wrong.