95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package comms
|
|
|
|
import (
|
|
"context"
|
|
"git.wamblee.org/converge/pkg/testsupport"
|
|
"github.com/stretchr/testify/suite"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type AgentServerTestSuite struct {
|
|
suite.Suite
|
|
|
|
ctx context.Context
|
|
cancelFunc context.CancelFunc
|
|
pprofServer *http.Server
|
|
|
|
agentConnection net.Conn
|
|
serverConnection net.Conn
|
|
}
|
|
|
|
func (s *AgentServerTestSuite) SetupSuite() {
|
|
s.pprofServer = testsupport.StartPprof("")
|
|
}
|
|
|
|
func (s *AgentServerTestSuite) TearDownSuite() {
|
|
testsupport.StopPprof(s.ctx, s.pprofServer)
|
|
}
|
|
|
|
func (s *AgentServerTestSuite) SetupTest() {
|
|
ctx, cancelFunc := testsupport.CreateTestContext(context.Background(), 10*time.Second)
|
|
s.ctx = ctx
|
|
s.cancelFunc = cancelFunc
|
|
serverConnection, agentConnection := net.Pipe()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
serverConnection.SetDeadline(deadline)
|
|
agentConnection.SetDeadline(deadline)
|
|
s.serverConnection = serverConnection
|
|
s.agentConnection = agentConnection
|
|
}
|
|
|
|
func (suite *AgentServerTestSuite) TearDownTest() {
|
|
}
|
|
|
|
func TestAgentServerTestSuite(t *testing.T) {
|
|
suite.Run(t, &AgentServerTestSuite{})
|
|
}
|
|
|
|
func (s *AgentServerTestSuite) TestNewCommChannel() {
|
|
|
|
// Setup Comm channel
|
|
commChannels := testsupport.RunAndWait(
|
|
&s.Suite,
|
|
func() any {
|
|
log.Println("Agent initializing")
|
|
commChannel, err := NewCommChannel(Agent, s.agentConnection)
|
|
s.Nil(err)
|
|
return commChannel
|
|
},
|
|
func() any {
|
|
log.Println("Server initializing")
|
|
commChannel, err := NewCommChannel(ConvergeServer, s.serverConnection)
|
|
s.Nil(err)
|
|
return commChannel
|
|
},
|
|
)
|
|
|
|
s.Equal(2, len(commChannels))
|
|
agentCommChannel := commChannels[0].(CommChannel)
|
|
serverCommChannel := commChannels[1].(CommChannel)
|
|
|
|
// verify the side channel is working by sending an object
|
|
testsupport.RunAndWait(
|
|
&s.Suite,
|
|
func() any {
|
|
protocolVersion := ProtocolVersion{Version: 10}
|
|
err := SendWithTimeout[ProtocolVersion](agentCommChannel.SideChannel, protocolVersion)
|
|
s.Nil(err)
|
|
log.Printf("Sent one message %v", protocolVersion)
|
|
return nil
|
|
},
|
|
func() any {
|
|
protocolVersion, err := ReceiveWithTimeout[ProtocolVersion](serverCommChannel.SideChannel)
|
|
s.Nil(err)
|
|
log.Printf("Received one message %v", protocolVersion)
|
|
return nil
|
|
},
|
|
)
|
|
|
|
log.Printf("%v %v", agentCommChannel, serverCommChannel)
|
|
}
|