mock_resend_sink.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 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. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/errorx"
  19. )
  20. type MockResendSink struct {
  21. results [][]byte
  22. resentResults [][]byte
  23. count int
  24. onHit chan int
  25. }
  26. func NewMockResendSink(onHit chan int) *MockResendSink {
  27. m := &MockResendSink{onHit: onHit}
  28. return m
  29. }
  30. func (m *MockResendSink) Open(ctx api.StreamContext) error {
  31. log := ctx.GetLogger()
  32. log.Debugln("Opening mock sink")
  33. m.results = make([][]byte, 0)
  34. m.resentResults = make([][]byte, 0)
  35. return nil
  36. }
  37. func (m *MockResendSink) Collect(ctx api.StreamContext, item interface{}) error {
  38. logger := ctx.GetLogger()
  39. defer func() {
  40. m.count++
  41. m.onHit <- m.count
  42. }()
  43. if m.count%2 == 0 {
  44. return fmt.Errorf("%s: mock io error", errorx.IOErr)
  45. }
  46. if v, _, err := ctx.TransformOutput(item); err == nil {
  47. logger.Debugf("mock sink receive %s", item)
  48. m.results = append(m.results, v)
  49. } else {
  50. logger.Info("mock sink transform data error: %v", err)
  51. }
  52. return nil
  53. }
  54. func (m *MockResendSink) CollectResend(ctx api.StreamContext, item interface{}) error {
  55. logger := ctx.GetLogger()
  56. if m.count%3 != 1 {
  57. return fmt.Errorf("%s: mock io error", errorx.IOErr)
  58. }
  59. if v, _, err := ctx.TransformOutput(item); err == nil {
  60. logger.Debugf("mock sink resend %s", item)
  61. m.resentResults = append(m.resentResults, v)
  62. } else {
  63. logger.Info("mock sink transform data error: %v", err)
  64. }
  65. return nil
  66. }
  67. func (m *MockResendSink) Close(_ api.StreamContext) error {
  68. // do nothing
  69. return nil
  70. }
  71. func (m *MockResendSink) Configure(_ map[string]interface{}) error {
  72. return nil
  73. }
  74. func (m *MockResendSink) GetResults() [][]byte {
  75. return m.results
  76. }
  77. func (m *MockResendSink) GetResendResults() [][]byte {
  78. return m.resentResults
  79. }