Skip to content

Commit

Permalink
Sharding - Additional tests
Browse files Browse the repository at this point in the history
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan committed Oct 28, 2022
1 parent 6f7e892 commit 14b043c
Show file tree
Hide file tree
Showing 2 changed files with 311 additions and 25 deletions.
169 changes: 169 additions & 0 deletions pkg/sharding/ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,172 @@ func TestPublicKey(t *testing.T) {
})
}
}

func TestLogRanges_String(t *testing.T) {
type fields struct {
inactive Ranges
active int64
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "empty",
fields: fields{
inactive: Ranges{},
active: 0,
},
want: "active=0",
},
{
name: "one",
fields: fields{
inactive: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
},
active: 3,
},
want: "1=2,active=3",
},
{
name: "two",
fields: fields{
inactive: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
{
TreeID: 2,
TreeLength: 3,
},
},
active: 4,
},
want: "1=2,2=3,active=4",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LogRanges{
inactive: tt.fields.inactive,
active: tt.fields.active,
}
if got := l.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestLogRanges_TotalInactiveLength(t *testing.T) {
type fields struct {
inactive Ranges
active int64
}
tests := []struct {
name string
fields fields
want int64
}{
{
name: "empty",
fields: fields{
inactive: Ranges{},
active: 0,
},
want: 0,
},
{
name: "one",
fields: fields{
inactive: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
},
active: 3,
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LogRanges{
inactive: tt.fields.inactive,
active: tt.fields.active,
}
if got := l.TotalInactiveLength(); got != tt.want {
t.Errorf("TotalInactiveLength() = %v, want %v", got, tt.want)
}
})
}
}

func TestLogRanges_AllShards(t *testing.T) {
type fields struct {
inactive Ranges
active int64
}
tests := []struct {
name string
fields fields
want []int64
}{
{
name: "empty",
fields: fields{
inactive: Ranges{},
active: 0,
},
want: []int64{0},
},
{
name: "one",
fields: fields{
inactive: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
},
active: 3,
},
want: []int64{3, 1},
},
{
name: "two",
fields: fields{
inactive: Ranges{
{
TreeID: 1,
TreeLength: 2,
},
{
TreeID: 2,
TreeLength: 3,
},
},
active: 4,
},
want: []int64{4, 1, 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LogRanges{
inactive: tt.fields.inactive,
active: tt.fields.active,
}
if got := l.AllShards(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AllShards() = %v, want %v", got, tt.want)
}
})
}
}
167 changes: 142 additions & 25 deletions pkg/sharding/sharding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ import "testing"
// Create some test data
// Good data
const validTreeID1 = "0FFFFFFFFFFFFFFF"
const validTreeID2 = "3315648d077a9f02"
const validTreeID3 = "7241b7903737211c"
const shortTreeID = "12345"

const (
validTreeID2 = "3315648d077a9f02"
validTreeID3 = "7241b7903737211c"
shortTreeID = "12345"
)

const validUUID = "f794467401d57241b7903737211c721cb3315648d077a9f02ceefb6e404a05de"

const validEntryID1 = validTreeID1 + validUUID
const validEntryID2 = validTreeID2 + validUUID
const validEntryID3 = validTreeID3 + validUUID
const (
validEntryID1 = validTreeID1 + validUUID
validEntryID2 = validTreeID2 + validUUID
validEntryID3 = validTreeID3 + validUUID
)

var validTreeIDs = []string{validTreeID1, validTreeID2, validTreeID3, shortTreeID}
var validEntryIDs = []string{validEntryID1, validEntryID2, validEntryID3}
var (
validTreeIDs = []string{validTreeID1, validTreeID2, validTreeID3, shortTreeID}
validEntryIDs = []string{validEntryID1, validEntryID2, validEntryID3}
)

// Bad data
// Unknown TreeID
Expand All @@ -49,26 +56,37 @@ const tooLongEntryID = validEntryID1 + "e"

var tooShortEntryID = validEntryID1[:len(validEntryID1)-1]

var wrongLengthTreeIDs = []string{tooLongTreeID, validEntryID3, validUUID}
var wrongLengthUUIDs = []string{tooShortUUID, tooLongUUID, validEntryID3, validTreeID1}
var wrongLengthEntryandUUIDs = []string{tooLongEntryID, tooShortEntryID, tooLongUUID, tooShortUUID, validTreeID3}
var (
wrongLengthTreeIDs = []string{tooLongTreeID, validEntryID3, validUUID}
wrongLengthUUIDs = []string{tooShortUUID, tooLongUUID, validEntryID3, validTreeID1}
wrongLengthEntryandUUIDs = []string{tooLongEntryID, tooShortEntryID, tooLongUUID, tooShortUUID, validTreeID3}
)

