basic gobchannel tests now done.

This commit is contained in:
Erik Brakkee 2024-08-20 11:47:36 +02:00
parent 2e8107ddbd
commit bf837d31b2
3 changed files with 109 additions and 4 deletions

View File

@ -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() {
}

View File

@ -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
},
)
}

View File

@ -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
}