wasm_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/plugin/wasm"
  19. "github.com/lf-edge/ekuiper/internal/plugin/wasm/runtime"
  20. "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "reflect"
  24. "sync"
  25. "testing"
  26. )
  27. // EDIT HERE: Define the plugins that you want to test.
  28. var testingPlugin = &wasm.PluginInfo{
  29. PluginMeta: runtime.PluginMeta{
  30. Name: "fibonacci",
  31. Version: "v1",
  32. WasmEngine: "wasmedge",
  33. },
  34. Functions: []string{"fib"},
  35. }
  36. var FuncData = []interface{}{
  37. 25.0, // float
  38. }
  39. var i int32 = 121393
  40. var ResData = []int32{i}
  41. var (
  42. m *wasm.Manager
  43. ctx api.StreamContext
  44. cancels sync.Map
  45. )
  46. func TestExec(t *testing.T) {
  47. var err error
  48. m, err = wasm.MockManager(map[string]*wasm.PluginInfo{testingPlugin.Name: testingPlugin})
  49. if err != nil {
  50. panic(err)
  51. }
  52. c := context.WithValue(context.Background(), context.LoggerKey, conf.Log)
  53. ctx = c.WithMeta("rule1", "op1", &state.MemoryStore{}).WithInstance(1)
  54. //ctrl := &Control{}
  55. f, err := m.Function(testingPlugin.Functions[0])
  56. if err != nil {
  57. fmt.Println("[wasm_test_server.go] err:", err)
  58. return
  59. }
  60. newctx, _ := ctx.WithCancel()
  61. fc := context.NewDefaultFuncContext(newctx, 1)
  62. r, ok := f.Exec(FuncData, fc)
  63. if !ok {
  64. fmt.Print("cannot exec func\n")
  65. }
  66. if reflect.DeepEqual(ResData, r) { //! ==
  67. t.Errorf("error mismatch:\n exp=%d\n got=%d\n\n", ResData, r)
  68. } else {
  69. fmt.Println("success")
  70. }
  71. }