Skip to content

Commit

Permalink
observer refactoring to generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Neznaemov committed Apr 8, 2024
1 parent f9b34b4 commit 148b746
Showing 1 changed file with 20 additions and 56 deletions.
76 changes: 20 additions & 56 deletions client/deliver/testing/block_deliverer_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,63 +122,27 @@ func NewBlocksDelivererMock(rootPath string, closeWhenAllRead bool) (*BlocksDeli
}

func (m *BlocksDelivererMock) Blocks(
ctx context.Context,
_ context.Context,
channelName string,
identity msp.SigningIdentity,
_ msp.SigningIdentity,
blockRange ...int64,
) (<-chan *common.Block, func() error, error) {
if _, ok := m.data[channelName]; !ok {
return nil, nil, fmt.Errorf("have no mocked data for this channel")
}
closer := func() error { return nil }

var (
blockRangeFrom int64 = 0
blockRangeTo int64 = math.MaxInt64
)

if len(blockRange) > 0 {
blockRangeFrom = blockRange[0]
}
if len(blockRange) > 1 {
blockRangeTo = blockRange[1]
}

if blockRangeFrom < 0 {
blockRangeFrom = int64(len(m.data[channelName])) + blockRangeFrom
}

if blockRangeTo < 0 {
blockRangeTo = int64(len(m.data[channelName])) + blockRangeTo
}

if blockRangeFrom > int64(len(m.data[channelName])) {
blockRangeFrom = int64(len(m.data[channelName])) - 1
}

if blockRangeTo > int64(len(m.data[channelName])) {
blockRangeTo = int64(len(m.data[channelName])) - 1
}

ch := make(chan *common.Block, (blockRangeTo-blockRangeFrom)+1)
for i := blockRangeFrom; i <= blockRangeTo; i++ {
ch <- m.data[channelName][i]
}

if m.closeWhenAllRead {
close(ch)
}

return ch, closer, nil
return blocks[*common.Block](m.data, channelName, m.closeWhenAllRead, blockRange...)
}

func (m *BlocksDelivererMock) ParsedBlocks(
ctx context.Context,
_ context.Context,
channelName string,
identity msp.SigningIdentity,
_ msp.SigningIdentity,
blockRange ...int64,
) (<-chan *hlfproto.Block, func() error, error) {
if _, ok := m.parsedData[channelName]; !ok {

return blocks[*hlfproto.Block](m.parsedData, channelName, m.closeWhenAllRead, blockRange...)
}

func blocks[T any](data map[string][]T, channelName string, closeWhenAllRead bool, blockRange ...int64) (<-chan T, func() error, error) {
if _, ok := data[channelName]; !ok {
return nil, nil, fmt.Errorf("have no mocked data for this channel")
}
closer := func() error { return nil }
Expand All @@ -196,27 +160,27 @@ func (m *BlocksDelivererMock) ParsedBlocks(
}

if blockRangeFrom < 0 {
blockRangeFrom = int64(len(m.data[channelName])) + blockRangeFrom
blockRangeFrom = int64(len(data[channelName])) + blockRangeFrom
}

if blockRangeTo < 0 {
blockRangeTo = int64(len(m.data[channelName])) + blockRangeTo
blockRangeTo = int64(len(data[channelName])) + blockRangeTo
}

if blockRangeFrom > int64(len(m.data[channelName])) {
blockRangeFrom = int64(len(m.data[channelName])) - 1
if blockRangeFrom > int64(len(data[channelName])) {
blockRangeFrom = int64(len(data[channelName])) - 1
}

if blockRangeTo > int64(len(m.data[channelName])) {
blockRangeTo = int64(len(m.data[channelName])) - 1
if blockRangeTo > int64(len(data[channelName])) {
blockRangeTo = int64(len(data[channelName])) - 1
}

ch := make(chan *hlfproto.Block, (blockRangeTo-blockRangeFrom)+1)
ch := make(chan T, (blockRangeTo-blockRangeFrom)+1)
for i := blockRangeFrom; i <= blockRangeTo; i++ {
ch <- m.parsedData[channelName][i]
ch <- data[channelName][i]
}

if m.closeWhenAllRead {
if closeWhenAllRead {
close(ch)
}

Expand Down

0 comments on commit 148b746

Please sign in to comment.