mock_source.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2021-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 mocknode
  15. import (
  16. "fmt"
  17. "sync"
  18. "time"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/topo/topotest/mockclock"
  21. "github.com/lf-edge/ekuiper/internal/xsql"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/cast"
  24. )
  25. type MockSource struct {
  26. data []*xsql.Tuple
  27. offset int
  28. sync.Mutex
  29. }
  30. const TIMELEAP = 200
  31. func (m *MockSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  32. log := ctx.GetLogger()
  33. mockClock := mockclock.GetMockClock()
  34. log.Infof("%d: mock source %s starts", conf.GetNowInMilli(), ctx.GetOpId())
  35. log.Debugf("mock source %s starts with offset %d", ctx.GetOpId(), m.offset)
  36. for i, d := range m.data {
  37. if i < m.offset {
  38. log.Debugf("mock source is skipping %d", i)
  39. continue
  40. }
  41. log.Debugf("mock source is waiting %d", i)
  42. diff := d.Timestamp - conf.GetNowInMilli()
  43. if diff <= 0 {
  44. log.Warnf("Time stamp invalid, current time is %d, but timestamp is %d", conf.GetNowInMilli(), d.Timestamp)
  45. diff = TIMELEAP
  46. }
  47. next := mockClock.After(time.Duration(diff) * time.Millisecond)
  48. // Mock timer, only send out the data once the mock time goes to the timestamp.
  49. // Another mechanism must be imposed to move forward the mock time.
  50. select {
  51. case <-next:
  52. m.Lock()
  53. m.offset = i + 1
  54. consumer <- api.NewDefaultSourceTupleWithTime(d.Message, xsql.Metadata{"topic": "mock"}, mockClock.Now())
  55. log.Debugf("%d: mock source %s is sending data %d:%s", cast.TimeToUnixMilli(mockClock.Now()), ctx.GetOpId(), i, d)
  56. m.Unlock()
  57. case <-ctx.Done():
  58. log.Debugf("mock source open DONE")
  59. return
  60. }
  61. }
  62. log.Debugf("mock source sends out all data")
  63. }
  64. func (m *MockSource) GetOffset() (interface{}, error) {
  65. m.Lock()
  66. defer m.Unlock()
  67. return m.offset, nil
  68. }
  69. func (m *MockSource) Rewind(offset interface{}) error {
  70. oi, err := cast.ToInt(offset, cast.STRICT)
  71. if err != nil {
  72. return fmt.Errorf("mock source fails to rewind: %s", err)
  73. } else {
  74. m.offset = oi
  75. }
  76. return nil
  77. }
  78. func (m *MockSource) Close(_ api.StreamContext) error {
  79. m.offset = 0
  80. return nil
  81. }
  82. func (m *MockSource) Configure(dataKey string, _ map[string]interface{}) error {
  83. m.data = TestData[dataKey]
  84. return nil
  85. }