source_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2022-2023 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 neuron
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/io/mock"
  18. "github.com/lf-edge/ekuiper/internal/xsql"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. _ "go.nanomsg.org/mangos/v3/transport/ipc"
  21. "reflect"
  22. "testing"
  23. "time"
  24. )
  25. func TestRun(t *testing.T) {
  26. exp := []api.SourceTuple{
  27. api.NewDefaultSourceTuple(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "yellow"}, "errors": map[string]interface{}{"tag_name3": 122.0}}, map[string]interface{}{"topic": "$$neuron_ipc:///tmp/neuron-ekuiper.ipc"}),
  28. api.NewDefaultSourceTuple(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 11.22, "tag_name2": "green", "tag_name3": 60.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_ipc:///tmp/neuron-ekuiper.ipc"}),
  29. api.NewDefaultSourceTuple(map[string]interface{}{"group_name": "group1", "timestamp": 1646125996000.0, "node_name": "node1", "values": map[string]interface{}{"tag_name1": 15.4, "tag_name2": "green", "tag_name3": 70.0}, "errors": map[string]interface{}{}}, map[string]interface{}{"topic": "$$neuron_ipc:///tmp/neuron-ekuiper.ipc"}),
  30. }
  31. s := GetSource()
  32. err := s.Configure("new", nil)
  33. if err != nil {
  34. t.Errorf(err.Error())
  35. return
  36. }
  37. server, _ := mockNeuron(true, false, DefaultNeuronUrl)
  38. defer server.Close()
  39. mock.TestSourceOpen(s, exp, t)
  40. }
  41. func connectFailTest(t *testing.T) {
  42. s := GetSource()
  43. err := s.Configure("new", nil)
  44. if err != nil {
  45. t.Errorf(err.Error())
  46. return
  47. }
  48. ctx, cancel := mock.NewMockContext("ruleTestReconnect", "op1").WithCancel()
  49. consumer := make(chan api.SourceTuple)
  50. errCh := make(chan error)
  51. server, _ := mockNeuron(false, false, DefaultNeuronUrl)
  52. go s.Open(ctx, consumer, errCh)
  53. go func() {
  54. select {
  55. case err := <-errCh:
  56. t.Errorf("received error: %v", err)
  57. case tuple := <-consumer:
  58. if !reflect.DeepEqual(tuple, &xsql.ErrorSourceTuple{Error: fmt.Errorf("neuron connection detached")}) {
  59. t.Errorf("received unexpected tuple: %v", tuple)
  60. }
  61. }
  62. cancel()
  63. }()
  64. time.Sleep(1 * time.Second)
  65. server.Close()
  66. time.Sleep(1 * time.Second)
  67. }