|
| 1 | +package factory |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/iotexproject/go-pkgs/hash" |
| 10 | + "github.com/iotexproject/iotex-core/v2/action" |
| 11 | + "github.com/iotexproject/iotex-core/v2/blockchain/block" |
| 12 | + "github.com/iotexproject/iotex-core/v2/test/identityset" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBlockPreparer_PrepareOrWait(t *testing.T) { |
| 18 | + preparer := newBlockPreparer() |
| 19 | + prevHash := hash.Hash256b([]byte("previousHash")) |
| 20 | + timestamp := time.Now() |
| 21 | + mockBlk := &block.Block{} |
| 22 | + called := false |
| 23 | + |
| 24 | + // Mock mint function |
| 25 | + mintFn := func() (*block.Block, error) { |
| 26 | + if called { |
| 27 | + return nil, errors.New("block already minted") |
| 28 | + } |
| 29 | + return mockBlk, nil |
| 30 | + } |
| 31 | + mintFn2 := func() (*block.Block, error) { |
| 32 | + return &block.Block{ |
| 33 | + Body: block.Body{ |
| 34 | + Actions: []*action.SealedEnvelope{}, |
| 35 | + }, |
| 36 | + }, nil |
| 37 | + } |
| 38 | + |
| 39 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 40 | + defer cancel() |
| 41 | + |
| 42 | + wg := sync.WaitGroup{} |
| 43 | + wg.Add(2) |
| 44 | + go func() { |
| 45 | + blk1, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn) |
| 46 | + require.NoError(t, err) |
| 47 | + require.Equal(t, mockBlk, blk1) |
| 48 | + wg.Done() |
| 49 | + }() |
| 50 | + go func() { |
| 51 | + blk2, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp.Add(time.Second), mintFn2) |
| 52 | + require.NoError(t, err) |
| 53 | + require.NotEqual(t, mockBlk, blk2) |
| 54 | + wg.Done() |
| 55 | + }() |
| 56 | + blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn) |
| 57 | + require.NoError(t, err) |
| 58 | + require.Equal(t, mockBlk, blk) |
| 59 | + wg.Wait() |
| 60 | +} |
| 61 | + |
| 62 | +func TestBlockPreparer_PrepareOrWait_Timeout(t *testing.T) { |
| 63 | + preparer := newBlockPreparer() |
| 64 | + prevHash := hash.Hash256b([]byte("previousHash")) |
| 65 | + timestamp := time.Now() |
| 66 | + |
| 67 | + // Mock mint function that takes too long |
| 68 | + mintFn := func() (*block.Block, error) { |
| 69 | + time.Sleep(2 * time.Second) |
| 70 | + return &block.Block{}, nil |
| 71 | + } |
| 72 | + |
| 73 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn) |
| 77 | + require.Error(t, err) |
| 78 | + require.Nil(t, blk) |
| 79 | +} |
| 80 | + |
| 81 | +func TestBlockPreparer_ReceiveBlock(t *testing.T) { |
| 82 | + preparer := newBlockPreparer() |
| 83 | + prevHash := hash.Hash256b([]byte("previousHash")) |
| 84 | + timestamp := time.Now() |
| 85 | + |
| 86 | + // Mock mint function |
| 87 | + mintFn := func() (*block.Block, error) { |
| 88 | + builder := &block.TestingBuilder{} |
| 89 | + blk, err := builder.SetPrevBlockHash(prevHash).SignAndBuild(identityset.PrivateKey(0)) |
| 90 | + return &blk, err |
| 91 | + } |
| 92 | + |
| 93 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + blk, err := preparer.PrepareOrWait(ctx, prevHash[:], timestamp, mintFn) |
| 97 | + require.NoError(t, err) |
| 98 | + |
| 99 | + emptyblk := &block.Block{} |
| 100 | + require.NoError(t, preparer.ReceiveBlock(emptyblk)) |
| 101 | + _, ok := preparer.tasks[prevHash] |
| 102 | + require.True(t, ok) |
| 103 | + |
| 104 | + require.NoError(t, preparer.ReceiveBlock(blk)) |
| 105 | + _, ok = preparer.tasks[prevHash] |
| 106 | + require.False(t, ok) |
| 107 | +} |
0 commit comments