From 2a663c19d2928d857b2501283306a3e696de354d Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Wed, 21 Aug 2024 16:58:15 +0200 Subject: [PATCH] added a few tests for inmemoryconnection. --- pkg/comms/gobchannel_test.go | 2 +- pkg/support/collections/linkedmap_test.go | 2 +- pkg/testsupport/inmemoryconnection_test.go | 39 ++++++++++++++++++++++ teststats.sh | 8 +++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100755 teststats.sh diff --git a/pkg/comms/gobchannel_test.go b/pkg/comms/gobchannel_test.go index d827705..4f8cb72 100644 --- a/pkg/comms/gobchannel_test.go +++ b/pkg/comms/gobchannel_test.go @@ -49,7 +49,7 @@ func (s *GOBChannelTestSuite) SetupTest() { func (s *GOBChannelTestSuite) TearDownTest() { } -func TestNewGOBChannel(t *testing.T) { +func TestNewGOBChannelSuite(t *testing.T) { suite.Run(t, &GOBChannelTestSuite{}) } diff --git a/pkg/support/collections/linkedmap_test.go b/pkg/support/collections/linkedmap_test.go index 308ac4f..a55e740 100644 --- a/pkg/support/collections/linkedmap_test.go +++ b/pkg/support/collections/linkedmap_test.go @@ -23,7 +23,7 @@ func (s *LinkedMapTestSuite) SetupTest() { func (s *LinkedMapTestSuite) TearDownTest() { } -func TestLinkedMap(t *testing.T) { +func TestLinkedMapSuite(t *testing.T) { suite.Run(t, &LinkedMapTestSuite{}) } diff --git a/pkg/testsupport/inmemoryconnection_test.go b/pkg/testsupport/inmemoryconnection_test.go index 0eed707..40c3991 100644 --- a/pkg/testsupport/inmemoryconnection_test.go +++ b/pkg/testsupport/inmemoryconnection_test.go @@ -3,6 +3,7 @@ package testsupport import ( "context" "github.com/stretchr/testify/suite" + "io" "net/http" "testing" "time" @@ -43,3 +44,41 @@ func (s *InMemoryTestSuite) SetupTest() { func (s *InMemoryTestSuite) TearDownTest() { s.cancelFunc() } + +// All test cases can be performed with Front() and Back reversed since they are just two sides of the same Pipe. + +func (s *InMemoryTestSuite) SendReceive(a io.ReadWriteCloser, b io.ReadWriteCloser) { + data := "hello" + n, err := a.Write([]byte(data)) + s.Nil(err) + s.Equal(len(data), n) + + buf := make([]byte, len(data)*2) + n, err = b.Read(buf) + s.Nil(err) + s.Equal([]byte(data), buf[:n]) +} + +func (s *InMemoryTestSuite) Test_SendReceive1() { + s.SendReceive(s.pipe.Front(), s.pipe.Back()) +} + +func (s *InMemoryTestSuite) Test_SendReceive2() { + s.SendReceive(s.pipe.Back(), s.pipe.Front()) +} + +func (s *InMemoryTestSuite) closeLeadsToEofOnRead(a io.ReadWriteCloser, b io.ReadWriteCloser) { + a.Close() + buf := make([]byte, 100) + n, err := b.Read(buf) + s.Equal(io.EOF, err) + s.Equal(0, n) +} + +func (s *InMemoryTestSuite) Test_CloseLeadsToEofOnRead1() { + s.closeLeadsToEofOnRead(s.pipe.Front(), s.pipe.Back()) +} + +func (s *InMemoryTestSuite) Test_CloseLeadsToEofOnRead2() { + s.closeLeadsToEofOnRead(s.pipe.Back(), s.pipe.Front()) +} diff --git a/teststats.sh b/teststats.sh new file mode 100755 index 0000000..6126c42 --- /dev/null +++ b/teststats.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +testout="$( make test )" + + +echo "Total: $( echo "$testout" | grep -v 'Suite[^/]' | grep -cE '^ *--- (PASS|FAIL)' )" +echo "Pass: $( echo "$testout" | grep -v 'Suite[^/]' | grep -cE '^ *--- PASS' )" +echo "Fail: $( echo "$testout" | grep -v 'Suite[^/]' | grep -cE '^ *--- FAIL' )"