channel.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. *
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package testutils
  18. import (
  19. "context"
  20. )
  21. // DefaultChanBufferSize is the default buffer size of the underlying channel.
  22. const DefaultChanBufferSize = 1
  23. // Channel wraps a generic channel and provides a timed receive operation.
  24. type Channel struct {
  25. ch chan interface{}
  26. }
  27. // Send sends value on the underlying channel.
  28. func (c *Channel) Send(value interface{}) {
  29. c.ch <- value
  30. }
  31. // SendContext sends value on the underlying channel, or returns an error if
  32. // the context expires.
  33. func (c *Channel) SendContext(ctx context.Context, value interface{}) error {
  34. select {
  35. case c.ch <- value:
  36. return nil
  37. case <-ctx.Done():
  38. return ctx.Err()
  39. }
  40. }
  41. // SendOrFail attempts to send value on the underlying channel. Returns true
  42. // if successful or false if the channel was full.
  43. func (c *Channel) SendOrFail(value interface{}) bool {
  44. select {
  45. case c.ch <- value:
  46. return true
  47. default:
  48. return false
  49. }
  50. }
  51. // ReceiveOrFail returns the value on the underlying channel and true, or nil
  52. // and false if the channel was empty.
  53. func (c *Channel) ReceiveOrFail() (interface{}, bool) {
  54. select {
  55. case got := <-c.ch:
  56. return got, true
  57. default:
  58. return nil, false
  59. }
  60. }
  61. // Receive returns the value received on the underlying channel, or the error
  62. // returned by ctx if it is closed or cancelled.
  63. func (c *Channel) Receive(ctx context.Context) (interface{}, error) {
  64. select {
  65. case <-ctx.Done():
  66. return nil, ctx.Err()
  67. case got := <-c.ch:
  68. return got, nil
  69. }
  70. }
  71. // Replace clears the value on the underlying channel, and sends the new value.
  72. //
  73. // It's expected to be used with a size-1 channel, to only keep the most
  74. // up-to-date item. This method is inherently racy when invoked concurrently
  75. // from multiple goroutines.
  76. func (c *Channel) Replace(value interface{}) {
  77. for {
  78. select {
  79. case c.ch <- value:
  80. return
  81. case <-c.ch:
  82. }
  83. }
  84. }
  85. // NewChannel returns a new Channel.
  86. func NewChannel() *Channel {
  87. return NewChannelWithSize(DefaultChanBufferSize)
  88. }
  89. // NewChannelWithSize returns a new Channel with a buffer of bufSize.
  90. func NewChannelWithSize(bufSize int) *Channel {
  91. return &Channel{ch: make(chan interface{}, bufSize)}
  92. }