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

[🐛 Bug]: @wdio/shared-store-service getValue returns undefined in onComplete hook #8335

Closed
3 tasks done
mighty98 opened this issue May 14, 2022 · 13 comments
Closed
3 tasks done
Labels
Bug 🐛 good first pick a reasonable task to start getting familiar with the code base help wanted Issues that are free to take by anyone interested

Comments

@mighty98
Copy link
Contributor

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

7.19.7

Node.js Version

14.16.1

Mode

WDIO Testrunner

Which capabilities are you using?

No response

What happened?

Im creating a custom service where in onComplete hook i need to getValue from shared-store-service
In onPrepare hook i initialise the value
onPrepare = async () => { await setValue('result', { total: 0, pass: 0, fail: 0, duration: 0 }); };

in afterTest i update it
afterTest = async (test: Test, context: any, result: TestResult) => { const current: any = await getValue('result'); current.total += 1; result.passed ? (current.pass += 1) : (current.fail += 1); current.duration += result.duration; await setValue('result', current); };

And finally in onComplete i read it
onComplete = async () => { const current: any = await getValue('result'); console.log(ON COMPLETE ${JSON.stringify(current)}); };

But it returns undefined

image

What is your expected behavior?

I should be able to fetch value in onComplete

How to reproduce the bug.

Steps are mentioned in description (Hope that would suffice)

Relevant log output

2022-05-14T16:44:56.543Z INFO @wdio/cli:launcher: Run onWorkerEnd hook
2022-05-14T16:44:56.544Z INFO @wdio/cli:launcher: Run onComplete hook
2022-05-14T16:44:56.551Z WARN @wdio/shared-store-service: undefined undefined undefined undefined
ON COMPLETE undefined

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues
@mighty98 mighty98 added Bug 🐛 Needs Triaging ⏳ No one has looked into the issue yet labels May 14, 2022
@christian-bromann
Copy link
Member

Can you please provide a reproducible example?

@christian-bromann christian-bromann added Reproducible Example Missing and removed Needs Triaging ⏳ No one has looked into the issue yet labels May 14, 2022
@mighty98
Copy link
Contributor Author

@christian-bromann Sorry.. Do you mean a full running code to reproduce this? The given screenshots would not help?
(Raising issue for the first time here. Not really sure what was expected)

@christian-bromann
Copy link
Member

Do you mean a full running code to reproduce this? The given screenshots would not help?
(Raising issue for the first time here. Not really sure what was expected)

Yes, because putting what you've shared into a fresh project, returns:

ON COMPLETE {"total":1,"pass":1,"fail":0,"duration":2052}

@mighty98
Copy link
Contributor Author

Oh that's strange.. I will work on getting the whole reproducible code. Meanwhile few pointers

  1. I have multiple custom services that use multiple hooks
  2. I don't use selenium-standalone-service but have created a custom service for starting server

@mighty98
Copy link
Contributor Author

@christian-bromann I have added the code here: https://github.com/mighty98/wdio-hooks

npm run test -- --suite google

I could reproduce the issue

@christian-bromann
Copy link
Member

I see now. The problem is that all service hooks are run at the same time. So the server of the shared service is closed at the same time you are trying to receive a value from it. There are two options:

  1. delay this shutdown by an arbitrary number e.g. 1 second to allow access to values before it gets shut down - the problem with that is if you do any other actions that take longer than 1 second before you try to access the values, the server might already be shut down
  2. don't shut down the server at as it will be closed automatically if the parent process (the launcher process) closes as well

I think solution number 2 seems more resilient. Any contributions that resolves the bug are highly appreciated. Please take a look into our contribution guidelines and let us know if you have any questions. Cheers!

@christian-bromann christian-bromann added help wanted Issues that are free to take by anyone interested good first pick a reasonable task to start getting familiar with the code base and removed Reproducible Example Missing labels May 16, 2022
@mighty98
Copy link
Contributor Author

mighty98 commented May 16, 2022

@christian-bromann Thanks for the quick reply....
About not shutting down the server.. Is it something i can do in my code? Isnt it handled at framework level.. ??
Also, do we have any other hook that runs before onComplete and after all workers are closed? If we use onComplete for these cleanup task we may run into similar such issues right?

@christian-bromann
Copy link
Member

Isnt it handled at framework level.. ??

It is handled in the framework level, which is why I mention that contributions are welcome.

Also, do we have any other hook that runs before onComplete and after all workers are closed?

Yes, after for example but that hook is run for every runner.

If we use onComplete for these cleanup task we may run into similar such issues right?

Not sure what you mean.

@mighty98
Copy link
Contributor Author

mighty98 commented May 16, 2022

@christian-bromann Thanks.
As for my last question - If onComplete is the only hook at main process level, we may run into such issues where dependent services may not be available if there is no specific order to execute (As long as we create custom services user can control the execution order but if there are multiple services owned not by the user and they have their own onComplete logic, i wonder if we will run into such cases).. I may be wrong and this could be just one of a case

As for the required change, I may be very inexperienced to make the required change. But just removing onComplete hook from launcher here would resolve this?

export default class SharedStoreLauncher {
    async onPrepare (_: never, capabilities: SharedStoreServiceCapabilities[]) {
        server = require('./server').default
        const result = await server.startServer()
        setPort(result.port)
        capabilities.forEach(capability => {capability['wdio:sharedStoreServicePort'] = result.port})

        log.info(`Started shared server on port ${result.port}`)
    }

    async onComplete () {
        await server.stopServer()
    }
}

@christian-bromann
Copy link
Member

But just removing onComplete hook from launcher here would resolve this?

I believe so. It might also require the removal of a test.

@mighty98
Copy link
Contributor Author

@christian-bromann Can you please take a look at #8349

christian-bromann pushed a commit that referenced this issue May 18, 2022
* fix for #8335

* Incorporating review comment
@mighty98
Copy link
Contributor Author

@christian-bromann Was i supposed to add a new version in package.json to be able to use this?

@christian-bromann
Copy link
Member

If your versions are prefixed with a ^ you don't. Please note that this change is not released yet. Keep an eye on the release section as it will be included in the next release (which I don't know when it happens but surely soon!).

Closing as this bug was fixed and will be released soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 good first pick a reasonable task to start getting familiar with the code base help wanted Issues that are free to take by anyone interested
Projects
None yet
Development

No branches or pull requests

2 participants