Skip to content

Unit Testing core service BlockPoolService #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 13, 2020
177 changes: 177 additions & 0 deletions core/service/blockPoolService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package service

import (
"reflect"
"sync"
"testing"

"github.com/zoobc/zoobc-core/common/model"
)

func TestNewBlockPoolService(t *testing.T) {
tests := []struct {
name string
want *BlockPoolService
}{
// TODO: Add test cases.
{
name: "NewBlockPoolService:success",
want: &BlockPoolService{
BlockQueue: make(map[int64]*model.Block),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewBlockPoolService(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewBlockPoolService() = %v, want %v", got, tt.want)
}
})
}
}

func TestBlockPoolService_GetBlocks(t *testing.T) {
type fields struct {
BlockQueueLock sync.RWMutex
BlockQueue map[int64]*model.Block
}
tests := []struct {
name string
fields fields
want map[int64]*model.Block
}{
// TODO: Add test cases.
{
name: "GetBlocks:success",
fields: fields{
// BlockQueueLock: sync.RWMutex,
BlockQueue: map[int64]*model.Block{
0: {},
},
},
want: map[int64]*model.Block{
0: {},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bps := &BlockPoolService{
BlockQueueLock: tt.fields.BlockQueueLock,
BlockQueue: tt.fields.BlockQueue,
}
if got := bps.GetBlocks(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("BlockPoolService.GetBlocks() = %v, want %v", got, tt.want)
}
})
}
}

func TestBlockPoolService_GetBlock(t *testing.T) {
type fields struct {
BlockQueueLock sync.RWMutex
BlockQueue map[int64]*model.Block
}
type args struct {
index int64
}
tests := []struct {
name string
fields fields
args args
want *model.Block
}{
// TODO: Add test cases.
{
name: "GetBlock:success",
fields: fields{
BlockQueue: map[int64]*model.Block{
0: {},
},
},
want: &model.Block{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bps := &BlockPoolService{
BlockQueueLock: tt.fields.BlockQueueLock,
BlockQueue: tt.fields.BlockQueue,
}
if got := bps.GetBlock(tt.args.index); !reflect.DeepEqual(got, tt.want) {
t.Errorf("BlockPoolService.GetBlock() = %v, want %v", got, tt.want)
}
})
}
}

func TestBlockPoolService_InsertBlock(t *testing.T) {
type fields struct {
BlockQueueLock sync.RWMutex
BlockQueue map[int64]*model.Block
}
type args struct {
block *model.Block
index int64
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
{
name: "InsertBlock:success",
fields: fields{
BlockQueue: map[int64]*model.Block{
0: {},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bps := &BlockPoolService{
BlockQueueLock: tt.fields.BlockQueueLock,
BlockQueue: tt.fields.BlockQueue,
}
bps.InsertBlock(tt.args.block, tt.args.index)
if got := bps.GetBlock(tt.args.index); !reflect.DeepEqual(got, tt.fields.BlockQueue[tt.args.index]) {
t.Errorf("BlockPoolService.InsertBlock() = %v, want %v", got, tt.fields.BlockQueue[tt.args.index])
}
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be assertion here to check whether the blocks is inserted correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just updated, pls check again ya @capt4ce

}
}

func TestBlockPoolService_ClearBlockPool(t *testing.T) {
type fields struct {
BlockQueueLock sync.RWMutex
BlockQueue map[int64]*model.Block
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
{
name: "ClearBlockPool:success",
fields: fields{
BlockQueue: map[int64]*model.Block{
0: {},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bps := &BlockPoolService{
BlockQueueLock: tt.fields.BlockQueueLock,
BlockQueue: tt.fields.BlockQueue,
}
bps.ClearBlockPool()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be assertion here checking the blockpool cleared correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just updated, pls check again ya @capt4ce

if len(tt.fields.BlockQueue) > 0 {
t.Errorf("BlockPoolService.ClearBlockPool() = %v, want %v", tt.fields.BlockQueue, make(map[int64]*model.Block))
}
})
}
}