source_test.go 3.0 KB

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