Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Staykov committed Oct 29, 2024
1 parent fe6734b commit 6f14463
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions zk/datastream/client/stream_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"net"
"sync"
Expand All @@ -27,7 +26,7 @@ func TestStreamClientReadHeaderEntry(t *testing.T) {
name string
input []byte
expectedResult *types.HeaderEntry
expectedError error
expectedError string
}
testCases := []testCase{
{
Expand All @@ -40,13 +39,13 @@ func TestStreamClientReadHeaderEntry(t *testing.T) {
TotalLength: 24,
TotalEntries: 64,
},
expectedError: nil,
expectedError: "",
},
{
name: "Invalid byte array length",
input: []byte{20, 21, 22, 23, 24, 20},
expectedResult: nil,
expectedError: errors.New("failed to read header bytes reading from server: unexpected EOF"),
expectedError: "read header bytes reading from server: unexpected EOF",
},
}

Expand All @@ -64,7 +63,11 @@ func TestStreamClientReadHeaderEntry(t *testing.T) {
}()

header, err := c.readHeaderEntry()
require.Equal(t, testCase.expectedError, err)
if testCase.expectedError != "" {
require.EqualError(t, err, testCase.expectedError)
} else {
require.NoError(t, err)
}
assert.DeepEqual(t, testCase.expectedResult, header)
})
}
Expand All @@ -75,7 +78,7 @@ func TestStreamClientReadResultEntry(t *testing.T) {
name string
input []byte
expectedResult *types.ResultEntry
expectedError error
expectedError string
}
testCases := []testCase{
{
Expand All @@ -87,7 +90,7 @@ func TestStreamClientReadResultEntry(t *testing.T) {
ErrorNum: 0,
ErrorStr: []byte{},
},
expectedError: nil,
expectedError: "",
},
{
name: "Happy path - error str length",
Expand All @@ -98,19 +101,19 @@ func TestStreamClientReadResultEntry(t *testing.T) {
ErrorNum: 0,
ErrorStr: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
},
expectedError: nil,
expectedError: "",
},
{
name: "Invalid byte array length",
input: []byte{20, 21, 22, 23, 24, 20},
expectedResult: nil,
expectedError: errors.New("failed to read main result bytes reading from server: unexpected EOF"),
expectedError: "read main result bytes reading from server: unexpected EOF",
},
{
name: "Invalid error length",
input: []byte{0, 0, 0, 12, 0, 0, 0, 0, 20, 21},
expectedResult: nil,
expectedError: errors.New("failed to read result errStr bytes reading from server: unexpected EOF"),
expectedError: "read result errStr bytes reading from server: unexpected EOF",
},
}

Expand All @@ -128,7 +131,11 @@ func TestStreamClientReadResultEntry(t *testing.T) {
}()

result, err := c.readResultEntry([]byte{1})
require.Equal(t, testCase.expectedError, err)
if testCase.expectedError != "" {
require.EqualError(t, err, testCase.expectedError)
} else {
require.NoError(t, err)
}
assert.DeepEqual(t, testCase.expectedResult, result)
})
}
Expand All @@ -139,7 +146,7 @@ func TestStreamClientReadFileEntry(t *testing.T) {
name string
input []byte
expectedResult *types.FileEntry
expectedError error
expectedError string
}
testCases := []testCase{
{
Expand All @@ -152,7 +159,7 @@ func TestStreamClientReadFileEntry(t *testing.T) {
EntryNum: 45,
Data: []byte{0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 64},
},
expectedError: nil,
expectedError: "",
}, {
name: "Happy path - no data",
input: []byte{2, 0, 0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 45},
Expand All @@ -163,24 +170,24 @@ func TestStreamClientReadFileEntry(t *testing.T) {
EntryNum: 45,
Data: []byte{},
},
expectedError: nil,
expectedError: "",
},
{
name: "Invalid packet type",
input: []byte{5, 0, 0, 0, 17, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 45},
expectedResult: nil,
expectedError: errors.New("expected data packet type 2 or 254 and received 5"),
expectedError: "expected data packet type 2 or 254 and received 5",
},
{
name: "Invalid byte array length",
input: []byte{2, 21, 22, 23, 24, 20},
expectedResult: nil,
expectedError: errors.New("error reading file bytes: reading from server: unexpected EOF"),
expectedError: "reading file bytes: reading from server: unexpected EOF",
}, {
name: "Invalid data length",
input: []byte{2, 0, 0, 0, 31, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 64},
expectedResult: nil,
expectedError: errors.New("error reading file data bytes: reading from server: unexpected EOF"),
expectedError: "reading file data bytes: reading from server: unexpected EOF",
},
}
for _, testCase := range testCases {
Expand All @@ -197,7 +204,11 @@ func TestStreamClientReadFileEntry(t *testing.T) {
}()

result, err := c.NextFileEntry()
require.Equal(t, testCase.expectedError, err)
if testCase.expectedError != "" {
require.EqualError(t, err, testCase.expectedError)
} else {
require.NoError(t, err)
}
assert.DeepEqual(t, testCase.expectedResult, result)
})
}
Expand Down

0 comments on commit 6f14463

Please sign in to comment.