forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_done_example_test.go
124 lines (97 loc) · 3.84 KB
/
context_done_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package wazero_test
import (
"context"
_ "embed"
"fmt"
"log"
"time"
"github.com/tetratelabs/wazero"
)
// infiniteLoopWasm exports a function named "infinite_loop" that never exits.
//
//go:embed internal/integration_test/engine/testdata/infinite_loop.wasm
var infiniteLoopWasm []byte
// ExampleRuntimeConfig_WithCloseOnContextDone_context_timeout demonstrates how to ensure the termination
// of infinite loop function with context.Context created by context.WithTimeout powered by
// RuntimeConfig.WithEnsureTermination configuration.
func ExampleRuntimeConfig_WithCloseOnContextDone_context_timeout() {
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx,
// Enables the WithCloseOnContextDone option.
wazero.NewRuntimeConfig().WithCloseOnContextDone(true))
defer r.Close(ctx)
moduleInstance, err := r.InstantiateWithConfig(ctx, infiniteLoopWasm,
wazero.NewModuleConfig().WithName("malicious_wasm"))
if err != nil {
log.Panicln(err)
}
infiniteLoop := moduleInstance.ExportedFunction("infinite_loop")
// Create the context.Context to be passed to the invocation of infinite_loop.
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
// Invoke the infinite loop with the timeout context.
_, err = infiniteLoop.Call(ctx)
// Timeout is correctly handled and triggers the termination of infinite loop.
fmt.Println(err)
// Output:
// module closed with context deadline exceeded
}
// ExampleRuntimeConfig_WithCloseOnContextDone_context_cancel demonstrates how to ensure the termination
// of infinite loop function with context.Context created by context.WithCancel powered by
// RuntimeConfig.WithEnsureTermination configuration.
func ExampleRuntimeConfig_WithCloseOnContextDone_context_cancel() {
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx,
// Enables the WithCloseOnContextDone option.
wazero.NewRuntimeConfig().WithCloseOnContextDone(true))
defer r.Close(ctx)
moduleInstance, err := r.InstantiateWithConfig(ctx, infiniteLoopWasm,
wazero.NewModuleConfig().WithName("malicious_wasm"))
if err != nil {
log.Panicln(err)
}
infiniteLoop := moduleInstance.ExportedFunction("infinite_loop")
// Create the context.Context to be passed to the invocation of infinite_loop.
ctx, cancel := context.WithCancel(ctx)
go func() {
// After 2 seconds, cancel the invocation of infinite loop.
time.Sleep(2 * time.Second)
cancel()
}()
// Invoke the infinite loop with the timeout context.
_, err = infiniteLoop.Call(ctx)
// context Cancellation is correctly handled and triggers the termination of infinite loop.
fmt.Println(err)
// Output:
// module closed with context canceled
}
// ExampleRuntimeConfig_WithCloseOnContextDone_moduleClose demonstrates how to ensure the termination
// of infinite loop function with api.Module's CloseWithExitCode method powered by
// RuntimeConfig.WithEnsureTermination configuration.
func ExampleRuntimeConfig_WithCloseOnContextDone_moduleClose() {
ctx := context.Background()
r := wazero.NewRuntimeWithConfig(ctx,
// Enables the WithCloseOnContextDone option.
wazero.NewRuntimeConfig().WithCloseOnContextDone(true))
defer r.Close(ctx)
moduleInstance, err := r.InstantiateWithConfig(ctx, infiniteLoopWasm,
wazero.NewModuleConfig().WithName("malicious_wasm"))
if err != nil {
log.Panicln(err)
}
infiniteLoop := moduleInstance.ExportedFunction("infinite_loop")
go func() {
// After 2 seconds, close the module instance with CloseWithExitCode, which triggers the termination
// of infinite loop.
time.Sleep(2 * time.Second)
if err := moduleInstance.CloseWithExitCode(ctx, 1); err != nil {
log.Panicln(err)
}
}()
// Invoke the infinite loop with the timeout context.
_, err = infiniteLoop.Call(ctx)
// The exit code is correctly handled and triggers the termination of infinite loop.
fmt.Println(err)
// Output:
// module closed with exit_code(1)
}