Skip to content

Commit

Permalink
Fix unit test issues.
Browse files Browse the repository at this point in the history
Signed-off-by: txaty <[email protected]>
  • Loading branch information
txaty committed Nov 23, 2023
1 parent 7497561 commit e7204d2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion merkle_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
func mockDataBlocks(num int) []DataBlock {
blocks := make([]DataBlock, num)
for i := 0; i < num; i++ {
byteLen := rand.Intn(1 << 15)
byteLen := max(32, rand.Intn(1<<15))
block := &mock.DataBlock{
Data: make([]byte, byteLen),
}
Expand Down
7 changes: 5 additions & 2 deletions proof_gen_and_tree_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"crypto/sha256"
"fmt"
"reflect"
"sync/atomic"
"testing"
)

Expand Down Expand Up @@ -147,6 +148,7 @@ func TestMerkleTreeNew_modeProofGenAndTreeBuild(t *testing.T) {
}

func TestMerkleTreeNew_modeProofGenAndTreeBuildParallel(t *testing.T) {
var hashFuncCounter atomic.Uint32
type args struct {
blocks []DataBlock
config *Config
Expand Down Expand Up @@ -221,12 +223,13 @@ func TestMerkleTreeNew_modeProofGenAndTreeBuildParallel(t *testing.T) {
{
name: "test_tree_build_hash_func_error",
args: args{
blocks: mockDataBlocks(100),
blocks: mockDataBlocks(5),
config: &Config{
HashFunc: func(block []byte) ([]byte, error) {
if len(block) == 64 {
if hashFuncCounter.Load() >= 5 {
return nil, fmt.Errorf("hash func error")
}
hashFuncCounter.Add(1)
sha256Func := sha256.New()
sha256Func.Write(block)
return sha256Func.Sum(nil), nil
Expand Down
19 changes: 19 additions & 0 deletions tree_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ func TestMerkleTreeNew_modeTreeBuildParallel(t *testing.T) {
},
wantErr: true,
},
{
name: "test_hash_func_error_when_computing_nodes_parallel",
args: args{
blocks: mockDataBlocks(4),
config: &Config{
HashFunc: func(block []byte) ([]byte, error) {
if hashFuncCounter.Load() == 5 {
return nil, fmt.Errorf("hash func error")
}
hashFuncCounter.Add(1)
sha256Func := sha256.New()
sha256Func.Write(block)
return sha256Func.Sum(nil), nil
},
},
},
wantErr: true,
},
{
name: "test_hash_func_error_when_computing_root_parallel",
args: args{
Expand All @@ -299,6 +317,7 @@ func TestMerkleTreeNew_modeTreeBuildParallel(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hashFuncCounter.Store(0)
m, err := New(tt.args.config, tt.args.blocks)
if (err != nil) != tt.wantErr {
t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit e7204d2

Please sign in to comment.