mirror of https://github.com/grpc/grpc-go.git
internal: Exposes underlying channel in testutils.Channel{} (#6742)
This commit is contained in:
parent
b046ccaf08
commit
e88e8498c6
|
|
@ -26,19 +26,21 @@ const DefaultChanBufferSize = 1
|
||||||
|
|
||||||
// Channel wraps a generic channel and provides a timed receive operation.
|
// Channel wraps a generic channel and provides a timed receive operation.
|
||||||
type Channel struct {
|
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.
|
// Send sends value on the underlying channel.
|
||||||
func (c *Channel) Send(value any) {
|
func (c *Channel) Send(value any) {
|
||||||
c.ch <- value
|
c.C <- value
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendContext sends value on the underlying channel, or returns an error if
|
// SendContext sends value on the underlying channel, or returns an error if
|
||||||
// the context expires.
|
// the context expires.
|
||||||
func (c *Channel) SendContext(ctx context.Context, value any) error {
|
func (c *Channel) SendContext(ctx context.Context, value any) error {
|
||||||
select {
|
select {
|
||||||
case c.ch <- value:
|
case c.C <- value:
|
||||||
return nil
|
return nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
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.
|
// if successful or false if the channel was full.
|
||||||
func (c *Channel) SendOrFail(value any) bool {
|
func (c *Channel) SendOrFail(value any) bool {
|
||||||
select {
|
select {
|
||||||
case c.ch <- value:
|
case c.C <- value:
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|
@ -60,7 +62,7 @@ func (c *Channel) SendOrFail(value any) bool {
|
||||||
// and false if the channel was empty.
|
// and false if the channel was empty.
|
||||||
func (c *Channel) ReceiveOrFail() (any, bool) {
|
func (c *Channel) ReceiveOrFail() (any, bool) {
|
||||||
select {
|
select {
|
||||||
case got := <-c.ch:
|
case got := <-c.C:
|
||||||
return got, true
|
return got, true
|
||||||
default:
|
default:
|
||||||
return nil, false
|
return nil, false
|
||||||
|
|
@ -73,7 +75,7 @@ func (c *Channel) Receive(ctx context.Context) (any, error) {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case got := <-c.ch:
|
case got := <-c.C:
|
||||||
return got, nil
|
return got, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -86,9 +88,9 @@ func (c *Channel) Receive(ctx context.Context) (any, error) {
|
||||||
func (c *Channel) Replace(value any) {
|
func (c *Channel) Replace(value any) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case c.ch <- value:
|
case c.C <- value:
|
||||||
return
|
return
|
||||||
case <-c.ch:
|
case <-c.C:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -100,5 +102,5 @@ func NewChannel() *Channel {
|
||||||
|
|
||||||
// NewChannelWithSize returns a new Channel with a buffer of bufSize.
|
// NewChannelWithSize returns a new Channel with a buffer of bufSize.
|
||||||
func NewChannelWithSize(bufSize int) *Channel {
|
func NewChannelWithSize(bufSize int) *Channel {
|
||||||
return &Channel{ch: make(chan any, bufSize)}
|
return &Channel{C: make(chan any, bufSize)}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue