some more tests added.
This commit is contained in:
parent
45595a34aa
commit
b3f5eead9b
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -67,44 +68,60 @@ func (suite *ChannelReadWriterTestSuite) runAndWait(functions ...TestFunc) []any
|
|||||||
func (suite *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
func (suite *ChannelReadWriterTestSuite) Test_SuccessfulCommunication() {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
data string
|
data []string
|
||||||
chunkSizes []int
|
chunkSizes []int
|
||||||
chunks []string
|
chunks []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "buffer_large_enough",
|
name: "buffer_large_enough",
|
||||||
data: "hello",
|
data: []string{"hello"},
|
||||||
chunkSizes: []int{10},
|
chunkSizes: []int{10},
|
||||||
chunks: []string{"hello"},
|
chunks: []string{"hello"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "two_reads_required",
|
name: "two_reads_required",
|
||||||
data: "hello",
|
data: []string{"hello"},
|
||||||
chunkSizes: []int{3, 10},
|
chunkSizes: []int{3, 10},
|
||||||
chunks: []string{"hel", "lo"},
|
chunks: []string{"hel", "lo"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "many_reads_required",
|
name: "many_reads_required",
|
||||||
data: "hello",
|
data: []string{"hello"},
|
||||||
chunkSizes: []int{1, 1, 1, 1, 1},
|
chunkSizes: []int{1, 1, 1, 1, 1},
|
||||||
chunks: []string{"h", "e", "l", "l", "o"},
|
chunks: []string{"h", "e", "l", "l", "o"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "buffer_large_enough_multiple_writes",
|
||||||
|
data: []string{"hel", "lo"},
|
||||||
|
chunkSizes: []int{3, 2},
|
||||||
|
chunks: []string{"hel", "lo"},
|
||||||
|
},
|
||||||
|
{ // NOTE: no intelligence in the reader to fill up the read buffer when it is not full
|
||||||
|
// therefore, the second read will have only 1 char since the first channel read returned
|
||||||
|
// 3 of which 2 where returned in the first read call to the ChannelReadWriter.
|
||||||
|
name: "buffer_too_small_multiple_writes",
|
||||||
|
data: []string{"hel", "lo"},
|
||||||
|
chunkSizes: []int{2, 2, 2},
|
||||||
|
chunks: []string{"he", "l", "lo"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
suite.Run(test.name, func() {
|
suite.Run(test.name, func() {
|
||||||
suite.runAndWait(
|
suite.runAndWait(
|
||||||
func() any {
|
func() any {
|
||||||
|
for _, d := range test.data {
|
||||||
select {
|
select {
|
||||||
case <-suite.ctx.Done():
|
case <-suite.ctx.Done():
|
||||||
suite.FailNow("deadline reached")
|
suite.FailNow("deadline reached")
|
||||||
log.Println("Write deadline exceeded")
|
log.Println("Write deadline exceeded")
|
||||||
case suite.toChannel <- []byte(test.data):
|
case suite.toChannel <- []byte(d):
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
func() any {
|
func() any {
|
||||||
remainder := test.data
|
remainder := strings.Join(test.data, "")
|
||||||
for i, chunkSize := range test.chunkSizes {
|
for i, chunkSize := range test.chunkSizes {
|
||||||
buf := make([]byte, chunkSize)
|
buf := make([]byte, chunkSize)
|
||||||
n, err := suite.conn.Read(buf)
|
n, err := suite.conn.Read(buf)
|
||||||
|
Loading…
Reference in New Issue
Block a user