How to test the handlers itself?

60 views
Skip to first unread message

Ricardo Ambrogi

unread,
Mar 11, 2023, 4:33:53 PM3/11/23
to Service Weaver
Hi team,

I'm creating a PoC with Service Weaver and some gRPC services, and for me, it's unclear how to call the APIs directly from the tests. The docs show how to test the components, but I cannot find an example of how to call the APIs from the root component.

I used to have something like this:
mux := http.NewServeMux()
server := httptest.NewUnstartedServer(mux)
server.EnableHTTP2 = true
userClient := userv1connect.NewUserServiceClient(server.Client(), server.URL)

And then I could call the userClient.SomeRoute inside my tests. Where SomeRoute is defined as a method of the root component.

So, how can I test the root component, including its handlers?

Ricardo Ambrogi

unread,
Mar 13, 2023, 11:13:51 AM3/13/23
to Service Weaver
Just to make it clear, I was able to test it, what I mean is, what's the correct way, as it looks like I can only run the tests with `SingleProcess`, and they are very flaky, so I'm not sure if the approach below is the way to go:


mux := http.NewServeMux()
server := httptest.NewUnstartedServer(mux)
server.EnableHTTP2 = true

root := weavertest.Init(context.Background(), t, weavertest.Options{Config: config, SingleProcess: true})
s, err := srv.NewServer(root)
if err != nil {
t.Fatal(err)

rgrandl

unread,
Mar 13, 2023, 12:44:25 PM3/13/23
to Service Weaver
Thanks for trying this out. I don't think I understand what are you trying to do here. 

weaver test is designed to allow you to test interactions between components. 

In your code, I see that you are trying to instantiate some server by yourself? Also, I am not sure which APIs that you can't access from the root component are you talking about.

Can you give us more details on what are you trying to do here?

Ricardo Ambrogi

unread,
Mar 15, 2023, 11:50:01 AM3/15/23
to Service Weaver
Oh, my bad for the miscommunication there. When I said API, I meant the HTTP handlers, and how could I  create a Service Weaver server with the httptest handler, so I could call the HTTP handlers from my test. But now I see that this doesn't relate to service weaver directly per se. But connecting the dots.
Reply all
Reply to author
Forward
0 new messages