@@ -5,6 +5,13 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "log"
9+ "os"
10+ "strings"
11+ "sync"
12+ "testing"
13+ "time"
14+
815 observe "github.com/dylibso/observe-sdk/go"
916 "github.com/dylibso/observe-sdk/go/adapter/stdout"
1017 "github.com/stretchr/testify/assert"
@@ -13,12 +20,6 @@ import (
1320 "github.com/tetratelabs/wazero/experimental"
1421 "github.com/tetratelabs/wazero/experimental/logging"
1522 "github.com/tetratelabs/wazero/sys"
16- "log"
17- "os"
18- "strings"
19- "sync"
20- "testing"
21- "time"
2223)
2324
2425func TestWasmUrl (t * testing.T ) {
@@ -1038,6 +1039,170 @@ func TestEnableExperimentalFeature(t *testing.T) {
10381039 }
10391040}
10401041
1042+ func TestModuleLinking (t * testing.T ) {
1043+ manifest := Manifest {
1044+ Wasm : []Wasm {
1045+ WasmFile {
1046+ Path : "wasm/lib.wasm" ,
1047+ Name : "lib" ,
1048+ },
1049+ WasmFile {
1050+ Path : "wasm/main.wasm" ,
1051+ Name : "main" ,
1052+ },
1053+ },
1054+ }
1055+
1056+ if plugin , ok := pluginInstance (t , manifest ); ok {
1057+ defer plugin .Close (context .Background ())
1058+
1059+ exit , output , err := plugin .Call ("run_test" , []byte ("benjamin" ))
1060+
1061+ if assertCall (t , err , exit ) {
1062+ expected := "Hello, BENJAMIN"
1063+
1064+ actual := string (output )
1065+
1066+ assert .Equal (t , expected , actual )
1067+ }
1068+ }
1069+ }
1070+
1071+ func TestModuleLinkingMultipleInstances (t * testing.T ) {
1072+ manifest := Manifest {
1073+ Wasm : []Wasm {
1074+ WasmFile {
1075+ Path : "wasm/lib.wasm" ,
1076+ Name : "lib" ,
1077+ },
1078+ WasmFile {
1079+ Path : "wasm/main.wasm" ,
1080+ Name : "main" ,
1081+ },
1082+ },
1083+ }
1084+
1085+ ctx := context .Background ()
1086+ config := wasiPluginConfig ()
1087+
1088+ compiledPlugin , err := NewCompiledPlugin (ctx , manifest , PluginConfig {
1089+ EnableWasi : true ,
1090+ }, []HostFunction {})
1091+
1092+ if err != nil {
1093+ t .Fatalf ("Could not create plugin: %v" , err )
1094+ }
1095+
1096+ for i := 0 ; i < 3 ; i ++ {
1097+ plugin , err := compiledPlugin .Instance (ctx , config )
1098+ if err != nil {
1099+ t .Fatalf ("Could not create plugin instance: %v" , err )
1100+ }
1101+ // purposefully not closing the plugin instance
1102+
1103+ for j := 0 ; j < 3 ; j ++ {
1104+
1105+ exit , output , err := plugin .Call ("run_test" , []byte ("benjamin" ))
1106+
1107+ if assertCall (t , err , exit ) {
1108+ expected := "Hello, BENJAMIN"
1109+
1110+ actual := string (output )
1111+
1112+ assert .Equal (t , expected , actual )
1113+ }
1114+ }
1115+ }
1116+ }
1117+
1118+ func TestCompiledModuleMultipleInstances (t * testing.T ) {
1119+ manifest := Manifest {
1120+ Wasm : []Wasm {
1121+ WasmFile {
1122+ Path : "wasm/count_vowels.wasm" ,
1123+ Name : "main" ,
1124+ },
1125+ },
1126+ }
1127+
1128+ ctx := context .Background ()
1129+ config := wasiPluginConfig ()
1130+
1131+ compiledPlugin , err := NewCompiledPlugin (ctx , manifest , PluginConfig {
1132+ EnableWasi : true ,
1133+ }, []HostFunction {})
1134+
1135+ if err != nil {
1136+ t .Fatalf ("Could not create plugin: %v" , err )
1137+ }
1138+
1139+ var wg sync.WaitGroup
1140+ numInstances := 300
1141+
1142+ // Create and test instances in parallel
1143+ for i := 0 ; i < numInstances ; i ++ {
1144+ wg .Add (1 )
1145+ go func (instanceNum int ) {
1146+ defer wg .Done ()
1147+
1148+ plugin , err := compiledPlugin .Instance (ctx , config )
1149+ if err != nil {
1150+ t .Errorf ("Could not create plugin instance %d: %v" , instanceNum , err )
1151+ return
1152+ }
1153+ // purposefully not closing the plugin instance
1154+
1155+ // Sequential calls for this instance
1156+ for j := 0 ; j < 3 ; j ++ {
1157+ exit , _ , err := plugin .Call ("count_vowels" , []byte ("benjamin" ))
1158+ if err != nil {
1159+ t .Errorf ("Instance %d, call %d failed: %v" , instanceNum , j , err )
1160+ return
1161+ }
1162+ if exit != 0 {
1163+ t .Errorf ("Instance %d, call %d returned non-zero exit code: %d" , instanceNum , j , exit )
1164+ }
1165+ }
1166+ }(i )
1167+ }
1168+ wg .Wait ()
1169+ }
1170+
1171+ func TestMultipleCallsOutputParallel (t * testing.T ) {
1172+ manifest := manifest ("count_vowels.wasm" )
1173+ numInstances := 300
1174+
1175+ var wg sync.WaitGroup
1176+
1177+ // Create and test instances in parallel
1178+ for i := 0 ; i < numInstances ; i ++ {
1179+ wg .Add (1 )
1180+ go func (instanceNum int ) {
1181+ defer wg .Done ()
1182+
1183+ if plugin , ok := pluginInstance (t , manifest ); ok {
1184+ defer plugin .Close (context .Background ())
1185+
1186+ // Sequential calls for this instance
1187+ exit , output1 , err := plugin .Call ("count_vowels" , []byte ("aaa" ))
1188+ if ! assertCall (t , err , exit ) {
1189+ return
1190+ }
1191+
1192+ exit , output2 , err := plugin .Call ("count_vowels" , []byte ("bbba" ))
1193+ if ! assertCall (t , err , exit ) {
1194+ return
1195+ }
1196+
1197+ assert .Equal (t , `{"count":3,"total":3,"vowels":"aeiouAEIOU"}` , string (output1 ))
1198+ assert .Equal (t , `{"count":1,"total":4,"vowels":"aeiouAEIOU"}` , string (output2 ))
1199+ }
1200+ }(i )
1201+ }
1202+
1203+ wg .Wait ()
1204+ }
1205+
10411206func BenchmarkInitialize (b * testing.B ) {
10421207 ctx := context .Background ()
10431208 cache := wazero .NewCompilationCache ()
0 commit comments