mock.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2021 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 mock
  15. import (
  16. "context"
  17. "time"
  18. filename "github.com/keepeye/logrus-filename"
  19. "github.com/sirupsen/logrus"
  20. "github.com/lf-edge/ekuiper/sdk/go/api"
  21. )
  22. type mockContext struct {
  23. Ctx context.Context
  24. RuleId string
  25. OpId string
  26. }
  27. // Implement context interface
  28. func (c *mockContext) Deadline() (deadline time.Time, ok bool) {
  29. return c.Ctx.Deadline()
  30. }
  31. func (c *mockContext) Done() <-chan struct{} {
  32. return c.Ctx.Done()
  33. }
  34. func (c *mockContext) Err() error {
  35. return c.Ctx.Err()
  36. }
  37. func (c *mockContext) Value(key interface{}) interface{} {
  38. return c.Ctx.Value(key)
  39. }
  40. // Stream metas
  41. func (c *mockContext) GetContext() context.Context {
  42. return c.Ctx
  43. }
  44. func (c *mockContext) GetLogger() api.Logger {
  45. return Logger
  46. }
  47. func (c *mockContext) GetRuleId() string {
  48. return c.RuleId
  49. }
  50. func (c *mockContext) GetOpId() string {
  51. return c.OpId
  52. }
  53. func (c *mockContext) GetInstanceId() int {
  54. return 0
  55. }
  56. func (c *mockContext) GetRootPath() string {
  57. // loc, _ := conf.GetLoc("")
  58. return "root path"
  59. }
  60. func (c *mockContext) SetError(err error) {
  61. }
  62. func (c *mockContext) WithMeta(ruleId string, opId string) api.StreamContext {
  63. return c
  64. }
  65. func (c *mockContext) WithInstance(_ int) api.StreamContext {
  66. return c
  67. }
  68. func (c *mockContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  69. ctx, cancel := context.WithCancel(c.Ctx)
  70. return &mockContext{
  71. RuleId: c.RuleId,
  72. OpId: c.OpId,
  73. Ctx: ctx,
  74. }, cancel
  75. }
  76. func (c *mockContext) IncrCounter(key string, amount int) error {
  77. return nil
  78. }
  79. func (c *mockContext) GetCounter(key string) (int, error) {
  80. return 0, nil
  81. }
  82. func (c *mockContext) PutState(key string, value interface{}) error {
  83. return nil
  84. }
  85. func (c *mockContext) GetState(key string) (interface{}, error) {
  86. return nil, nil
  87. }
  88. func (c *mockContext) DeleteState(key string) error {
  89. return nil
  90. }
  91. func (c *mockContext) Snapshot() error {
  92. return nil
  93. }
  94. func (c *mockContext) SaveState(checkpointId int64) error {
  95. return nil
  96. }
  97. func newMockContext(ruleId string, opId string) api.StreamContext {
  98. return &mockContext{Ctx: context.Background(), RuleId: ruleId, OpId: opId}
  99. }
  100. type mockFuncContext struct {
  101. api.StreamContext
  102. funcId int
  103. }
  104. func (fc *mockFuncContext) GetFuncId() int {
  105. return fc.funcId
  106. }
  107. func newMockFuncContext(ctx api.StreamContext, id int) api.FunctionContext {
  108. return &mockFuncContext{
  109. StreamContext: ctx,
  110. funcId: id,
  111. }
  112. }
  113. var Logger *logrus.Logger
  114. func init() {
  115. l := logrus.New()
  116. filenameHook := filename.NewHook()
  117. filenameHook.Field = "file"
  118. l.AddHook(filenameHook)
  119. l.SetFormatter(&logrus.TextFormatter{
  120. TimestampFormat: "2006-01-02 15:04:05",
  121. DisableColors: true,
  122. FullTimestamp: true,
  123. })
  124. l.WithField("type", "main")
  125. Logger = l
  126. }