From 148b7463c9662b477e81f9d76559c680dac420b9 Mon Sep 17 00:00:00 2001 From: Nikita Neznaemov Date: Mon, 8 Apr 2024 17:47:33 +0300 Subject: [PATCH] observer refactoring to generics --- .../deliver/testing/block_deliverer_mock.go | 76 +++++-------------- 1 file changed, 20 insertions(+), 56 deletions(-) diff --git a/client/deliver/testing/block_deliverer_mock.go b/client/deliver/testing/block_deliverer_mock.go index fcd895f..e8c8526 100644 --- a/client/deliver/testing/block_deliverer_mock.go +++ b/client/deliver/testing/block_deliverer_mock.go @@ -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 } @@ -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) }