github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/vm/wasmtime/instance_test.go (about) 1 package wasmtime_test 2 3 /* 4 import ( 5 _ "embed" 6 "fmt" 7 "os" 8 "path/filepath" 9 // . "github.com/onsi/gomega" 10 ) 11 12 var ( 13 wasmLogCode []byte 14 wasmGJsonCode []byte 15 wasmEasyJsonCode []byte 16 wasmWordCountCode []byte 17 wasmWordCountV2Code []byte 18 wasmTokenDistributeCode []byte 19 20 // ctx context.Context 21 // idg confid.SFIDGenerator 22 ) 23 24 func init() { 25 wd, _ := os.Getwd() 26 fmt.Println(wd) 27 root := filepath.Join(wd, "../../../../_examples") 28 fmt.Println(root) 29 30 var err error 31 wasmLogCode, err = os.ReadFile(filepath.Join(root, "log/log.wasm")) 32 if err != nil { 33 panic(err) 34 } 35 36 wasmGJsonCode, err = os.ReadFile(filepath.Join(root, "gjson/gjson.wasm")) 37 if err != nil { 38 panic(err) 39 } 40 wasmEasyJsonCode, err = os.ReadFile(filepath.Join(root, "easyjson/easyjson.wasm")) 41 if err != nil { 42 panic(err) 43 } 44 wasmWordCountCode, err = os.ReadFile(filepath.Join(root, "word_count/word_count.wasm")) 45 if err != nil { 46 panic(err) 47 } 48 wasmWordCountV2Code, err = os.ReadFile(filepath.Join(root, "word_count_v2/word_count_v2.wasm")) 49 if err != nil { 50 panic(err) 51 } 52 53 wasmTokenDistributeCode, err = os.ReadFile(filepath.Join(root, "token_distribute/token_distribute.wasm")) 54 if err != nil { 55 panic(err) 56 } 57 58 /* 59 ctx = global.WithContext(context.Background()) 60 ctx = types.WithETHClientConfig(ctx, &types.ETHClientConfig{ 61 PrivateKey: "", 62 ChainEndpoint: "https://babel-api.testnet.iotex.io", 63 }) 64 65 idg = confid.MustSFIDGeneratorFromContext(ctx) 66 67 go kit.Run(tasks.Root, global.TaskServer()) 68 } 69 70 func TestInstance_LogWASM(t *testing.T) { 71 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmLogCode) 72 NewWithT(t).Expect(err).To(BeNil()) 73 id := vm.AddInstance(ctx, i) 74 75 err = vm.StartInstance(ctx, id) 76 NewWithT(t).Expect(err).To(BeNil()) 77 defer vm.StopInstance(ctx, id) 78 79 ret := i.HandleEvent(ctx, "start", []byte("IoTeX")) 80 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 81 NewWithT(t).Expect(ret.ErrMsg).To(Equal("")) 82 83 ret = i.HandleEvent(ctx, "not_exported", []byte("IoTeX")) 84 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_UnexportedHandler)) 85 } 86 87 func TestInstance_GJsonWASM(t *testing.T) { 88 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmGJsonCode) 89 NewWithT(t).Expect(err).To(BeNil()) 90 id := vm.AddInstance(ctx, i) 91 92 err = vm.StartInstance(ctx, id) 93 NewWithT(t).Expect(err).To(BeNil()) 94 defer vm.StopInstance(ctx, id) 95 96 ret := i.HandleEvent(ctx, "start", []byte(` 97 { 98 "name": { 99 "first": "Tom", 100 "last": "Anderson", 101 "age": 39 102 }, 103 "friends": [ 104 { 105 "first_name": "Dale", 106 "last_name": "Murphy", 107 "age": 44, 108 "nets": [ 109 "ig", 110 "fb", 111 "tw" 112 ] 113 }, 114 { 115 "first_name": "Roger", 116 "last_name": "Craig", 117 "age": 68, 118 "nets": [ 119 "fb", 120 "tw" 121 ] 122 }, 123 { 124 "first_name": "Jane", 125 "last_name": "Murphy", 126 "age": 47, 127 "nets": [ 128 "ig", 129 "tw" 130 ] 131 } 132 ] 133 }`)) 134 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 135 } 136 137 func TestInstance_EasyJsonWASM(t *testing.T) { 138 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmEasyJsonCode) 139 NewWithT(t).Expect(err).To(BeNil()) 140 id := vm.AddInstance(ctx, i) 141 142 err = vm.StartInstance(ctx, id) 143 NewWithT(t).Expect(err).To(BeNil()) 144 defer vm.StopInstance(ctx, id) 145 146 ret := i.HandleEvent(ctx, "start", []byte(` 147 { 148 "id": 11, 149 "student_name": "Tom", 150 "student_school": { 151 "school_name": "MIT", 152 "school_addr": "xz" 153 }, 154 "birthday": "2017-08-04T20:58:07.9894603+08:00" 155 }`)) 156 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 157 } 158 159 func TestInstance_WordCount(t *testing.T) { 160 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmWordCountCode) 161 NewWithT(t).Expect(err).To(BeNil()) 162 id := vm.AddInstance(ctx, i) 163 164 err = vm.StartInstance(ctx, id) 165 NewWithT(t).Expect(err).To(BeNil()) 166 defer vm.StopInstance(ctx, id) 167 168 ret := i.HandleEvent(ctx, "start", []byte("a b c d a")) 169 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 170 171 NewWithT(t).Expect(i.Get("a")).To(Equal(int32(2))) 172 NewWithT(t).Expect(i.Get("b")).To(Equal(int32(1))) 173 NewWithT(t).Expect(i.Get("c")).To(Equal(int32(1))) 174 NewWithT(t).Expect(i.Get("d")).To(Equal(int32(1))) 175 176 ret = i.HandleEvent(ctx, "start", []byte("a b c d a")) 177 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 178 179 NewWithT(t).Expect(i.Get("a")).To(Equal(int32(4))) 180 NewWithT(t).Expect(i.Get("b")).To(Equal(int32(2))) 181 NewWithT(t).Expect(i.Get("c")).To(Equal(int32(2))) 182 NewWithT(t).Expect(i.Get("d")).To(Equal(int32(2))) 183 } 184 185 func TestInstance_WordCountV2(t *testing.T) { 186 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmWordCountV2Code) 187 NewWithT(t).Expect(err).To(BeNil()) 188 id := vm.AddInstance(ctx, i) 189 190 err = vm.StartInstance(ctx, id) 191 NewWithT(t).Expect(err).To(BeNil()) 192 defer vm.StopInstance(ctx, id) 193 194 ret := i.HandleEvent(ctx, "start", []byte("a b c d a")) 195 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 196 197 NewWithT(t).Expect(i.Get("a")).To(Equal(int32(2))) 198 NewWithT(t).Expect(i.Get("b")).To(Equal(int32(1))) 199 NewWithT(t).Expect(i.Get("c")).To(Equal(int32(1))) 200 NewWithT(t).Expect(i.Get("d")).To(Equal(int32(1))) 201 202 ret = i.HandleEvent(ctx, "start", []byte("a b c d a")) 203 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 204 205 NewWithT(t).Expect(i.Get("a")).To(Equal(int32(4))) 206 NewWithT(t).Expect(i.Get("b")).To(Equal(int32(2))) 207 NewWithT(t).Expect(i.Get("c")).To(Equal(int32(2))) 208 NewWithT(t).Expect(i.Get("d")).To(Equal(int32(2))) 209 210 ret = i.HandleEvent(ctx, "word_count", nil) 211 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode(4))) 212 } 213 214 func TestInstance_TokenDistribute(t *testing.T) { 215 i, err := wasmtime.NewInstanceByCode(ctx, idg.MustGenSFID(), wasmTokenDistributeCode) 216 NewWithT(t).Expect(err).To(BeNil()) 217 id := vm.AddInstance(ctx, i) 218 219 err = vm.StartInstance(ctx, id) 220 NewWithT(t).Expect(err).To(BeNil()) 221 defer vm.StopInstance(ctx, id) 222 223 for idx := int32(0); idx < 20; idx++ { 224 ret := i.HandleEvent(ctx, "start", []byte("test")) 225 NewWithT(t).Expect(ret.Code).To(Equal(wasm.ResultStatusCode_OK)) 226 NewWithT(t).Expect(i.Get("clicks")).To(Equal(idx + 1)) 227 } 228 } 229 */