error_channel_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "context"
  16. "errors"
  17. "testing"
  18. )
  19. func TestErrorChannel(t *testing.T) {
  20. errCh := NewErrorChannel()
  21. if actualErr := errCh.ReceiveError(); actualErr != nil {
  22. t.Errorf("expect nil from err channel, but got %v", actualErr)
  23. }
  24. err := errors.New("unknown error")
  25. errCh.SendError(err)
  26. if actualErr := errCh.ReceiveError(); actualErr != err {
  27. t.Errorf("expect %v from err channel, but got %v", err, actualErr)
  28. }
  29. ctx, cancel := context.WithCancel(context.Background())
  30. errCh.SendErrorWithCancel(err, cancel)
  31. if actualErr := errCh.ReceiveError(); actualErr != err {
  32. t.Errorf("expect %v from err channel, but got %v", err, actualErr)
  33. }
  34. if ctxErr := ctx.Err(); ctxErr != context.Canceled {
  35. t.Errorf("expect context canceled, but got %v", ctxErr)
  36. }
  37. }