// Not valid hex
const notHexTreeID1 = "ZZZZZZZZZZZZZZZZ"
const notHexTreeID2 = "FFFFFFF_FFFFFFFF"
const notHexTreeID3 = "xxFFFFFFFFFFFFFF"

const notHexUUID1 = "94467401d57241b7903737211c721cb3315648d077a9f02ceefb6e404a05dezq"
const notHexUUID2 = "y794467401d57241b7903737211c721cb3315648d077a9f02ceefb6e404a05de"
const notHexUUID3 = "f794467401d57241b7903737211c721cbp3315648d077a9f02ceefb6e404a05d"

const notHexEntryID1 = notHexTreeID1 + validUUID
const notHexEntryID2 = validTreeID2 + notHexUUID1
const notHexEntryID3 = notHexTreeID2 + notHexUUID3

var notHexTreeIDs = []string{notHexTreeID1, notHexTreeID2, notHexTreeID3}
var notHexUUIDs = []string{notHexUUID1, notHexUUID2, notHexUUID3}
var notHexEntryandUUIDs = []string{notHexEntryID1, notHexEntryID2, notHexEntryID3, notHexUUID1, notHexUUID2, notHexUUID3}
const (
notHexTreeID2 = "FFFFFFF_FFFFFFFF"
notHexTreeID3 = "xxFFFFFFFFFFFFFF"
)

const (
notHexUUID1 = "94467401d57241b7903737211c721cb3315648d077a9f02ceefb6e404a05dezq"
notHexUUID2 = "y794467401d57241b7903737211c721cb3315648d077a9f02ceefb6e404a05de"
notHexUUID3 = "f794467401d57241b7903737211c721cbp3315648d077a9f02ceefb6e404a05d"
)

const (
notHexEntryID1 = notHexTreeID1 + validUUID
notHexEntryID2 = validTreeID2 + notHexUUID1
notHexEntryID3 = notHexTreeID2 + notHexUUID3
)

var (
notHexTreeIDs = []string{notHexTreeID1, notHexTreeID2, notHexTreeID3}
notHexUUIDs = []string{notHexUUID1, notHexUUID2, notHexUUID3}
notHexEntryandUUIDs = []string{notHexEntryID1, notHexEntryID2, notHexEntryID3, notHexUUID1, notHexUUID2, notHexUUID3}
)

// Test functions
func TestCreateEntryIDFromParts(t *testing.T) {
Expand Down Expand Up @@ -110,7 +128,6 @@ func TestCreateEntryIDFromParts(t *testing.T) {
t.Errorf("created entryID with incorrect UUID: expected %v, got %v", validUUID, entryID.UUID)
}
}

}

func TestCreateEmptyEntryID(t *testing.T) {
Expand Down Expand Up @@ -219,4 +236,104 @@ func TestGetTreeIDFromIDString(t *testing.T) {
if e == nil {
t.Errorf("expected error for invalid UUID, but got none")
}
// uuid length error
uuidHexStringLen := "qlALqZuNP9Iqpg2WVAOkJUntCXQtOOQpOfqox5JbJK4jw5xBs53Wqu1WQ5vTfvqr"
_, e = GetTreeIDFromIDString(uuidHexStringLen)
if e == nil {
t.Errorf("expected error for invalid TreeID, but got none")
}
invalidStringLength := "FWQfOtwd7I4BcCZ5OU7Hbmmp"
_, e = GetTreeIDFromIDString(invalidStringLength)
if e == nil {
t.Errorf("expected error for invalid TreeID, but got none")
}
}

func TestTreeID(t *testing.T) {
type args struct {
entryID string
}
tests := []struct {
name string
args args
want int64
wantErr bool
}{
{
name: "valid entryID",
args: args{
entryID: validEntryID1,
},
want: 1152921504606846975,
wantErr: false,
},
{
name: "invalid entryID",
args: args{
entryID: invalidEntryID,
},
want: 0,
wantErr: true,
},
{
name: "invalid UUID",
args: args{
entryID: notHexEntryID2,
},
want: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TreeID(tt.args.entryID)
if (err != nil) != tt.wantErr {
t.Errorf("TreeID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("TreeID() got = %v, want %v", got, tt.want)
}
})
}
}

func TestValidateTreeID(t *testing.T) {
type args struct {
t string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid treeID",
args: args{
t: validTreeID1,
},
wantErr: false,
},
{
name: "invalid treeID",
args: args{
t: invalidTreeID,
},
wantErr: true,
},
{
name: "invalid UUID",
args: args{
t: notHexTreeID2,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ValidateTreeID(tt.args.t); (err != nil) != tt.wantErr {
t.Errorf("ValidateTreeID() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 14b043c

Please sign in to comment.