From d5c0206b9c1407b44706aa2397e6ce0858586a07 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 20 Aug 2024 11:47:36 +0200 Subject: [PATCH] basic gobchannel tests now done. --- pkg/comms/agentserver_test.go | 18 +++++- pkg/comms/gobchannel_test.go | 94 ++++++++++++++++++++++++++++ pkg/testsupport/channelreadwriter.go | 1 - 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 pkg/comms/gobchannel_test.go diff --git a/pkg/comms/agentserver_test.go b/pkg/comms/agentserver_test.go index b395104..4aee7af 100644 --- a/pkg/comms/agentserver_test.go +++ b/pkg/comms/agentserver_test.go @@ -49,9 +49,7 @@ func TestAgentServerTestSuite(t *testing.T) { suite.Run(t, &AgentServerTestSuite{}) } -func (s *AgentServerTestSuite) TestNewCommChannel() { - - // Setup Comm channel +func (s *AgentServerTestSuite) createCommChannel() (CommChannel, CommChannel) { commChannels := testsupport.RunAndWait( &s.Suite, func() any { @@ -71,6 +69,12 @@ func (s *AgentServerTestSuite) TestNewCommChannel() { s.Equal(2, len(commChannels)) agentCommChannel := commChannels[0].(CommChannel) serverCommChannel := commChannels[1].(CommChannel) + return agentCommChannel, serverCommChannel +} + +func (s *AgentServerTestSuite) TestNewCommChannel() { + // Setup Comm channel + agentCommChannel, serverCommChannel := s.createCommChannel() // verify the side channel is working by sending an object testsupport.RunAndWait( @@ -92,3 +96,11 @@ func (s *AgentServerTestSuite) TestNewCommChannel() { log.Printf("%v %v", agentCommChannel, serverCommChannel) } + +func (s *AgentServerTestSuite) Test_ListenForAgentEvents() { + +} + +func (s *AgentServerTestSuite) Test_LIstenForServerEvents() { + +} diff --git a/pkg/comms/gobchannel_test.go b/pkg/comms/gobchannel_test.go new file mode 100644 index 0000000..d827705 --- /dev/null +++ b/pkg/comms/gobchannel_test.go @@ -0,0 +1,94 @@ +package comms + +import ( + "context" + "git.wamblee.org/converge/pkg/testsupport" + "github.com/stretchr/testify/suite" + "net" + "net/http" + "testing" + "time" +) + +type GOBChannelTestSuite struct { + suite.Suite + + ctx context.Context + cancelFunc context.CancelFunc + pprofServer *http.Server + + agentConnection net.Conn + serverConnection net.Conn + + agentGOB GOBChannel + serverGOB GOBChannel +} + +func (s *GOBChannelTestSuite) SetupSuite() { + s.pprofServer = testsupport.StartPprof("") +} + +func (s *GOBChannelTestSuite) TearDownSuite() { + testsupport.StopPprof(s.ctx, s.pprofServer) +} + +func (s *GOBChannelTestSuite) SetupTest() { + ctx, cancelFunc := testsupport.CreateTestContext(context.Background(), 10*time.Second) + s.ctx = ctx + s.cancelFunc = cancelFunc + agentConnection, serverConnection := net.Pipe() + deadline := time.Now().Add(10 * time.Second) + agentConnection.SetDeadline(deadline) + serverConnection.SetDeadline(deadline) + s.agentConnection = agentConnection + s.serverConnection = serverConnection + s.agentGOB = NewGOBChannel(s.agentConnection) + s.serverGOB = NewGOBChannel(s.serverConnection) +} + +func (s *GOBChannelTestSuite) TearDownTest() { +} + +func TestNewGOBChannel(t *testing.T) { + suite.Run(t, &GOBChannelTestSuite{}) +} + +func (s *GOBChannelTestSuite) Test_SendConcreteObject() { + protocol := ProtocolVersion{ + Version: 10, + } + testsupport.RunAndWait( + &s.Suite, + func() any { + s.Nil(SendWithTimeout[ProtocolVersion](s.agentGOB, protocol)) + return nil + }, + func() any { + protocol2, err := ReceiveWithTimeout[ProtocolVersion](s.serverGOB) + s.Nil(err) + s.Equal(protocol, protocol2) + return nil + }, + ) +} + +func (s *GOBChannelTestSuite) Test_SendInterface() { + + session := NewSessionInfo("abc", "ftp") + testsupport.RunAndWait( + &s.Suite, + func() any { + s.Nil(SendWithTimeout[ConvergeMessage](s.agentGOB, + ConvergeMessage{ + Value: session, + })) + return nil + }, + func() any { + session2, err := ReceiveWithTimeout[ConvergeMessage](s.serverGOB) + s.Nil(err) + s.Equal(session, session2.Value) + return nil + }, + ) +} diff --git a/pkg/testsupport/channelreadwriter.go b/pkg/testsupport/channelreadwriter.go index 51193f9..1ee07f6 100644 --- a/pkg/testsupport/channelreadwriter.go +++ b/pkg/testsupport/channelreadwriter.go @@ -40,7 +40,6 @@ func (rw *ChannelReadWriter) Read(p []byte) (n int, err error) { nread := copy(p, rw.readBuf) if nread > 0 { - log.Printf("Read %v bytes", nread) rw.readBuf = rw.readBuf[nread:] return nread, nil }