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

feat(watcher): Debounce autoWatchBatchDelay #2562

Conversation

danielcompton
Copy link
Contributor

I'm opening this up for feedback early, to make sure I'm on the right track here. My intention is to change the behaviour of autoWatchBatchDelay to debounce file change events, and not run the tests until at least autoWatchBatchDelay ms have elapsed since the last file change.

Closes #2331

@danielcompton danielcompton force-pushed the feat-debounce-autowatchbatchdelay branch from 4e4cdb0 to 0b99191 Compare February 9, 2017 09:01
@dignifiedquire
Copy link
Member

Thanks @danielcompton, I agree this change makes sense. There are probably some tests that need updating after this change.

@danielcompton
Copy link
Contributor Author

Yeah there's some tests that fail, and I still need to verify the behaviour is correct.

@danielcompton
Copy link
Contributor Author

(Probably also adding more tests)

@wesleycho
Copy link
Member

I think all that is needed here is to just adjust the delay in the tests to bounce it back a little further.

@danielcompton
Copy link
Contributor Author

danielcompton commented May 7, 2017

Thanks for the tip, you're right that did fix those tests.

I modified the 'batch interval' test block to test the debouncing, and it seems like that isn't working correctly. I'm not sure if this is because the tests need to mock more of lodash, or if there is something else going on? I'm at about the limits of my JS knowledge, if you can take a quick look to see if there's something obvious I'm not doing, I'd appreciate it :)

lib/file-list.js Outdated
// to avoid spamming the listener.
function emit () {
self._emitter.emit('file_list_modified', self.files)
}
var throttledEmit = _.throttle(emit, self._batchInterval, {leading: false})
var debouncedEmit = _.debounce(emit, self._batchInterval, {leading: false})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think {leading: false} needs to be passed into the function here - debouncing never fires the leading call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

@wesleycho
Copy link
Member

Not sure if this helps you, but in another PR for upgrading dependencies (including lodash), I had to make this adjustment: https://github.com/wesleycho/karma/blob/c80a3161dd4c345be2ec00c6691811b5dc9b2a5d/test/unit/file-list.spec.js#L770-L805

@danielcompton danielcompton force-pushed the feat-debounce-autowatchbatchdelay branch 2 times, most recently from 64ee461 to 7d906f7 Compare May 8, 2017 03:24
@danielcompton
Copy link
Contributor Author

danielcompton commented May 8, 2017

Phew! I think I've got it all.

In the old version, the tests were passing, but they weren't testing what they seemed to be testing.

list.removeFile, list.changeFile, and list.addFile all return promises and call self._emitModified when a file modification is accepted as valid. There is an important difference in behaviour between list.removeFile and the other two though. Due to the promise structure of list.removeFile, self._emitModified is called synchronously. The test code was calling all of these functions one after another without waiting on the result. The only function call that had any effect on the debouncing function were the list.removeFile calls. The other two still called self._emitModified, but only after exiting the test scope. You can verify this by commenting out the two list.removeFile calls, and watch the test fail.

        list.changeFile('/some/b.js')
        // list.removeFile('/some/a.js') // /some/b.js, /a.txt
        // list.removeFile('/a.txt')     // /some/b.js
        list.addFile('/a.txt')        // /some/b.js, /a.txt
        list.addFile('/some/0.js')    // /some/0.js, /some/b.js, /a.txt

        clock.tick(99)
        expect(modified).to.not.have.been.called

        clock.tick(2)
        expect(modified).to.have.been.calledOnce // This will fail

In my testing for this feature, I extended the test with more calls to modify and change files to extend the debouncing period. However because the promises weren't being waited on, they didn't run until after the test block had finished. This lead to very confusing behaviour where removing a file made the debouncing test work, but changing or adding a file doesn't.

I've converted the tests to wait on the promises and added more tests, and they all pass.

@danielcompton danielcompton force-pushed the feat-debounce-autowatchbatchdelay branch 3 times, most recently from 00d9274 to 1aff005 Compare May 8, 2017 05:12
- renamed batchInterval to autoWatchBatchDelay to aid in greppability.
- Modified debouncing tests to wait on promises.
- Added documentation explaining how list.removeFile is different from
list.addFile and list.changeFile.

Closes karma-runner#2331
@danielcompton danielcompton force-pushed the feat-debounce-autowatchbatchdelay branch from 1aff005 to 2f8c049 Compare May 8, 2017 05:15
@danielcompton
Copy link
Contributor Author

This is ready for review now.

@danielcompton
Copy link
Contributor Author

@wesleycho would you able to take another review please?

@dignifiedquire
Copy link
Member

Thanks a lot for getting through this and especially updating the tests to correctly test what they should test!

@dignifiedquire dignifiedquire merged commit 87566d1 into karma-runner:master May 28, 2017
@danielcompton danielcompton deleted the feat-debounce-autowatchbatchdelay branch February 19, 2018 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants