agnivade/wasmbrowsertest

goroutine support

unicomp21 opened this issue · 14 comments

how do i run the go scheduler, and goroutines/channels within my browser tests?

I am not sure I follow. You write tests as usual, using goroutines/channels and what not, and then just use the tool to execute them in the browser.

What if I want to run goroutines inside the browser, as part of the component being tested? Doesn't the go scheduler need to be invoked somehow by the browser event loop?

The entire Go runtime is compiled into the wasm binary. You can create goroutines as normal and they will be executed as if GOMAXPROCS was 1. I'd suggest to read some more tutorials on Go WASM to understand it a bit more.

The primary thread, ie GOMAXPROCS 1, in the browser, where does it run? Is it a web worker thread? Or does it share time on the main browser thread? If there is a link somewhere explaining all this, I'd be grateful. I've been googling around trying to find an explanation.

Maybe I'm asking the wrong questions. Is there a wasmbrowser test, where the go component being tested is using goroutines, already in github somewhere?

@unicomp21 what is your overall goal here? If your goal is to build a simple program with Go running in the browser, this is a great resource to help you get started: https://www.aaron-powell.com/posts/2019-02-04-golang-wasm-1-introduction/

If you have already taken that step and want to take the next step - writing an app in the browser - then you'll undoubtedly want to also run tests in your browser. wasmbrowsertest helps you do this by spinning up Chrome / Chromedp and the taking your test binary which is compiled with the WASM target, loading it into the browser, and then executing the tests for you and reporting on the test results for you. I think of wasmbrowsertest as a thin layer over top of Go's built in WASM support whose sole purpose is to make sure your tests work in your target GOOS/GOARCH. From a Go language development perspective, it was super important to be able to regression test the Go language across every GOOS/GOARCH it is targeting. Wasmbrowsertest fulfills that need for the WASM compilation target. From an app development perspective this is also helpful - I usually would run my tests against a windows compilation target but the WASM compilation target has some nuances so it's best to test against it too.

I personally took both of the above steps maybe a year ago? The next step was to test code which interacts across the WASM <-> Javascript divide. To do this, you will ultimately need to write your own javascript which interacts with your Go program. Tinygo has some tutorials on how to do this and you can find some other information in the Golang language's WASM help center. This step will NOT involve wasmbrowsertest because wasmbrowsertest was not created to run your func main - it was created to run your func TestMain. To complete this step I did a lot of manual testing.

If you understand the basics of how the whole chain of technologies work together - Go, WASM, JS - then at least for me I had an "ah ha!" moment and I understood how wasmbrowsertest works.

I think the problem right now @unicomp21 is that your questions don't exactly make sense.

wasmbrowsertest is an infrastructure tool. It does not know or care how Golang deals with GOMAXPROCS, goroutines, or any other language feature is mapped into WASM. Its job is to take a WASM binary which was compiled by the Go language, put it into a browser, run the binary, and then convert the outputs from the WASM code into a format which is identical or nearly identical to the outputs from the go test command we all know and love.

If your goal is to learn about how Go's WASM compilation target works, I strongly recommend first following that tutorial I linked and then reading all of the files in https://github.com/golang/go/tree/master/misc/wasm

TinyGo has similar files which you have already linked which have similar educational value (although I personally am completely unaware of how TinyGo works).

It does not know or care how Golang deals with GOMAXPROCS

That's the problem, I do care. My struggle is understanding how the executor/scheduler is working in both the test runner and in the actual web browser app.

@unicomp21

The primary thread, ie GOMAXPROCS 1, in the browser, where does it run? Is it a web worker thread?

I feel like you are asking how does WebAssembly work. There are tons of material out there which should help you get started.

Here are some talks that you can watch: https://github.com/golang/go/wiki/webassembly#go-webassembly-talks
Here's an introductory blog post I wrote: https://blog.gopheracademy.com/advent-2018/go-in-the-browser/.

If you have further questions, please feel free to drop in to the #webassembly channel in Gophers slack.

I'll close the issue as I think it's out of scope for this tool. But if you have any questions on the tool, please feel free to create a new issue.

lol

The slack channel is perfect for the questions you are asking.

Perhaps this would have been better in discussions ;)

Not really, what you are asking about isn't about wasmbrowsertest. :) The slack channel would be great for this.