From 38184e88c885bc43f03f60d8472645499184cde9 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 14 May 2024 20:47:57 +0800 Subject: [PATCH] sweep: check all states in `TestMarkInputsPublishFailed` --- sweep/sweeper_test.go | 63 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/sweep/sweeper_test.go b/sweep/sweeper_test.go index e25864bcb..667906de9 100644 --- a/sweep/sweeper_test.go +++ b/sweep/sweeper_test.go @@ -206,7 +206,7 @@ func TestMarkInputsPublishFailed(t *testing.T) { Store: mockStore, }) - // Create three testing inputs. + // Create testing inputs for each state. // // inputNotExist specifies an input that's not found in the sweeper's // `inputs` map. @@ -240,18 +240,52 @@ func TestMarkInputsPublishFailed(t *testing.T) { state: Published, } + // inputPublishFailed specifies an input that's failed to be published. + inputPublishFailed := &wire.TxIn{ + PreviousOutPoint: wire.OutPoint{Index: 5}, + } + s.inputs[inputPublishFailed.PreviousOutPoint] = &SweeperInput{ + state: PublishFailed, + } + + // inputSwept specifies an input that's swept. + inputSwept := &wire.TxIn{ + PreviousOutPoint: wire.OutPoint{Index: 6}, + } + s.inputs[inputSwept.PreviousOutPoint] = &SweeperInput{ + state: Swept, + } + + // inputExcluded specifies an input that's excluded. + inputExcluded := &wire.TxIn{ + PreviousOutPoint: wire.OutPoint{Index: 7}, + } + s.inputs[inputExcluded.PreviousOutPoint] = &SweeperInput{ + state: Excluded, + } + + // inputFailed specifies an input that's failed. + inputFailed := &wire.TxIn{ + PreviousOutPoint: wire.OutPoint{Index: 8}, + } + s.inputs[inputFailed.PreviousOutPoint] = &SweeperInput{ + state: Failed, + } + + // Gather all inputs' outpoints. + pendingOps := make([]wire.OutPoint, 0, len(s.inputs)+1) + for op := range s.inputs { + pendingOps = append(pendingOps, op) + } + pendingOps = append(pendingOps, inputNotExist.PreviousOutPoint) + // Mark the test inputs. We expect the non-exist input and the // inputInit to be skipped, and the final input to be marked as // published. - s.markInputsPublishFailed([]wire.OutPoint{ - inputNotExist.PreviousOutPoint, - inputInit.PreviousOutPoint, - inputPendingPublish.PreviousOutPoint, - inputPublished.PreviousOutPoint, - }) + s.markInputsPublishFailed(pendingOps) // We expect unchanged number of pending inputs. - require.Len(s.inputs, 3) + require.Len(s.inputs, 7) // We expect the init input's state to stay unchanged. require.Equal(Init, @@ -266,6 +300,19 @@ func TestMarkInputsPublishFailed(t *testing.T) { require.Equal(PublishFailed, s.inputs[inputPublished.PreviousOutPoint].state) + // We expect the publish failed input to stay unchanged. + require.Equal(PublishFailed, + s.inputs[inputPublishFailed.PreviousOutPoint].state) + + // We expect the swept input to stay unchanged. + require.Equal(Swept, s.inputs[inputSwept.PreviousOutPoint].state) + + // We expect the excluded input to stay unchanged. + require.Equal(Excluded, s.inputs[inputExcluded.PreviousOutPoint].state) + + // We expect the failed input to stay unchanged. + require.Equal(Failed, s.inputs[inputFailed.PreviousOutPoint].state) + // Assert mocked statements are executed as expected. mockStore.AssertExpectations(t) }