Allow tests to access local version of web worker.#9475
Allow tests to access local version of web worker.#9475dreamofabear merged 8 commits intoampproject:masterfrom
Conversation
src/web-worker/amp-worker.js
Outdated
| const useRtvVersion = !getMode().localDev && !getMode().test; | ||
| const url = calculateEntryPointScriptUrl( | ||
| loc, 'ww', getMode().localDev, useRtvVersion); | ||
| loc, 'ww', !useRtvVersion, useRtvVersion, getMode().minified); |
There was a problem hiding this comment.
Not sure about this change here. Without it, we're not pulling the web worker from local code. @choumx WDYT?
There was a problem hiding this comment.
That's fine. Could use a better var name though.
const useLocalFile = getMode().localDev || getMode().test;
const useRtvVersion = !useLocalFile;
src/service/extension-location.js
Outdated
| */ | ||
| export function calculateEntryPointScriptUrl( | ||
| location, entryPoint, isLocalDev, opt_rtv) { | ||
| location, entryPoint, isLocalDev, opt_rtv, opt_minifed) { |
There was a problem hiding this comment.
Do you think opt_minified should be changed to opt_notMinified so that the non-max url is returned by defaukt?
There was a problem hiding this comment.
Nah, inverted bools are harder to read.
src/service/extension-location.js
Outdated
| */ | ||
| export function calculateEntryPointScriptUrl( | ||
| location, entryPoint, isLocalDev, opt_rtv) { | ||
| location, entryPoint, isLocalDev, opt_rtv, opt_minifed) { |
There was a problem hiding this comment.
Nah, inverted bools are harder to read.
src/service/extension-location.js
Outdated
| return `${base}/rtv/${getMode().rtvVersion}/${entryPoint}.js`; | ||
| } | ||
| return `${base}/${entryPoint}.js`; | ||
| const version = opt_minifed ? '' : '.max'; |
src/service/extension-location.js
Outdated
| return `${base}/${entryPoint}.js`; | ||
| const version = opt_minifed ? '' : '.max'; | ||
| const rtv = opt_rtv ? `/rtv/${getMode().rtvVersion}` : ''; | ||
| return `${base}${rtv}/${entryPoint}${version}.js`; |
There was a problem hiding this comment.
I think having the conditional is more readable here.
src/web-worker/amp-worker.js
Outdated
| const useRtvVersion = !getMode().localDev && !getMode().test; | ||
| const url = calculateEntryPointScriptUrl( | ||
| loc, 'ww', getMode().localDev, useRtvVersion); | ||
| loc, 'ww', !useRtvVersion, useRtvVersion, getMode().minified); |
There was a problem hiding this comment.
That's fine. Could use a better var name though.
const useLocalFile = getMode().localDev || getMode().test;
const useRtvVersion = !useLocalFile;
src/service/extension-location.js
Outdated
| * @param {string} entryPoint | ||
| * @param {boolean=} isLocalDev | ||
| * @param {boolean=} opt_rtv | ||
| * @param {boolean=} opt_minifed |
src/service/extension-location.js
Outdated
| */ | ||
| export function calculateEntryPointScriptUrl( | ||
| location, entryPoint, isLocalDev, opt_rtv) { | ||
| location, entryPoint, isLocalDev, opt_rtv, opt_minifed) { |
| expect(script).to.equal('http://localhost:8000/dist/sw.js'); | ||
| }, | ||
| 'sw', | ||
| /* isLocalDev */ true); |
There was a problem hiding this comment.
I think so too, but this is what the linter wants. I think it's technically correct since the first argument is on the same line as the call, and the rest of the args are indented by four.
There was a problem hiding this comment.
If these args can't fit on the same line, can you extract the object literal into a separate var instead? The linter ought to be a sanity check rather than the final authority on readability. 😃
src/service/extension-location.js
Outdated
| location, entryPoint, isLocalDev, opt_rtv, opt_minified) { | ||
| const base = calculateScriptBaseUrl(location, isLocalDev); | ||
| if (opt_rtv) { | ||
| if (opt_rtv && opt_minified) { |
There was a problem hiding this comment.
const rtv = opt_rtv ? `/rtv/${getMode().rtvVersion}` : '';
const max = opt_minified ? '' : '.max';
return `${base}${rtv}/${entryPoint}${max}.js`There was a problem hiding this comment.
This is what I had before. @choumx @jridgewell which way do we want to go with?
There was a problem hiding this comment.
Ours are almost 100% the same. I think that means it's the better option. 😉
There was a problem hiding this comment.
Not a big deal, but I think having possibly empty strings in the format string makes it harder to verify that slashes, etc. are correct. Another option is to incrementally build the url string.
src/service/extension-location.js
Outdated
| */ | ||
| export function calculateEntryPointScriptUrl( | ||
| location, entryPoint, isLocalDev, opt_rtv) { | ||
| location, entryPoint, isLocalDev, opt_rtv, opt_minified) { |
There was a problem hiding this comment.
@kmh287 Trying to understand why we need it back? I think we are trying to avoid this minified logic in PROD as much as we can.
There was a problem hiding this comment.
To be able to run gulp test without --compiled. Currently we always serve the minified version which requires gulp dist to be run first.
We can remove this once unit tests no longer require gulp build.
There was a problem hiding this comment.
Can't you set the test server to serve max files?
There was a problem hiding this comment.
I thought we always serve unminified version when running gulp test without --compiled
https://github.com/ampproject/amphtml/blob/master/build-system/tasks/runtime-test.js#L181
There was a problem hiding this comment.
Can't you set the test server to serve max files?
Hmm, we could do that. We'd need to dupe the logic in app.js into test-server.js.
I thought we always serve unminified version when running gulp test without --compiled
Only for the app.js server which currently doesn't run during testing.
Another issue is that location.host actually returns the Karma server on localhost, so that needs to change to the test server's port.
There was a problem hiding this comment.
I thought https://github.com/ampproject/amphtml/blob/master/build-system/tasks/runtime-test.js#L196 did the work for us
There was a problem hiding this comment.
Looks like that var refers to test-server.js. Perhaps we should consolidate though.
src/service/extension-location.js
Outdated
| if (opt_rtv && opt_minified) { | ||
| return `${base}/rtv/${getMode().rtvVersion}/${entryPoint}.js`; | ||
| } else if (opt_rtv) { | ||
| return `${base}/rtv/${getMode().rtvVersion}/${entryPoint}.max.js`; |
There was a problem hiding this comment.
can someone walk me through on how the opt-rtv work?
There was a problem hiding this comment.
It fetches the current RTV file instead of the un-versioned file, e.g. cdn.ampproject.org/rtv/123/v0.js vs. cdn.ampproject.org/v0.js. We should never fetch max AND RTV though.
There was a problem hiding this comment.
It's needed to support canary-ing of ww.js since it's fetched by AMP JS rather than from the AMP page.
There was a problem hiding this comment.
I think better solution here is to always return ${base}/rtv/${getMode().rtvVersion}/${entryPoint}.js And let the local server choose what the right file to serve based on serving mode. Just like what we do with #calculateExtensionScriptUrl?
There was a problem hiding this comment.
Yea, maybe we can make this change in test-server.js along with the other stuff above.
|
@kmh287 A couple of good suggested changes, let's discuss it more tomorrow. 😄 |
kmh287
left a comment
There was a problem hiding this comment.
Thanks for the comments, everyone. I've updated the approach and hopefully addressed all previous comments. PTAL.
build-system/test-server.js
Outdated
| /** | ||
| * Serve entry point script url | ||
| */ | ||
| app.get('/dist/ww.js', function(req, res, next) { |
There was a problem hiding this comment.
I wonder if this is needed.
We have https://github.com/ampproject/amphtml/blob/master/build-system/tasks/karma.conf.js#L188
where we import our gulp webserver as a Karma server middleware.
There was a problem hiding this comment.
In app.js, around line 850, mode is not set to 'default' and so the min version is used.
When I comment out the added code in test-server.js, test-bind-impl.js pulls in the min version or outright fails if gulp dist hasn't been run since the last gulp clean. Witht his code added, the test pulls in the max version.
There was a problem hiding this comment.
Yea, the middleware setting isn't working for some reason. Kevin, can you add a TODO/issue to investigate that?
There was a problem hiding this comment.
we should eventually get rid of this 2nd server running in a different port. This probably can fix your problem: #9561
There was a problem hiding this comment.
@lannka I've pulled in that PR and I'm still seeing errors while testing if I don't first run gulp dist. Without the changes to test-server.js, the test still requests the minified web worker.
There was a problem hiding this comment.
In your test browser, can you try to load "http://localhost:9876/dist/ww.js"? It worked on our end.
There was a problem hiding this comment.
I think he was saying we should eventually get rid of the second server, not that the referenced PR does it.
There was a problem hiding this comment.
@lannka I get the min version only if I've run gulp dist since running gulp clean
There was a problem hiding this comment.
@lannka and I resolved on chat. I needed to update karma.
d531fc1 to
387858e
Compare
387858e to
329da81
Compare
|
@jridgewell @zhouyx @choumx @lannka PTAL |
/to @choumx