-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fix broken CheckLiveBillRunService #841
Merged
Merged
Conversation
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
https://eaflood.atlassian.net/browse/WATER-4379 We recently updated the `CheckLiveBillRunService` to support this project handling annual billing (see [Handle live bill run check for annual bill runs](#761)). We thought all was well until we spotted some strange behaviour when testing. This app wouldn't create new SROC supplementary bill runs because `CheckLiveBillRunService` was claiming one was in progress. But a check of the DB found that wasn't the case. We added some extra debug logging, restarted the app and tried again ... and it worked fine! Ok, chalk it up to 'puters and their weird ways. Till it happened again. We looked again at the source and immediately spotted a flaw. But we thought it was unrelated. PR #761 updated the code in the service to this ```javascript const statuses = LIVE_STATUSES // Only one annual bill run per region and financial year is allowed. So, we include sent and sending in the statues // to check for if (batchType === 'annual') { statuses.push('sent', 'sending') } const numberOfLiveBillRuns = await BillRunModel.query() .select(1) .where({ regionId, toFinancialYearEnding, batchType, scheme: 'sroc' }) .whereIn('status', LIVE_STATUSES) .resultSize() return numberOfLiveBillRuns !== 0 ``` The flaw we spotted was `.whereIn('status', LIVE_STATUSES)`. Even though we'd created a new `statuses` object that we were updating if the batch type is 'annual', we weren't using it in our query. That line should have been `.whereIn('status', statuses)`. But hang on, our unit tests were saying everything is okay and working for both batch types. Huh!? 🫤 And then we had our 'a-ha' moment. 😁 This line `const statuses = LIVE_STATUSES` is not _copying_ `LIVE_STATUSES` to `statuses`. It is _setting_ `statuses` and `LIVE_STATUSES` to be the same thing. It is essentially the same thing as [Passing by reference vs passing by value](https://stackoverflow.com/a/430958/6117745). > Say I want to share a web page with you. If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself. > > If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have destroyed your copy of the object - but the original web page remains intact. Our unit tests worked because the supplementary test runs _before_ the annual. When you run the annual it is causing the `LIVE_STATUSES` to get updated hence the query works and the test passes. Now, you can apply this same behaviour to an environment. When the app first starts `LIVE_STATUSES` won't include `'sending'` and `'sent'`. So, supplementary bill runs will go through. Then someone creates an annual bill run. Now `LIVE_STATUSES` does include those values and because it is declared outside of the scope of the function, it will stay that way for subsequent calls. This means the next supplementary bill run will fail because the query will return a result. Don't you love programming! 🤦😧😬 This change fixes the issue.
Doing this our test will now fail. Whoopee!
Cruikshanks
requested review from
Demwunz,
robertparkinson,
Jozzey,
Beckyrose200 and
rvsiyad
March 20, 2024 12:42
Jozzey
approved these changes
Mar 20, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cruikshanks
added a commit
that referenced
this pull request
Mar 20, 2024
https://eaflood.atlassian.net/browse/WATER-4416 https://eaflood.atlassian.net/browse/WATER-4379 We spotted an issue with removing a bill from a bill run not setting the supplementary flags on a the licences involved correctly. We've fixed that, but its led to our QA team rigorously re-creating bill runs over and over. Doing this they spotted we did have an issue with the `CheckLiveBillRunService` that we fixed in [Fix broken CheckLiveBillRunService](#841). Now they've spotted something else. `CheckLiveBillRunService` includes batch type as a filter. This means it _will_ allow you to create a supplementary bill run, for example, even if another type of bill run is 'in progress' (queued, processing, or ready). AT the time we thought that was ok but now we know better. We had actually spotted this in our work to migrate the setup bill run journey (see [Handle bill run setup matches an existing bill run](#810)). We hoped we'd be using our version of the journey by now and we could quietly retire `CheckLiveBillRunService` as part of ongoing maintenance and no one would be any the wiser (it has been live for 7 months now!) But our QA team would rather clear the issues currently found before bringing in the new setup journey for testing. So, rather than fix the service, we'll replace it with `DetermineBlockingBillRunService` which matches what the legacy service does and deals with this scenario.
Cruikshanks
added a commit
that referenced
this pull request
Mar 21, 2024
https://eaflood.atlassian.net/browse/WATER-4416 https://eaflood.atlassian.net/browse/WATER-4379 We spotted an issue with removing a bill from a bill run and it not setting the supplementary flags on the licences involved correctly. We've fixed that, but it's led to our QA team rigorously re-creating bill runs over and over. Doing this they spotted we did have an issue with the `CheckLiveBillRunService` that we fixed in [Fix broken CheckLiveBillRunService](#841). Now they've spotted something else. `CheckLiveBillRunService` includes batch type as a filter. This means it _will_ allow you to create a supplementary bill run, for example, even if another type of bill run is 'in progress' (queued, processing, or ready). At the time we thought that was ok but now we know better. We spotted this in our work to migrate the setup bill run journey (see [Handle bill run setup matches an existing bill run](#810)). We hoped we'd be using our version of the journey by now and we could quietly retire `CheckLiveBillRunService` as part of ongoing maintenance and no one would be any the wiser (it has been live for 7 months now!) However, our QA team would rather clear the issues currently found before bringing in the new setup journey for testing. So, rather than fix the service, we'll replace it with `DetermineBlockingBillRunService` which matches what the legacy service does and deals with this scenario.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
https://eaflood.atlassian.net/browse/WATER-4379
We recently updated the
CheckLiveBillRunService
to support this project handling annual billing (see Handle live bill run check for annual bill runs).We thought all was well until we spotted some strange behaviour when testing. This app wouldn't create new SROC supplementary bill runs because
CheckLiveBillRunService
was claiming one was in progress. But a check of the DB found that wasn't the case.We added some extra debug logging, restarted the app and tried again ... and it worked fine! Ok, chalk it up to 'puters and their weird ways.
Till it happened again. We looked again at the source and immediately spotted a flaw. But we thought it was unrelated.
PR #761 updated the code in the service to this
The flaw we spotted was
.whereIn('status', LIVE_STATUSES)
. Even though we'd created a newstatuses
object that we were updating if the batch type is 'annual', we weren't using it in our query. That line should have been.whereIn('status', statuses)
.But hang on, our unit tests were saying everything is okay and working for both batch types. Huh!? 🫤
And then we had our 'a-ha' moment. 😁
This line
const statuses = LIVE_STATUSES
is not copyingLIVE_STATUSES
tostatuses
. It is settingstatuses
andLIVE_STATUSES
to be the same thing. It is essentially the same thing as Passing by reference vs passing by value.Our unit tests worked because the supplementary test runs before the annual. When you run the annual it causes the
LIVE_STATUSES
to get updated hence the query works and the test passes.Now, you can apply this same behaviour to an environment. When the app first starts
LIVE_STATUSES
won't include'sending'
and'sent'
. So, supplementary bill runs will go through. Then someone creates an annual bill run. NowLIVE_STATUSES
does include those values and because it is declared outside of the scope of the function, it will stay that way for subsequent calls. This means the next supplementary bill run will fail because the query will return a result.Don't you love programming?! 🤦😧😬
This change fixes the issue.