internal: Exposes underlying channel in testutils.Channel{} (#6742)

This commit is contained in:
Aditya Sood 2023-10-19 23:19:47 +05:30 committed by GitHub
parent b046ccaf08
commit e88e8498c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 9 deletions

View File

@ -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()
@ -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
@ -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
@ -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
}
}
@ -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:
}
}
}
@ -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)}
}