dynamic_channel_buffer_test.go 1.3 KB

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