Skip to content

Commit

Permalink
internal: Exposes underlying channel in testutils.Channel{} (grpc#6742)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-Sood authored Oct 19, 2023
1 parent b046cca commit e88e849
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/testutils/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ const DefaultChanBufferSize = 1

// Channel wraps a generic channel and provides a timed receive operation.
type Channel struct {
ch chan any
// C is the underlying channel on which values sent using the SendXxx() methods are delivered.
// Tests which cannot use ReceiveXxx() for whatever reasons can use C to read the values.
C chan any
}

// Send sends value on the underlying channel.
func (c *Channel) Send(value any) {
c.ch <- value
c.C <- value
}

// SendContext sends value on the underlying channel, or returns an error if
// the context expires.
func (c *Channel) SendContext(ctx context.Context, value any) error {
select {
case c.ch <- value:
case c.C <- value:
return nil
case <-ctx.Done():
return ctx.Err()
Expand All @@ -49,7 +51,7 @@ func (c *Channel) SendContext(ctx context.Context, value any) error {
// if successful or false if the channel was full.
func (c *Channel) SendOrFail(value any) bool {
select {
case c.ch <- value:
case c.C <- value:
return true
default:
return false
Expand All @@ -60,7 +62,7 @@ func (c *Channel) SendOrFail(value any) bool {
// and false if the channel was empty.
func (c *Channel) ReceiveOrFail() (any, bool) {
select {
case got := <-c.ch:
case got := <-c.C:
return got, true
default:
return nil, false
Expand All @@ -73,7 +75,7 @@ func (c *Channel) Receive(ctx context.Context) (any, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case got := <-c.ch:
case got := <-c.C:
return got, nil
}
}
Expand All @@ -86,9 +88,9 @@ func (c *Channel) Receive(ctx context.Context) (any, error) {
func (c *Channel) Replace(value any) {
for {
select {
case c.ch <- value:
case c.C <- value:
return
case <-c.ch:
case <-c.C:
}
}
}
Expand All @@ -100,5 +102,5 @@ func NewChannel() *Channel {

// NewChannelWithSize returns a new Channel with a buffer of bufSize.
func NewChannelWithSize(bufSize int) *Channel {
return &Channel{ch: make(chan any, bufSize)}
return &Channel{C: make(chan any, bufSize)}
}

0 comments on commit e88e849

Please sign in to comment.