MCPcopy Create free account
hub / github.com/bytecodealliance/wrpc / TestAsync

Function TestAsync

tests/go/async_test.go:24–153  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22)
23
24func TestAsync(t *testing.T) {
25 natsSrv := internal.RunNats(t)
26 nc, err := nats.Connect(natsSrv.ClientURL())
27 if err != nil {
28 t.Errorf("failed to connect to NATS.io: %s", err)
29 return
30 }
31 defer nc.Close()
32 defer func() {
33 if err := nc.Drain(); err != nil {
34 t.Errorf("failed to drain NATS.io connection: %s", err)
35 return
36 }
37 }()
38 client := wrpcnats.NewClient(nc, wrpcnats.WithPrefix("go"))
39
40 stop, err := async_server.Serve(client, integration.AsyncHandler{})
41 if err != nil {
42 t.Errorf("failed to serve `async-server` world: %s", err)
43 return
44 }
45
46 var cancel func()
47 ctx := context.Background()
48 dl, ok := t.Deadline()
49 if ok {
50 ctx, cancel = context.WithDeadline(ctx, dl)
51 } else {
52 ctx, cancel = context.WithTimeout(ctx, time.Minute)
53 }
54 defer cancel()
55
56 t.Run("with-streams", func(t *testing.T) {
57 slog.DebugContext(ctx, "calling `wrpc-test:integration/async.with-streams`")
58 byteRx, stringListRx, err := async.WithStreams(ctx, client)
59 if err != nil {
60 t.Errorf("failed to call `wrpc-test:integration/async.with-streams`: %s", err)
61 return
62 }
63 b, err := io.ReadAll(byteRx)
64 if err != nil {
65 t.Errorf("failed to read from stream: %s", err)
66 return
67 }
68 if string(b) != "test" {
69 t.Errorf("expected: `test`, got: %s", string(b))
70 return
71 }
72 if err := byteRx.Close(); err != nil {
73 t.Errorf("failed to close byte reader: %s", err)
74 return
75 }
76
77 ss, err := stringListRx.Receive()
78 if err != nil {
79 t.Errorf("failed to receive ready list<string> stream: %s", err)
80 return
81 }

Callers

nothing calls this directly

Calls 5

WithStreamsMethod · 0.80
IdentityNestedAsyncMethod · 0.80
ServeMethod · 0.65
ReceiveMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected