pkp-lib
pkp-lib copied to clipboard
Add unit test for each queue jobs class
Describe the problem
Starting from 3.4.0 we have queue jobs to handle long running tasks or for tasks that need to done later . Our queue jobs implementation is an extended implementation of Laravel queue jobs.
The problem arise or will arise when any of the jobs structure will update but there is one or more pending jobs with older structure already dispatched in queue waiting to get executed . This problem get more complex with the upgrade of newer version when older structure based job still in queue as they may continue to fail and no way to resolve it after upgrade which may lead to undesirable outcome.
Describe the proposal The initial plan was to force to run all pending queue jobs before upgrade which proposed at Add pre-flight check for pending jobs before running upgrade . This was a very simple and easy approach but may not suited for all possible cases . Also a job class structure or handle implementation may need to update based on requirement.
After some detailed discussion, we decided to add unit tests for each job class to make sure the consistence update the job class structure and handle method implementation . The idea is to try to mock each of jobs class to make sure it's on even after any update to it.
Impacted version OJS, OMP or OPS version 3.4.0 and main
Additional information Details discussion at Right approach to handle pending queue jobs at upgrade
Job classes to have unit tests
Stable-3.4.0
OJS
- [x] APP\jobs\doi\DepositIssue
- [x] APP\jobs\notifications\IssuePublishedNotifyUsers
- [x] APP\jobs\notifications\OpenAccessMailUsers
- [x] APP\jobs\statistics\CompileCounterSubmissionDailyMetrics
- [x] APP\jobs\statistics\CompileCounterSubmissionInstitutionDailyMetrics
- [x] APP\jobs\statistics\CompileIssueMetrics
- [x] APP\jobs\statistics\CompileSubmissionGeoDailyMetrics
- [x] APP\jobs\statistics\CompileUniqueInvestigations
- [x] APP\jobs\statistics\CompileUniqueRequests
- [ ] APP\jobs\statistics\CompileUsageStatsFromTemporaryRecords [deprecated 3.4.0.5]
- [x] APP\jobs\statistics\DeleteUsageStatsTemporaryRecords
- [x] APP\jobs\statistics\ProcessUsageStatsLogFile
OMP
- [x] APP\jobs\statistics\CompileCounterSubmissionDailyMetrics
- [x] APP\jobs\statistics\CompileCounterSubmissionInstitutionDailyMetrics
- [x] APP\jobs\statistics\CompileSeriesMetrics
- [x] APP\jobs\statistics\CompileSubmissionGeoDailyMetrics
- [x] APP\jobs\statistics\CompileUniqueInvestigations
- [x] APP\jobs\statistics\CompileUniqueRequests
- [ ] APP\jobs\statistics\CompileUsageStatsFromTemporaryRecords [deprecated 3.4.0.5]
- [x] APP\jobs\statistics\DeleteUsageStatsTemporaryRecords
- [x] APP\jobs\statistics\ProcessUsageStatsLogFile
OPS
- [x] APP\jobs\statistics\CompileCounterSubmissionDailyMetrics
- [x] APP\jobs\statistics\CompileCounterSubmissionInstitutionDailyMetrics
- [x] APP\jobs\statistics\CompileSubmissionGeoDailyMetrics
- [x] APP\jobs\statistics\CompileUniqueInvestigations
- [x] APP\jobs\statistics\CompileUniqueRequests
- [ ] APP\jobs\statistics\CompileUsageStatsFromTemporaryRecords [deprecated 3.4.0.5]
- [x] APP\jobs\statistics\DeleteUsageStatsTemporaryRecords
- [x] APP\jobs\statistics\ProcessUsageStatsLogFile
PKP-LIB
- [x] PKP\jobs\bulk\BulkEmailSender
- [x] PKP\jobs\doi\DepositContext
- [x] PKP\jobs\doi\DepositSubmission
- [x] PKP\jobs\email\EditorialReminder
- [x] PKP\jobs\metadata\BatchMetadataChangedJob
- [x] PKP\jobs\metadata\MetadataChangedJob
- [x] PKP\jobs\notifications\NewAnnouncementNotifyUsers
- [x] PKP\jobs\notifications\StatisticsReportMail
- [x] PKP\jobs\notifications\StatisticsReportNotify
- [x] PKP\jobs\statistics\ArchiveUsageStatsLogFile
- [x] PKP\jobs\statistics\CompileContextMetrics
- [x] PKP\jobs\statistics\CompileMonthlyMetrics
- [x] PKP\jobs\statistics\CompileSubmissionMetrics
- [x] PKP\jobs\statistics\RemoveDoubleClicks
- [x] PKP\jobs\submissions\RemoveSubmissionFileFromSearchIndexJob
- [x] PKP\jobs\submissions\RemoveSubmissionFromSearchIndexJob
- [x] PKP\jobs\submissions\UpdateSubmissionSearchJob
PRs (Merged) pkp-lib --> https://github.com/pkp/pkp-lib/pull/9981 ojs --> https://github.com/pkp/ojs/pull/4285 omp --> https://github.com/pkp/omp/pull/1666 ops --> https://github.com/pkp/ops/pull/745
PR Patch pkp-lib --> https://github.com/pkp/pkp-lib/pull/10368 ojs --> https://github.com/pkp/ojs/pull/4426 ops --> https://github.com/pkp/ops/pull/763 omp --> https://github.com/pkp/omp/pull/1693
@asmecher can you take a look the PR at https://github.com/pkp/pkp-lib/issues/9899#issue-2252485770 for the OJS only ?
@withanage can you please take a look at this particular implementation to mock the Guzzle Http Client for unit tests .
implementation of mock at PKPTestCase and to facilitate it , I had to add few logic within the core app at PKPApplication and PKPContainer.
Long story short, we like to mock the guzzle requests without making any actual request but our very static like Application::get()->getHttpClient() implementation kind of make it very hard to mock it . Me and @asmecher was discussing about it and we would like to get your suggestion and advice on this as I am not very happy with this implementation but at the same time could not figure out any better approach .
We can also decide not to mock it for 3.4.0 and have something better like Laravel Http in 3.5. But that means either we have to ditch those specific queue job classes unit test or have unit test and allow hitting real url, both option does not seems very best.
Also any suggestion or advise on approach on the job test implementation are welcomed .
Hi @touhidurabir
PKPTestCase
I have looked at your test. I also think that the way you have implmented, it is the best possible way to mock the Guzzle HTTP client and I can't offer a better approach either.
General support
- I would generally not use real-urls for php-unit-tests.
- Although we can consider changing the Laravel Http to 3.5, my opinion would be to support the unit tests for 3.4. Especially, as the job queues are a very difficult and complex use-case, I think only automated and isolated unit tests would be long-term best approach.
@touhidurabir, for some reason your .php additions in https://github.com/pkp/ojs/pull/4285/files are being flagged as binary by Github; is there some unusual encoding or something causing this?
@touhidurabir, for some reason your .php additions in https://github.com/pkp/ojs/pull/4285/files are being flagged as binary by Github; is there some unusual encoding or something causing this?
^ I suspect it's the ^@*^@ part of the serialized object. I suppose we could prevent that from happening using base64 or chr or something, but I don't know if it's a problem worth solving; I'm OK to ignore it.
@asmecher I just noticed the issue in github as it is not causing any issue for me in my editor(VS code or sublime) . I think we can easily solve it using the base64 encode/decode. At least anyone one can see the test file but wont be able to understand the what props are defined there with a single look as base64 encoded string .
I think it's worth solving and will only take about an hour or two to update all the tests . for me it's not causing any issue in my editor but may cause issue with different editor for other .
@asmecher I have updated the tests to resolve the binary file issue . Good to take a look now .
Sorry for the wait, @touhidurabir! I added a comment about that: https://github.com/pkp/ojs/pull/4285#pullrequestreview-2187026948
@touhidurabir, this looks good, thanks -- just one file still being classified as binary. Once that's resolved, please go ahead and merge!
All PRs merged -- is anything else required on this issue, @touhidurabir?
@asmecher nothing more required for this . for 3.5 we can have different issue to add unit tests for queue jobs .
@asmecher I am re-opening this issue as our github action was not running any job unit tests and it seems some pkp-lib jobs which runs fine for OJS are failing for both the OPS and OMP .
I have registered an issue for github action at https://github.com/pkp/pkp-github-actions/issues/68 . PR done but need to wait to merge till unit tests failing issue get fixed first .
@asmecher All testes update . Please take a look at https://github.com/pkp/pkp-lib/issues/9899#issue-225248577, section PR Patch . The tests are ok in local testing for both pkp-lib and app specific for all apps .