Skip to content

Commit

Permalink
Refactored a bit tests in 'state' package.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Jun 17, 2022
1 parent 1031b74 commit 7549046
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions pkg/state/blockreadwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/util/common"
"go.uber.org/atomic"
)

const (
Expand Down Expand Up @@ -283,19 +284,16 @@ func TestSimultaneousReadWrite(t *testing.T) {
if err != nil {
t.Fatalf("Can not read blocks from blockchain file: %v", err)
}
var mtx sync.Mutex
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
errCounter := 0
var errCounter atomic.Int64
readTasks := make(chan *readTask, tasksChanBufferSize)
wg.Add(1)
go func() {
defer wg.Done()
err1 := writeBlocks(ctx, to.rw, blocks, readTasks, true, false)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Writer error: %v\n", err1)
cancel()
}
Expand All @@ -306,16 +304,14 @@ func TestSimultaneousReadWrite(t *testing.T) {
defer wg.Done()
err1 := testReader(to.rw, readTasks)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Reader error: %v\n", err1)
cancel()
}
}()
}
wg.Wait()
if errCounter != 0 {
if errCounter.Load() != 0 {
t.Fatalf("Reader/writer error.")
}
}
Expand All @@ -338,19 +334,16 @@ func TestReadNewest(t *testing.T) {
if err != nil {
t.Fatalf("Can not read blocks from blockchain file: %v", err)
}
var mtx sync.Mutex
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
errCounter := 0
var errCounter atomic.Int64
readTasks := make(chan *readTask, tasksChanBufferSize)
wg.Add(1)
go func() {
defer wg.Done()
err1 := writeBlocks(ctx, to.rw, blocks, readTasks, false, false)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Writer error: %v\n", err1)
cancel()
}
Expand All @@ -361,16 +354,14 @@ func TestReadNewest(t *testing.T) {
defer wg.Done()
err1 := testNewestReader(to.rw, readTasks)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Reader error: %v\n", err1)
cancel()
}
}()
}
wg.Wait()
if errCounter != 0 {
if errCounter.Load() != 0 {
t.Fatalf("Reader/writer error.")
}
}
Expand Down Expand Up @@ -480,19 +471,16 @@ func TestProtobufReadWrite(t *testing.T) {
prevId = protobufBlocks[i].BlockID()
}

var mtx sync.Mutex
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
errCounter := 0
var errCounter atomic.Int64
readTasks := make(chan *readTask, tasksChanBufferSize)
wg.Add(1)
go func() {
defer wg.Done()
err1 := writeBlocks(ctx, to.rw, protobufBlocks, readTasks, true, true)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Writer error: %v\n", err1)
cancel()
}
Expand All @@ -503,16 +491,14 @@ func TestProtobufReadWrite(t *testing.T) {
defer wg.Done()
err1 := testReader(to.rw, readTasks)
if err1 != nil {
mtx.Lock()
errCounter++
mtx.Unlock()
errCounter.Inc()
fmt.Printf("Reader error: %v\n", err1)
cancel()
}
}()
}
wg.Wait()
if errCounter != 0 {
if errCounter.Load() != 0 {
t.Fatalf("Reader/writer error.")
}
}
Expand Down

0 comments on commit 7549046

Please sign in to comment.