Skip to content

Commit

Permalink
Add unit tests for framer.go (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
resoluteCoder authored Aug 21, 2023
1 parent da5339d commit e62e746
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ kubectl
.vagrant/
/docs/build
/dist
coverage.txt
86 changes: 82 additions & 4 deletions pkg/framer/framer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,88 @@ func TestSendData(t *testing.T) {
}

for _, testCase := range framedBufferTestCases {
result := f.SendData(testCase.inputBuffer)
t.Run(testCase.name, func(t *testing.T) {
result := f.SendData(testCase.inputBuffer)

if !bytes.Equal(testCase.expectedBuffer, result) {
t.Errorf("%s - expected: %+v, received: %+v", testCase.name, testCase.expectedBuffer, result)
}
if !bytes.Equal(testCase.expectedBuffer, result) {
t.Errorf("%s - expected: %+v, received: %+v", testCase.name, testCase.expectedBuffer, result)
}
})
}
}

func TestMessageReady(t *testing.T) {
buffer := []byte{1, 2, 3}

messageReadyTestCases := []struct {
name string
inputBuffer []byte
expectedResult bool
}{
{
name: "message ready",
inputBuffer: append([]byte{3, 0}, buffer...),
expectedResult: true,
},
{
name: "message not ready",
inputBuffer: buffer,
expectedResult: false,
},
}

for _, testCase := range messageReadyTestCases {
t.Run(testCase.name, func(t *testing.T) {
f := framer.New()
f.RecvData(testCase.inputBuffer)
receivedResult := f.MessageReady()

if testCase.expectedResult != receivedResult {
t.Errorf("%s - expected: %+v, received: %+v", testCase.name, testCase.expectedResult, receivedResult)
}
})
}
}

func TestGetMessage(t *testing.T) {
buffer := []byte{1, 2, 3}
framedBuffer := append([]byte{3, 0}, buffer...)

getMessageTestCases := []struct {
name string
inputBuffer []byte
expectedError bool
}{
{
name: "message read",
inputBuffer: framedBuffer,
expectedError: false,
},
{
name: "message not read with sending non framed buffer",
inputBuffer: buffer,
expectedError: true,
},
{
name: "message not read with sending insufficient buffer length",
inputBuffer: []byte{1},
expectedError: true,
},
}

for _, testCase := range getMessageTestCases {
t.Run(testCase.name, func(t *testing.T) {
f := framer.New()
f.RecvData(testCase.inputBuffer)

receivedResult, err := f.GetMessage()

if testCase.expectedError && err == nil {
t.Error(testCase.name)
}
if !testCase.expectedError && !bytes.Equal(buffer, receivedResult) {
t.Errorf("expected: %+v, received: %+v", buffer, receivedResult)
}
})
}
}

0 comments on commit e62e746

Please sign in to comment.