dynamic_channel_buffer_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 node
  15. import (
  16. "fmt"
  17. "testing"
  18. "time"
  19. "github.com/benbjohnson/clock"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func TestBuffer(t *testing.T) {
  24. b := NewDynamicChannelBuffer()
  25. b.SetLimit(100)
  26. stopSign := make(chan struct{})
  27. mc := conf.Clock.(*clock.Mock)
  28. go func(done chan struct{}) {
  29. for i := 0; i < 100; i++ {
  30. select {
  31. case b.In <- api.NewDefaultSourceTupleWithTime(map[string]interface{}{"a": 5}, nil, mc.Now()):
  32. fmt.Printf("feed in %d\n", i)
  33. default:
  34. t.Errorf("message %d dropped, should not drop message", i)
  35. }
  36. }
  37. close(done)
  38. }(stopSign)
  39. for i := 0; i < 50; i++ {
  40. _ = <-b.Out
  41. fmt.Printf("eaten %d\n", i)
  42. time.Sleep(10 * time.Millisecond)
  43. }
  44. <-stopSign
  45. l := b.GetLength()
  46. if l != 50 {
  47. t.Errorf("Expect buffer length 50, but got %d", l)
  48. }
  49. }