wasm_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wasm_test
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sync"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/internal/plugin/wasm"
  22. "github.com/lf-edge/ekuiper/internal/plugin/wasm/runtime"
  23. "github.com/lf-edge/ekuiper/internal/topo/context"
  24. "github.com/lf-edge/ekuiper/internal/topo/state"
  25. "github.com/lf-edge/ekuiper/pkg/api"
  26. )
  27. // EDIT HERE: Define the plugins that you want to test.
  28. var testingPlugin = &wasm.PluginInfo{
  29. PluginMeta: runtime.PluginMeta{
  30. Name: "get_exchange_rate",
  31. Version: "v1",
  32. WasmEngine: "wasmedge",
  33. },
  34. Functions: []string{"get_exchange_rate"},
  35. }
  36. var FuncData = []interface{}{
  37. // int32(25),
  38. "USD", "CNY",
  39. }
  40. var (
  41. i int32 = 121393
  42. ResData = []int32{i}
  43. )
  44. var (
  45. m *wasm.Manager
  46. ctx api.StreamContext
  47. cancels sync.Map
  48. )
  49. func TestExec(t *testing.T) {
  50. var err error
  51. m, err = wasm.MockManager(map[string]*wasm.PluginInfo{testingPlugin.Name: testingPlugin})
  52. if err != nil {
  53. panic(err)
  54. }
  55. c := context.WithValue(context.Background(), context.LoggerKey, conf.Log)
  56. ctx = c.WithMeta("rule1", "op1", &state.MemoryStore{}).WithInstance(1)
  57. // ctrl := &Control{}
  58. f, err := m.Function(testingPlugin.Functions[0])
  59. if err != nil {
  60. fmt.Println("[wasm_test_server.go] err:", err)
  61. return
  62. }
  63. newctx, _ := ctx.WithCancel()
  64. fc := context.NewDefaultFuncContext(newctx, 1)
  65. r, ok := f.Exec(FuncData, fc)
  66. if !ok {
  67. fmt.Print("cannot exec func\n")
  68. }
  69. if reflect.DeepEqual(ResData, r) { //! ==
  70. t.Errorf("error mismatch:\n exp=%d\n got=%d\n\n", ResData, r)
  71. } else {
  72. fmt.Println("success")
  73. }
  74. }