github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/watch/sinks_test.go (about)

     1  package watch
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // TestTimeoutDropErrSinkGen tests the full chain of sinks
    11  func TestTimeoutDropErrSinkGen(t *testing.T) {
    12  	require := require.New(t)
    13  	doneChan := make(chan struct{})
    14  
    15  	sinkGen := NewTimeoutDropErrSinkGen(time.Second)
    16  
    17  	// Generate two channels to perform the following test-cases
    18  	sink, ch := sinkGen.NewChannelSink()
    19  	sink2, ch2 := sinkGen.NewChannelSink()
    20  
    21  	go func() {
    22  		for {
    23  			select {
    24  			case <-ch.C:
    25  			case <-doneChan:
    26  				return
    27  			}
    28  		}
    29  	}()
    30  	require.NoError(sink.Write("some event"))
    31  
    32  	// Make sure the sink times out on the write operation if the channel is
    33  	// not read from.
    34  	err := sink2.Write("some event")
    35  	require.Error(err)
    36  	require.Equal(ErrSinkTimeout, err)
    37  
    38  	// Ensure that hitting a timeout causes the sink to close
    39  	<-ch2.Done()
    40  
    41  	// Make sure that closing a sink closes the channel
    42  	errClose := sink.Close()
    43  	<-ch.Done()
    44  	require.NoError(errClose)
    45  
    46  	// Close the leaking goroutine
    47  	close(doneChan)
    48  }