Skip to content

Best way to wait for adapter stop? #507

@klein0r

Description

@klein0r

I have a schedule adapter and I want to perform some checks when everything is finished (based on adapter configuration). What is the best way to wait for the adapter to stop?

Something like this?

'use strict';

const path = require('path');
const { tests, IntegrationTestHarness } = require('@iobroker/testing');
const chai = require('chai');
const expect = chai.expect;

// Run integration tests - See https://github.com/ioBroker/testing for a detailed explanation and further options
tests.integration(path.join(__dirname, '..'), {
    defineAdditionalTests({ suite }) {

        suite('Test', getHarness => {
            /**
             * @type {IntegrationTestHarness}
             */
            let harness;
            before(() => {
                harness = getHarness();
            });

            it('Should work', function (done) {
                this.timeout(60000);

                harness.startAdapterAndWait()
                    .then(() => {
                        expect(harness.isAdapterRunning()).to.equal(true);
                        expect(harness.isControllerRunning()).to.equal(true);

                        // Wait for adapter stop
                        return new Promise(resolve => {
                            harness.on('stateChange', async (id, state) => {
                                if (
                                    id === `system.adapter.${harness.adapterName}.0.alive` &&
                                    state &&
                                    state.val === false
                                ) {
                                    resolve(true);
                                }
                            });
                        });
                    })
                    .then(() => {
                        console.log('-----------------> STOPPED NOW');

                        done();
                    });
            });
        });

    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions