package iowrappers import ( "context" "github.com/stretchr/testify/suite" "log" "sync" "testing" "time" ) type ChannelReadWriterTestSuite struct { suite.Suite ctx context.Context cancelFunc context.CancelFunc toChannel chan<- []byte fromChannel <-chan []byte conn *ChannelReadWriter } func TestChannelReadWriterSuite(t *testing.T) { suite.Run(t, &ChannelReadWriterTestSuite{}) } func (suite *ChannelReadWriterTestSuite) createChannel() { toChannel := make(chan []byte) fromChannel := make(chan []byte) suite.toChannel = toChannel suite.fromChannel = fromChannel ctx, cancelFunc := context.WithCancel(context.Background()) ctx, timeoutCancelFunc := context.WithTimeout(ctx, 10*time.Second) suite.ctx = ctx suite.cancelFunc = func() { timeoutCancelFunc() cancelFunc() } suite.cancelFunc = cancelFunc suite.conn = NewChannelReadWriter(ctx, toChannel, fromChannel) } func (suite *ChannelReadWriterTestSuite) SetupTest() { suite.createChannel() } func (suite *ChannelReadWriterTestSuite) TearDownTest() { suite.cancelFunc() } type TestFunc func() any func runAndWait(functions ...TestFunc) []any { wg := sync.WaitGroup{} wg.Add(len(functions)) res := make([]any, len(functions)) for i, function := range functions { go func() { res[i] = function() wg.Done() }() } wg.Wait() return res } func (suite *ChannelReadWriterTestSuite) Test_SlicesLargeEnough() { requires := suite.Require() data := []byte("hello") runAndWait( func() any { suite.toChannel <- data log.Println("data sent") return nil }, func() any { buf := make([]byte, len(data)*2) n, err := suite.conn.Read(buf) requires.Nil(err) requires.Equal(n, len(data)) requires.Equal(data, buf[:n]) return nil }, ) } func (suite *ChannelReadWriterTestSuite) Test_SliceTooSmallFullReadInTwoParts() { suite.FailNow("todo") } func (suite *ChannelReadWriterTestSuite) Test_SliceTooSmallFullREadInManyParts() { suite.FailNow("todo") } func (suite *ChannelReadWriterTestSuite) Test_Close() { suite.FailNow("todo") } func (suite *ChannelReadWriterTestSuite) Test_CloseTwice() { suite.FailNow("todo") } func (suite *ChannelReadWriterTestSuite) Test_ContextCanceled() { suite.FailNow("todo") }