Repository navigation
Eliminate/Report Hung Promises (#4687), Prepopulate Stage and Region (#4311), Handle Quoted Strings (#4734) - #4713
Conversation
Add a "PromiseTracker" to the Variables class. It provides tracking and reporting of pending variable resolutions during service populations. Decompose populateObject and populateProperty into smaller pieces that are easier to understand. Additionally, remove the immediate recursive resolution of variables, recursing only at the Object or Property entry point (when changes have been made). This avoids a problem where the resolved value, prior to being replaced in the containing object or value refers to itself and thereby is waiting on its own resolution before it will resolve itself. Return the given value in warnIfNotFound so that it can chain.
| describe('#constructor()', () => { | ||
| const serverless = new Serverless(); | ||
|
|
||
| describe.only('Variables', () => { |
There was a problem hiding this comment.
Embarrassing. I left the only here. I'll clear this up.
| expect(serverless.service.resources.bar).to.equal(barValue); | ||
| it('should remove problematic attributes bofore calling populateObject with the service', | ||
| () => { | ||
| const populateObjectStub = sinon.stub(serverless.variables, 'populateObject', (val) => { |
There was a problem hiding this comment.
Why do you delegate the unit test into the stub here?
It is cleaner, if you just stub and then check how the stub was called afterwards like this:
const populateObjectStub = sinon.stub(serverless.variables, 'populateObject').resolves();
return expect(serverless.variables.populateService()).to.be.fulfilled
.then(() => {
expect(populateObjectStub).to.have.been.calledWithExactly(serverless.service);
const callVar = _.first(populateObjectStub.firstCall.args);
expect(callVar.provider.variableSyntax).to.be.undefined;
...
});
Otherwise you melt stub execution and result checking into one blob.
There was a problem hiding this comment.
The code under test, for reference: https://github.com/erikerikson/serverless/blob/c18eb70a28d61317007091ae765585adc7cd37f6/lib/classes/Variables.js#L98
This particular test is validating temporary state that only exists from just prior to the invocation of "populateObject" and only persists until immediately after its resolution. After populateService is settled, the state cannot be validated.
| return expect(serverless.variables.populateProperty(property) | ||
| .then(newProperty => { | ||
| return serverless.variables.populateProperty(property) | ||
| .should.become(undefined) |
There was a problem hiding this comment.
we use chai-as-promised, so you can model this with expect instead of introducing the should semantics.
return expect(serverless.variables.populateProperty(property)).to.eventually.be.undefined
.then(() => ...
There was a problem hiding this comment.
chai-as-promised provides these should semantics to promises as well. this seemed much cleaner.
Is there a reason not to use should here?
Is there a standard to avoid it?
There was a problem hiding this comment.
@erikerikson I do not have a specific opinion here. I just was curious why you switched to should here :) - noone had that idea before
There was a problem hiding this comment.
👍
Thanks for explaining. I'm happy to go either way. I stumbled into should elsewhere and it has sometimes made tests more legible.
I've actually avoided it on the sister branch to this one where I'm trying to debug the build/tests (#4716). I'd be happy to import that work here but mildly prefer should. Just say the word and I'll do that but otherwise I'll assume it's fine.
add expected rejection message
Calling .then here creates a new promise. One that is not returned and generating a rejection within that promise can lead to unhandledRejection events being inappropriately raised. As such, don't declare unneeded variables.
fee8699 to
3606589
Compare
serverless#4311 see prepopulateService - attempts to pre-populate the region and stage settings necessary for making a request to AWS, rejecting and dependencies thereon it runs into during that process see the `deep` variable work. this was a knock-on effect of providing pre-population. it actually represents an obscure class of bugs where the recursive population previously in getDeepValue caused the caller not to be in charge of population choices (thus fixing for pre-population) but also caused potential deadlocks resulting from getDeepValue creating circular dependencies. serverless#4734 see splitByComma - a regex to do the splitting but ignore quoted commas was getting very deep and involved. Instead, identify quoted string boundaries, then identify commas (including white space around them) and take those commas not contained by quotes as the locations for splitting the given string. Trim the string as well (removing leading and trailing white space). add sophistication to the overwrite syntax, allowing for whitespace and repetitions (for robustness) add sophistication to the string ref syntax, allowing for use in identifying multiple quoted strings in a variable (i.e. for overwrites) #NotCreated fix a bug I created earlier in the branch that caused reporting to be less informative (see renderMatches) separate PromiseTracker move this class into its own file for the purpose of increasing testability and offering reuse minutiae filter the properties given to populateVariables so as to avoid attempting resolution on non-variables. Efficiency and noise reduction change. Also cleans up the code (e.g. see populateObject and its use) cleaning of overwrite as a side effect offer variable cleaning as a idiom reorder the Variables.js `require`s to be in alphabetical order
|
This should actually be ready for review now. It's not clear to me why this single test that succeeds locally is failing on 4.x: |
42c17f4 to
fa58c29
Compare
9a522df to
ffc7686
Compare
|
I was able to reproduce the build break by switching to 4.x. |
|
@erikerikson Great work. I'll put it into in-review now and have a look soon :) |
|
@HyperBrain @erikerikson I just rebased my development version of SLS to master and deployed my project and got an error: The original variable is |
|
@laardee You're right. The original variable should be correct and I think the issue is, that the file name itself must additionally go through a deep resolution in case it partially matches the variable regex. The new solution seems only to respect deep resolutions for the referenced properties. |
|
Thank you for reporting @lardee. Thank you @HyperBrain for following up and linking another issue. Looks like this needs to be reverted. I'll add test cases and fix this. Apologies that cases slipped through. |
#4713 Follow up fixes, take 2
What did you implement:
Closes #4311, #4734, and #4687
For #4311:
deepvariable type for internal use only. This allows variable resolution to be paused and resumed. More specifically it allows population to return to the level of the original request for validation (i.e. during pre-population check whether AWS dependencies have been found during recursive variable resolution). This actually has an additional benefit of solving for an obscure class of bugs where the recursive population previously in getDeepValue caused potential deadlocks resulting from circular dependencies.For #4687:
splitByCommacapability (rather than usingsplit(',')). Note that creating a regex to do the splitting while ignoring quoted commas was getting very deep and involved so I chose to use simpler statements and be procedural. Trims white space as well.For #4687:
How did you implement it:
How can we verify it:
Tests have been added to cover the new error cases. A good deal of cleanup has been involved as previously many of the tests were checking that the code was as written rather than identifying function behaviors. Not perfectly but have transitioned them to something more sustainable.
Todos:
Is this ready for review?: YES
Is it a breaking change?: NO?