Skip to content

Commit

Permalink
destroyed some smelly stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder committed Sep 19, 2023
1 parent 81dcf13 commit eae8dd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
26 changes: 13 additions & 13 deletions pkg/controlsvc/controlsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"github.com/ghjm/cmdline"
)

const (
normalCloseError = "normal close"
writeControlServiceError = "Write error in control service"
)

type Copier interface {
Copy(dst io.Writer, src io.Reader) (written int64, err error)
}
Expand Down Expand Up @@ -209,7 +214,7 @@ func errorNormal(nc NetceptorForControlsvc, logMessage string, err error) bool {
if err == nil {
return false
}
if !strings.HasSuffix(err.Error(), "normal close") {
if !strings.HasSuffix(err.Error(), normalCloseError) {
nc.GetLogger().Error("%s: %s\n", logMessage, err)
}
return true

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

View workflow job for this annotation

GitHub Actions / lint-receptor

return with no blank line before (nlreturn)
Expand Down Expand Up @@ -253,7 +258,7 @@ func (s *Server) RunControlSession(conn net.Conn) {

break
} else if err != nil {
if !strings.HasSuffix(err.Error(), "normal close") {
if !strings.HasSuffix(err.Error(), normalCloseError) {
s.nc.GetLogger().Warning("Could not read in control service: %s\n", err)
}

Expand Down Expand Up @@ -289,8 +294,7 @@ func (s *Server) RunControlSession(conn net.Conn) {
}
if err != nil {
writeMsg := fmt.Sprintf("ERROR: %s", err)
logMsg := "Write error in control service"
if writeToConnWithLog(conn, s.nc, writeMsg, logMsg) {
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
}
Expand Down Expand Up @@ -333,30 +337,26 @@ func (s *Server) RunControlSession(conn net.Conn) {
errorNormal(s.nc, "", err)

writeMsg := fmt.Sprintf("ERROR: %s", err)
logMsg := "Write error in control service"
if writeToConnWithLog(conn, s.nc, writeMsg, logMsg) {
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
} else if cfr != nil {
rbytes, err := json.Marshal(cfr)
if err != nil {
writeMsg := fmt.Sprintf("ERROR: could not convert response to JSON: %s", err)
logMsg := "Write error in control service"
if writeToConnWithLog(conn, s.nc, writeMsg, logMsg) {
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
}
rbytes = append(rbytes, '\n')
writeMsg := string(rbytes)
logMsg := "Write error in control service"
if writeToConnWithLog(conn, s.nc, writeMsg, logMsg) {
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
}
} else {
writeMsg := "ERROR: Unknown command"
logMsg := "Write error in control service"
if writeToConnWithLog(conn, s.nc, writeMsg, logMsg) {
if writeToConnWithLog(conn, s.nc, writeMsg, writeControlServiceError) {
return
}
}
Expand All @@ -370,7 +370,7 @@ func (s *Server) ConnectionListener(ctx context.Context, listener net.Listener)
}
conn, err := listener.Accept()
if err != nil {
if !strings.HasSuffix(err.Error(), "normal close") {
if !strings.HasSuffix(err.Error(), normalCloseError) {
s.nc.GetLogger().Error("Error accepting connection: %s\n", err)
}

Expand Down
48 changes: 29 additions & 19 deletions pkg/controlsvc/controlsvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import (
"github.com/golang/mock/gomock"
)

const (
writeToConnError = "write to conn write message err"
)

func printExpectedError(t *testing.T, err error) {
t.Errorf("expected error %s", err)
}

func TestConnectionListener(t *testing.T) {
ctrl := gomock.NewController(t)
mockNetceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl)
Expand All @@ -29,7 +37,6 @@ func TestConnectionListener(t *testing.T) {
{
name: "return from context error",
expectedError: true,
expectedCalls: func(ctx context.CancelFunc) {},
},
{
name: "error accepting connection",
Expand All @@ -49,7 +56,9 @@ func TestConnectionListener(t *testing.T) {
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()

testCase.expectedCalls(ctxCancel)
if testCase.expectedCalls != nil {
testCase.expectedCalls(ctxCancel)
}
s := controlsvc.New(false, mockNetceptor)

if testCase.expectedError {
Expand Down Expand Up @@ -154,8 +163,6 @@ func TestRunControlSvc(t *testing.T) {
{
name: "no listeners error",
expectedError: "no listeners specified",
expectedCalls: func() {
},
listeners: map[string]string{
"service": "",
"unixSocket": "",
Expand All @@ -166,7 +173,9 @@ func TestRunControlSvc(t *testing.T) {

for _, testCase := range runControlSvcTestCases {
t.Run(testCase.name, func(t *testing.T) {
testCase.expectedCalls()
if testCase.expectedCalls != nil {
testCase.expectedCalls()
}
s := controlsvc.New(false, mockNetceptor)
s.SetServerUtils(mockUnix)
s.SetServerNet(mockNet)
Expand Down Expand Up @@ -214,9 +223,8 @@ func TestSockControlWriteMessage(t *testing.T) {
expectedCalls func()
}{
{
name: "without message",
message: "",
expectedCalls: func() {},
name: "without message",
message: "",
},
{
name: "with message",
Expand All @@ -229,7 +237,9 @@ func TestSockControlWriteMessage(t *testing.T) {

for _, testCase := range writeMessageTestCases {
t.Run(testCase.name, func(t *testing.T) {
testCase.expectedCalls()
if testCase.expectedCalls != nil {
testCase.expectedCalls()
}
err := sockControl.WriteMessage(testCase.message)

if testCase.message == "" && err != nil {
Expand Down Expand Up @@ -338,11 +348,11 @@ func TestSockControlReadFromConn(t *testing.T) {

if testCase.expectedError {
if err == nil && err.Error() != testCase.errorMessage {
t.Errorf("expected error: %s", err)
printExpectedError(t, err)
}
} else {
if err != nil {
t.Errorf("expected error %s", err)
printExpectedError(t, err)
}
}
})
Expand Down Expand Up @@ -378,10 +388,10 @@ func TestSockControlWriteToConn(t *testing.T) {
name: "with message and with error",
message: "message",
expectedCalls: func() {
mockCon.EXPECT().Write(gomock.Any()).Return(0, errors.New("write to conn write message error"))
mockCon.EXPECT().Write(gomock.Any()).Return(0, errors.New(writeToConnError))
},
expectedError: true,
errorMessage: "write to conn write message error",
errorMessage: writeToConnError,
},
{
name: "without message and error",
Expand All @@ -390,7 +400,7 @@ func TestSockControlWriteToConn(t *testing.T) {
mockCon.EXPECT().Write(gomock.Any()).Return(0, nil)
},
expectedError: false,
errorMessage: "write to conn write message error",
errorMessage: writeToConnError,
},
}

Expand All @@ -407,11 +417,11 @@ func TestSockControlWriteToConn(t *testing.T) {

if testCase.expectedError {
if err == nil && err.Error() != testCase.errorMessage {
t.Errorf("expected error: %s", err)
printExpectedError(t, err)
}
} else {
if err != nil {
t.Errorf("expected error %s", err)
printExpectedError(t, err)
}
}
})
Expand All @@ -432,14 +442,14 @@ func TestSockControlClose(t *testing.T) {

err := sockControl.Close()
if err == nil && err.Error() != errorMessage {
t.Errorf("expected error: %s", errorMessage)
printExpectedError(t, err)
}
}

func TestAddControlFunc(t *testing.T) {
ctrl := gomock.NewController(t)
mockCtrlCmd := mock_controlsvc.NewMockControlCommandType(ctrl)
mock_netceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl)
mockNetceptor := mock_controlsvc.NewMockNetceptorForControlsvc(ctrl)
controlFuncTestsCases := []struct {
name string
input string
Expand Down Expand Up @@ -472,7 +482,7 @@ func TestAddControlFunc(t *testing.T) {

for _, testCase := range controlFuncTestsCases {
t.Run(testCase.name, func(t *testing.T) {
s := controlsvc.New(true, mock_netceptor)
s := controlsvc.New(true, mockNetceptor)
err := s.AddControlFunc(testCase.input, mockCtrlCmd)
testCase.testCase(testCase.errorMessage, err)
})
Expand Down

0 comments on commit eae8dd4

Please sign in to comment.