Skip to content

Commit

Permalink
Merge pull request #1 from ElrondNetwork/EN-10372-general-refactor
Browse files Browse the repository at this point in the history
EN-10372: general refactoring
  • Loading branch information
bogdan-rosianu authored Jul 20, 2021
2 parents 21c4fc2 + 258831b commit e468187
Show file tree
Hide file tree
Showing 256 changed files with 985 additions and 36,024 deletions.
16 changes: 10 additions & 6 deletions core/accumulator/timeAccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"sync"
"time"

logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
)

var _ core.Accumulator = (*timeAccumulator)(nil)
var log = logger.GetOrCreate("core/accumulator")

const minimumAllowedTime = time.Millisecond * 10

Expand All @@ -26,10 +25,11 @@ type timeAccumulator struct {
mut sync.Mutex
data []interface{}
output chan []interface{}
log core.Logger
}

// NewTimeAccumulator returns a new accumulator instance
func NewTimeAccumulator(maxAllowedTime time.Duration, maxOffset time.Duration) (*timeAccumulator, error) {
func NewTimeAccumulator(maxAllowedTime time.Duration, maxOffset time.Duration, logger core.Logger) (*timeAccumulator, error) {
if maxAllowedTime < minimumAllowedTime {
return nil, fmt.Errorf("%w for maxAllowedTime as minimum allowed time is %v",
core.ErrInvalidValue,
Expand All @@ -39,6 +39,9 @@ func NewTimeAccumulator(maxAllowedTime time.Duration, maxOffset time.Duration) (
if maxOffset < 0 {
return nil, fmt.Errorf("%w for maxOffset: should not be negative", core.ErrInvalidValue)
}
if check.IfNil(logger) {
return nil, core.ErrNilLogger
}

ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -47,6 +50,7 @@ func NewTimeAccumulator(maxAllowedTime time.Duration, maxOffset time.Duration) (
maxAllowedTime: maxAllowedTime,
output: make(chan []interface{}),
maxOffset: maxOffset,
log: logger,
}

go ta.continuousEviction(ctx)
Expand Down Expand Up @@ -80,7 +84,7 @@ func (ta *timeAccumulator) continuousEviction(ctx context.Context) {
return
}
case <-ctx.Done():
log.Debug("closing timeAccumulator.continuousEviction go routine")
ta.log.Debug("closing timeAccumulator.continuousEviction go routine")
return
}
}
Expand Down Expand Up @@ -112,7 +116,7 @@ func (ta *timeAccumulator) doEviction(ctx context.Context) bool {
case ta.output <- tempData:
return false
case <-ctx.Done():
log.Debug("closing timeAccumulator.doEviction go routine")
ta.log.Debug("closing timeAccumulator.doEviction go routine")
return true
}
}
Expand Down
36 changes: 23 additions & 13 deletions core/accumulator/timeAccumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/accumulator"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/accumulator"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -17,7 +18,7 @@ var timeout = time.Second * 2
func TestNewTimeAccumulator_InvalidMaxWaitTimeShouldErr(t *testing.T) {
t.Parallel()

ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime-1, 0)
ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime-1, 0, &mock.LoggerMock{})

assert.True(t, check.IfNil(ta))
assert.True(t, errors.Is(err, core.ErrInvalidValue))
Expand All @@ -26,16 +27,25 @@ func TestNewTimeAccumulator_InvalidMaxWaitTimeShouldErr(t *testing.T) {
func TestNewTimeAccumulator_InvalidMaxOffsetShouldErr(t *testing.T) {
t.Parallel()

ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime, -1)
ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime, -1, &mock.LoggerMock{})

assert.True(t, check.IfNil(ta))
assert.True(t, errors.Is(err, core.ErrInvalidValue))
}

func TestNewTimeAccumulator_NilLoggerShouldErr(t *testing.T) {
t.Parallel()

ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime, 0, nil)

assert.True(t, check.IfNil(ta))
assert.True(t, errors.Is(err, core.ErrNilLogger))
}

func TestNewTimeAccumulator_ShouldWork(t *testing.T) {
t.Parallel()

ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime, 0)
ta, err := accumulator.NewTimeAccumulator(accumulator.MinimumAllowedTime, 0, &mock.LoggerMock{})

