source_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/benbjohnson/clock"
  21. _ "go.nanomsg.org/mangos/v3/transport/ipc"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/io/mock"
  24. mockContext "github.com/lf-edge/ekuiper/internal/io/mock/context"
  25. "github.com/lf-edge/ekuiper/internal/xsql"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. )
  28. func TestRun(t *testing.T) {
  29. mc := conf.Clock.(*clock.Mock)
  30. exp := []api.SourceTuple{
  31. api.NewDefaultSourceTupleWithTime(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"}, mc.Now()),
  32. api.NewDefaultSourceTupleWithTime(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"}, mc.Now()),
  33. api.NewDefaultSourceTupleWithTime(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"}, mc.Now()),
  34. }
  35. s := GetSource()
  36. err := s.Configure("new", nil)
  37. if err != nil {
  38. t.Errorf(err.Error())
  39. return
  40. }
  41. server, _ := mockNeuron(true, false, DefaultNeuronUrl)
  42. defer server.Close()
  43. mock.TestSourceOpen(s, exp, t)
  44. }
  45. func connectFailTest(t *testing.T) {
  46. s := GetSource()
  47. err := s.Configure("new", nil)
  48. if err != nil {
  49. t.Errorf(err.Error())
  50. return
  51. }
  52. ctx, cancel := mockContext.NewMockContext("ruleTestReconnect", "op1").WithCancel()
  53. consumer := make(chan api.SourceTuple)
  54. errCh := make(chan error)
  55. server, _ := mockNeuron(false, false, DefaultNeuronUrl)
  56. go s.Open(ctx, consumer, errCh)
  57. go func() {
  58. select {
  59. case err := <-errCh:
  60. t.Errorf("received error: %v", err)
  61. case tuple := <-consumer:
  62. if !reflect.DeepEqual(tuple, &xsql.ErrorSourceTuple{Error: fmt.Errorf("neuron connection detached")}) {
  63. t.Errorf("received unexpected tuple: %v", tuple)
  64. }
  65. }
  66. cancel()
  67. }()
  68. time.Sleep(1 * time.Second)
  69. server.Close()
  70. time.Sleep(1 * time.Second)
  71. }