mock_source.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package test
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/common"
  5. "github.com/emqx/kuiper/xsql"
  6. "github.com/emqx/kuiper/xstream/api"
  7. "sync"
  8. "time"
  9. )
  10. type MockSource struct {
  11. data []*xsql.Tuple
  12. offset int
  13. sync.Mutex
  14. }
  15. const TIMELEAP = 200
  16. func NewMockSource(data []*xsql.Tuple) *MockSource {
  17. mock := &MockSource{
  18. data: data,
  19. }
  20. return mock
  21. }
  22. func (m *MockSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, _ chan<- error) {
  23. log := ctx.GetLogger()
  24. mockClock := GetMockClock()
  25. log.Infof("%d: mock source %s starts", common.GetNowInMilli(), ctx.GetOpId())
  26. log.Debugf("mock source %s starts with offset %d", ctx.GetOpId(), m.offset)
  27. for i, d := range m.data {
  28. if i < m.offset {
  29. log.Debugf("mock source is skipping %d", i)
  30. continue
  31. }
  32. log.Debugf("mock source is waiting %d", i)
  33. diff := d.Timestamp - common.GetNowInMilli()
  34. if diff <= 0 {
  35. log.Warnf("Time stamp invalid, current time is %d, but timestamp is %d", common.GetNowInMilli(), d.Timestamp)
  36. diff = TIMELEAP
  37. }
  38. next := mockClock.After(time.Duration(diff) * time.Millisecond)
  39. //Mock timer, only send out the data once the mock time goes to the timestamp.
  40. //Another mechanism must be imposed to move forward the mock time.
  41. select {
  42. case <-next:
  43. m.Lock()
  44. m.offset = i + 1
  45. consumer <- api.NewDefaultSourceTuple(d.Message, xsql.Metadata{"topic": "mock"})
  46. log.Debugf("%d: mock source %s is sending data %d:%s", common.TimeToUnixMilli(mockClock.Now()), ctx.GetOpId(), i, d)
  47. m.Unlock()
  48. case <-ctx.Done():
  49. log.Debugf("mock source open DONE")
  50. return
  51. }
  52. }
  53. log.Debugf("mock source sends out all data")
  54. }
  55. func (m *MockSource) GetOffset() (interface{}, error) {
  56. m.Lock()
  57. defer m.Unlock()
  58. return m.offset, nil
  59. }
  60. func (m *MockSource) Rewind(offset interface{}) error {
  61. oi, err := common.ToInt(offset)
  62. if err != nil {
  63. return fmt.Errorf("mock source fails to rewind: %s", err)
  64. } else {
  65. m.offset = oi
  66. }
  67. return nil
  68. }
  69. func (m *MockSource) Close(_ api.StreamContext) error {
  70. m.offset = 0
  71. return nil
  72. }
  73. func (m *MockSource) Configure(_ string, _ map[string]interface{}) error {
  74. return nil
  75. }