You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
945 B

  1. package p2p
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. gogotypes "github.com/gogo/protobuf/types"
  7. "github.com/tendermint/tendermint/libs/log"
  8. )
  9. type testMessage = gogotypes.StringValue
  10. func TestCloseWhileDequeueFull(t *testing.T) {
  11. enqueueLength := 5
  12. chDescs := []*ChannelDescriptor{
  13. {ID: 0x01, Priority: 1},
  14. }
  15. pqueue := newPQScheduler(log.NewNopLogger(), NopMetrics(), chDescs, uint(enqueueLength), 1, 120)
  16. for i := 0; i < enqueueLength; i++ {
  17. pqueue.enqueue() <- Envelope{
  18. ChannelID: 0x01,
  19. Message: &testMessage{Value: "foo"}, // 5 bytes
  20. }
  21. }
  22. ctx, cancel := context.WithCancel(context.Background())
  23. defer cancel()
  24. go pqueue.process(ctx)
  25. // sleep to allow context switch for process() to run
  26. time.Sleep(10 * time.Millisecond)
  27. doneCh := make(chan struct{})
  28. go func() {
  29. pqueue.close()
  30. close(doneCh)
  31. }()
  32. select {
  33. case <-doneCh:
  34. case <-time.After(2 * time.Second):
  35. t.Fatal("pqueue failed to close")
  36. }
  37. }