Skip to content

Commit

Permalink
Add unit tests for controlsvc
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder committed Sep 20, 2023
1 parent eae8dd4 commit a2c5f89
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 128 deletions.
2 changes: 1 addition & 1 deletion pkg/controlsvc/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *connectCommand) ControlFunc(_ context.Context, nc NetceptorForControlCo
if err != nil {
return nil, err
}
err = cfo.BridgeConn("Connecting\n", rc, "connected service", nc.GetLogger())
err = cfo.BridgeConn("Connecting\n", rc, "connected service", nc.GetLogger(), &Util{})

Check failure on line 86 in pkg/controlsvc/connect.go

View workflow job for this annotation

GitHub Actions / coverage

undefined: Util

Check failure on line 86 in pkg/controlsvc/connect.go

View workflow job for this annotation

GitHub Actions / receptor (Go 1.19)

undefined: Util

Check failure on line 86 in pkg/controlsvc/connect.go

View workflow job for this annotation

GitHub Actions / receptor (Go 1.20)

undefined: Util
if err != nil {
return nil, err
}
Expand Down
34 changes: 14 additions & 20 deletions pkg/controlsvc/controlsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,13 @@ func (t *Tls) NewListener(inner net.Listener, config *tls.Config) net.Listener {

// SockControl implements the ControlFuncOperations interface that is passed back to control functions.
type SockControl struct {
conn net.Conn
utils Utiler
io Copier
conn net.Conn
}

func NewSockControl(conn net.Conn, utils Utiler, copier Copier) *SockControl {
// func NewSockControl(conn net.Conn, utils Utiler, copier Copier) *SockControl {

Check failure on line 87 in pkg/controlsvc/controlsvc.go

View workflow job for this annotation

GitHub Actions / lint-receptor

Comment should end in a period (godot)
func NewSockControl(conn net.Conn) *SockControl {
return &SockControl{
conn: conn,
utils: utils,
io: copier,
conn: conn,
}
}

Expand All @@ -110,21 +107,21 @@ func (s *SockControl) WriteMessage(message string) error {
}

// BridgeConn bridges the socket to another socket.
func (s *SockControl) BridgeConn(message string, bc io.ReadWriteCloser, bcName string, logger *logger.ReceptorLogger) error {
func (s *SockControl) BridgeConn(message string, bc io.ReadWriteCloser, bcName string, logger *logger.ReceptorLogger, utils Utiler) error {
if err := s.WriteMessage(message); err != nil {
return err
}
s.utils.BridgeConns(s.conn, "control service", bc, bcName, logger)
utils.BridgeConns(s.conn, "control service", bc, bcName, logger)

return nil
}

// ReadFromConn copies from the socket to an io.Writer, until EOF.
func (s *SockControl) ReadFromConn(message string, out io.Writer) error {
func (s *SockControl) ReadFromConn(message string, out io.Writer, io Copier) error {
if err := s.WriteMessage(message); err != nil {
return err
}
if _, err := s.io.Copy(out, s.conn); err != nil {
if _, err := io.Copy(out, s.conn); err != nil {
return err
}

Expand Down Expand Up @@ -155,10 +152,9 @@ type Server struct {
nc NetceptorForControlsvc
controlFuncLock sync.RWMutex
controlTypes map[string]ControlCommandType
// new stuff
serverUtils Utiler
serverNet Neter
serverTls Tlser
serverUtils Utiler
serverNet Neter
serverTls Tlser

Check warning on line 157 in pkg/controlsvc/controlsvc.go

View workflow job for this annotation

GitHub Actions / lint-receptor

var-naming: struct field serverTls should be serverTLS (revive)
}

// New returns a new instance of a control service.
Expand Down Expand Up @@ -318,7 +314,7 @@ func (s *Server) RunControlSession(conn net.Conn) {
}
s.controlFuncLock.RUnlock()
if ct != nil {
cfo := NewSockControl(conn, &Util{}, &SocketConnIO{})
cfo := NewSockControl(conn)

var cfr map[string]interface{}
var cc ControlCommand
Expand Down Expand Up @@ -469,12 +465,10 @@ func (s *Server) RunControlSvc(ctx context.Context, service string, tlscfg *tls.
}
}()
for _, listener := range []net.Listener{uli, tli, li} {
if reflect.ValueOf(listener).IsNil() {
if listener == nil || reflect.ValueOf(listener).IsNil() {
continue
}
if listener != nil {
go s.ConnectionListener(ctx, listener)
}
go s.ConnectionListener(ctx, listener)
}

return nil
Expand Down
Loading

0 comments on commit a2c5f89

Please sign in to comment.