MCPcopy Create free account
hub / github.com/dispatchrun/wazergo / TestMultipleHostModuleInstances

Function TestMultipleHostModuleInstances

module_test.go:50–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

48}
49
50func TestMultipleHostModuleInstances(t *testing.T) {
51 ctx := context.Background()
52
53 runtime := wazero.NewRuntime(ctx)
54 defer runtime.Close(ctx)
55
56 // three copies, all share the same host module name but different state
57 instance0 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(0))
58 instance1 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(21))
59 instance2 := wazergo.MustInstantiate(ctx, runtime, hostModule, answer(42))
60
61 defer instance0.Close(ctx)
62 defer instance1.Close(ctx)
63 defer instance2.Close(ctx)
64
65 guest, err := loadModule(ctx, runtime, "testdata/answer.wasm")
66 if err != nil {
67 t.Fatal(err)
68 }
69 defer guest.Close(ctx)
70
71 answer := guest.ExportedFunction("answer")
72 r0, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance0))
73 r1, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance1))
74 r2, _ := answer.Call(wazergo.WithModuleInstance(ctx, instance2))
75
76 for i, test := range [...]struct{ want, got int }{
77 {want: 0, got: int(r0[0])},
78 {want: 21, got: int(r1[0])},
79 {want: 42, got: int(r2[0])},
80 } {
81 if test.want != test.got {
82 t.Errorf("result %d is wrong: want=%d got=%d", i, test.want, test.got)
83 }
84 }
85}
86
87func loadModule(ctx context.Context, runtime wazero.Runtime, filePath string) (api.Module, error) {
88 b, err := os.ReadFile(filePath)

Callers

nothing calls this directly

Calls 7

MustInstantiateFunction · 0.92
WithModuleInstanceFunction · 0.92
answerFunction · 0.85
loadModuleFunction · 0.85
ExportedFunctionMethod · 0.80
CloseMethod · 0.45
CallMethod · 0.45

Tested by

no test coverage detected