forked from tetratelabs/wazero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_test.go
61 lines (49 loc) · 1.97 KB
/
namespace_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
package wazero
import (
_ "embed"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// TestRuntime_Namespace ensures namespaces are independent.
func TestRuntime_Namespace(t *testing.T) {
r := NewRuntime(testCtx)
defer r.Close(testCtx)
// Compile a module to add to the runtime
compiled, err := r.NewHostModuleBuilder("env").Compile(testCtx)
require.NoError(t, err)
// Instantiate "env" into the runtime default namespace (base case)
require.Nil(t, r.Module("env"))
m1, err := r.InstantiateModule(testCtx, compiled, NewModuleConfig())
require.NoError(t, err)
require.Equal(t, m1, r.Module("env"))
// NewNamespace does not inherit modules in the default namespace
ns1 := r.NewNamespace(testCtx)
require.Nil(t, ns1.Module("env"))
// Ensure this namespace has a new instance of "env"
m2, err := ns1.InstantiateModule(testCtx, compiled, NewModuleConfig())
require.NoError(t, err)
require.NotSame(t, m1, m2)
// Ensure the next namespace is similarly independent.
ns2 := r.NewNamespace(testCtx)
m3, err := ns2.InstantiateModule(testCtx, compiled, NewModuleConfig())
require.NoError(t, err)
require.NotSame(t, m1, m3)
require.NotSame(t, m2, m3)
// Ensure we can't re-instantiate the same module multiple times.
_, err = ns2.InstantiateModule(testCtx, compiled, NewModuleConfig())
require.EqualError(t, err, "module[env] has already been instantiated")
// Ensure we can instantiate the same module multiple times.
m4, err := ns2.InstantiateModule(testCtx, compiled, NewModuleConfig().WithName("env2"))
require.NoError(t, err)
require.NotSame(t, m3, m4)
// Ensure closing one namespace doesn't affect another
require.NoError(t, ns2.Close(testCtx))
require.Nil(t, ns2.Module("env"))
require.Nil(t, ns2.Module("env2"))
require.Equal(t, m1, r.Module("env"))
require.Equal(t, m2, ns1.Module("env"))
// Ensure closing the Runtime closes all namespaces
require.NoError(t, r.Close(testCtx))
require.Nil(t, r.Module("env"))
require.Nil(t, ns1.Module("env"))
}