assert.False(t, check.IfNil(ta))
assert.Nil(t, err)
Expand All @@ -48,7 +58,7 @@ func TestTimeAccumulator_AddDataShouldWorkEvenIfTheChanIsBlocked(t *testing.T) {

chDone := make(chan struct{})
allowedTime := time.Millisecond * 100
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0, &mock.LoggerMock{})
go func() {
ta.AddData(struct{}{})
time.Sleep(allowedTime * 3)
Expand All @@ -75,7 +85,7 @@ func TestTimeAccumulator_EvictionShouldStopWhenCloseIsCalled(t *testing.T) {
t.Parallel()

allowedTime := time.Millisecond * 100
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0, &mock.LoggerMock{})

ta.AddData(struct{}{})
time.Sleep(allowedTime * 3)
Expand All @@ -94,7 +104,7 @@ func TestTimeAccumulator_EvictionDuringWaitShouldStopWhenCloseIsCalled(t *testin
t.Parallel()

allowedTime := time.Millisecond * 100
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0, &mock.LoggerMock{})
ta.AddData(struct{}{})

_ = ta.Close()
Expand All @@ -111,7 +121,7 @@ func TestTimeAccumulator_EvictionShouldPreserveTheOrder(t *testing.T) {
t.Parallel()

allowedTime := time.Millisecond * 100
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0, &mock.LoggerMock{})

data := []interface{}{"data1", "data2", "data3"}
for _, d := range data {
Expand All @@ -130,7 +140,7 @@ func TestTimeAccumulator_EvictionWithOffsetShouldPreserveTheOrder(t *testing.T)
t.Parallel()

allowedTime := time.Millisecond * 100
ta, _ := accumulator.NewTimeAccumulator(allowedTime, time.Millisecond)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, time.Millisecond, &mock.LoggerMock{})

data := []interface{}{"data1", "data2", "data3"}
for _, d := range data {
Expand All @@ -151,7 +161,7 @@ func TestTimeAccumulator_ComputeWaitTimeWithMaxOffsetZeroShouldRetMaxWaitTime(t
t.Parallel()

allowedTime := time.Millisecond * 56
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, 0, &mock.LoggerMock{})

assert.Equal(t, allowedTime, ta.ComputeWaitTime())
}
Expand All @@ -161,7 +171,7 @@ func TestTimeAccumulator_ComputeWaitTimeShouldWork(t *testing.T) {

allowedTime := time.Millisecond * 56
maxOffset := time.Millisecond * 12
ta, _ := accumulator.NewTimeAccumulator(allowedTime, maxOffset)
ta, _ := accumulator.NewTimeAccumulator(allowedTime, maxOffset, &mock.LoggerMock{})

numComputations := 10000
for i := 0; i < numComputations; i++ {
Expand Down
4 changes: 2 additions & 2 deletions core/alarm/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/core/alarm"
"github.com/ElrondNetwork/elrond-go/core/atomic"
"github.com/ElrondNetwork/elrond-go-core/core/alarm"
"github.com/ElrondNetwork/elrond-go-core/core/atomic"
"github.com/stretchr/testify/require"
)

Expand Down
16 changes: 9 additions & 7 deletions core/appStatusPolling/appStatusPolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@ import (
"sync"
"time"

logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
)

const minPollingDuration = time.Second

var log = logger.GetOrCreate("core/appStatusPolling")

// AppStatusPolling will update an AppStatusHandler by polling components at a predefined interval
type AppStatusPolling struct {
pollingDuration time.Duration
mutRegisteredFunc sync.RWMutex
registeredFunctions []func(appStatusHandler core.AppStatusHandler)
appStatusHandler core.AppStatusHandler
log core.Logger
}

// NewAppStatusPolling will return an instance of AppStatusPolling
func NewAppStatusPolling(appStatusHandler core.AppStatusHandler, pollingDuration time.Duration) (*AppStatusPolling, error) {
func NewAppStatusPolling(appStatusHandler core.AppStatusHandler, pollingDuration time.Duration, logger core.Logger) (*AppStatusPolling, error) {
if check.IfNil(appStatusHandler) {
return nil, ErrNilAppStatusHandler
}
if pollingDuration < minPollingDuration {
return nil, ErrPollingDurationToSmall
}
if check.IfNil(logger) {
return nil, core.ErrNilLogger
}
return &AppStatusPolling{
pollingDuration: pollingDuration,
appStatusHandler: appStatusHandler,
log: logger,
}, nil
}

Expand All @@ -53,7 +55,7 @@ func (asp *AppStatusPolling) Poll(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Debug("closing AppStatusPolling.Poll go routine")
asp.log.Debug("closing AppStatusPolling.Poll go routine")
return
case <-time.After(asp.pollingDuration):
}
Expand Down
28 changes: 17 additions & 11 deletions core/appStatusPolling/appStatusPolling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,51 @@ import (
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/appStatusPolling"
"github.com/ElrondNetwork/elrond-go/core/mock"
"github.com/ElrondNetwork/elrond-go/statusHandler"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/appStatusPolling"
"github.com/ElrondNetwork/elrond-go-core/core/mock"
"github.com/stretchr/testify/assert"
)

func TestNewAppStatusPooling_NilAppStatusHandlerShouldErr(t *testing.T) {
t.Parallel()

_, err := appStatusPolling.NewAppStatusPolling(nil, time.Second)
_, err := appStatusPolling.NewAppStatusPolling(nil, time.Second, &mock.LoggerMock{})
assert.Equal(t, err, appStatusPolling.ErrNilAppStatusHandler)
}

func TestNewAppStatusPooling_NilLoggerShouldErr(t *testing.T) {
t.Parallel()

_, err := appStatusPolling.NewAppStatusPolling(&mock.StatusHandlerMock{}, time.Second, nil)
assert.Equal(t, err, core.ErrNilLogger)
}

func TestNewAppStatusPooling_NegativePollingDurationShouldErr(t *testing.T) {
t.Parallel()

_, err := appStatusPolling.NewAppStatusPolling(&statusHandler.NilStatusHandler{}, time.Duration(-1))
_, err := appStatusPolling.NewAppStatusPolling(&mock.StatusHandlerMock{}, time.Duration(-1), &mock.LoggerMock{})
assert.Equal(t, err, appStatusPolling.ErrPollingDurationToSmall)
}

func TestNewAppStatusPooling_ZeroPollingDurationShouldErr(t *testing.T) {
t.Parallel()

_, err := appStatusPolling.NewAppStatusPolling(&statusHandler.NilStatusHandler{}, 0)
_, err := appStatusPolling.NewAppStatusPolling(&mock.StatusHandlerMock{}, 0, &mock.LoggerMock{})
assert.Equal(t, err, appStatusPolling.ErrPollingDurationToSmall)
}

func TestNewAppStatusPooling_OkValsShouldPass(t *testing.T) {
t.Parallel()

_, err := appStatusPolling.NewAppStatusPolling(&statusHandler.NilStatusHandler{}, time.Second)
_, err := appStatusPolling.NewAppStatusPolling(&mock.StatusHandlerMock{}, time.Second, &mock.LoggerMock{})
assert.Nil(t, err)
}

func TestNewAppStatusPolling_RegisterHandlerFuncShouldErr(t *testing.T) {
t.Parallel()

asp, err := appStatusPolling.NewAppStatusPolling(&statusHandler.NilStatusHandler{}, time.Second)
asp, err := appStatusPolling.NewAppStatusPolling(&mock.StatusHandlerMock{}, time.Second, &mock.LoggerMock{})
assert.Nil(t, err)

err = asp.RegisterPollingFunc(nil)
Expand All @@ -60,11 +66,11 @@ func TestAppStatusPolling_Poll_TestNumOfConnectedAddressesCalled(t *testing.T) {
chDone <- struct{}{}
},
}
asp, err := appStatusPolling.NewAppStatusPolling(&ash, pollingDuration)
asp, err := appStatusPolling.NewAppStatusPolling(&ash, pollingDuration, &mock.LoggerMock{})
assert.Nil(t, err)

err = asp.RegisterPollingFunc(func(appStatusHandler core.AppStatusHandler) {
appStatusHandler.SetInt64Value(core.MetricNumConnectedPeers, int64(10))
appStatusHandler.SetInt64Value("metric", int64(10))
})
assert.Nil(t, err)

Expand Down
2 changes: 1 addition & 1 deletion core/check/ifNil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package check_test
import (
"testing"

"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion core/check/ifZero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion core/closing/safeChanCloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/stretchr/testify/assert"
)

Expand Down
Loading

0 comments on commit e468187

Please sign in to comment.