Skip to content

Commit

Permalink
Merge pull request #12116 from smartcontractkit/unittests
Browse files Browse the repository at this point in the history
fix unit tests for mercury
  • Loading branch information
infiloop2 authored Feb 22, 2024
2 parents 6998268 + 2e473a5 commit a53eaff
Show file tree
Hide file tree
Showing 5 changed files with 670 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,36 +246,86 @@ func TestPacker_UnpackCheckCallbackResult(t *testing.T) {
}
}

func TestPacker_PackUserCheckErrorHandler(t *testing.T) {
tests := []struct {
name string
errCode encoding.ErrCode
extraData []byte
rawOutput []byte
errored bool
}{
{
name: "happy path",
errCode: encoding.ErrCodeStreamsBadRequest,
extraData: func() []byte {
b, _ := hexutil.Decode("0x19d97a94737c9583000000000000000000000001ea8ed6d0617dd5b3b87374020efaf030")

return b
}(),
rawOutput: []byte{0xf, 0xb1, 0x72, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x55, 0xd0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x24, 0x19, 0xd9, 0x7a, 0x94, 0x73, 0x7c, 0x95, 0x83, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xea, 0x8e, 0xd6, 0xd0, 0x61, 0x7d, 0xd5, 0xb3, 0xb8, 0x73, 0x74, 0x2, 0xe, 0xfa, 0xf0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
errored: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
packer := NewAbiPacker()

b, err := packer.PackUserCheckErrorHandler(test.errCode, test.extraData)

if !test.errored {
require.NoError(t, err, "no error expected from packing")

assert.Equal(t, test.rawOutput, b, "raw bytes for output should match expected")
} else {
assert.NotNil(t, err, "error expected from packing function")
}
})
}
}

func Test_CalculateRetryConfigFn(t *testing.T) {
tests := []struct {
name string
times int
expected time.Duration
name string
times int
upkeepType automationTypes.UpkeepType
expected time.Duration
}{
{
name: "first retry",
times: 1,
expected: 1 * time.Second,
name: "first retry",
times: 1,
upkeepType: automationTypes.LogTrigger,
expected: 1 * time.Second,
},
{
name: "second retry",
times: 2,
upkeepType: automationTypes.LogTrigger,
expected: 1 * time.Second,
},
{
name: "second retry",
times: 2,
expected: 1 * time.Second,
name: "fifth retry",
times: 5,
upkeepType: automationTypes.LogTrigger,
expected: 1 * time.Second,
},
{
name: "fifth retry",
times: 5,
expected: 1 * time.Second,
name: "sixth retry",
times: 6,
upkeepType: automationTypes.LogTrigger,
expected: 5 * time.Second,
},
{
name: "sixth retry",
times: 6,
expected: 5 * time.Second,
name: "timeout",
times: totalMediumPluginRetries + 1,
upkeepType: automationTypes.LogTrigger,
expected: RetryIntervalTimeout,
},
{
name: "timeout",
times: totalMediumPluginRetries + 1,
expected: RetryIntervalTimeout,
name: "conditional first timeout",
times: 1,
upkeepType: automationTypes.ConditionTrigger,
expected: RetryIntervalTimeout,
},
}

Expand All @@ -284,7 +334,7 @@ func Test_CalculateRetryConfigFn(t *testing.T) {
cfg := newMercuryConfigMock()
var result time.Duration
for i := 0; i < tc.times; i++ {
result = CalculateStreamsRetryConfigFn(automationTypes.ConditionTrigger, "prk", cfg)
result = CalculateStreamsRetryConfigFn(tc.upkeepType, "prk", cfg)
}
assert.Equal(t, tc.expected, result)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,19 @@ func (c *client) DoRequest(ctx context.Context, streamsLookup *mercury.StreamsLo
return state, nil, errCode, retryable, retryInterval, reqErr
}

// Now we have exhausted all retries and we have an error code to expose to user expose it with noPipelineError
// otherwise expose the last pipeline error to pipeline runner (not the user)
if errCode != encoding.ErrCodeNil {
return encoding.NoPipelineError, nil, errCode, false, 0 * time.Second, nil
}
return state, nil, errCode, false, 0 * time.Second, reqErr
// Now we have exhausted all our retries. We treat it as not a pipeline error
// and expose error code to the user
return encoding.NoPipelineError, nil, errCode, false, 0 * time.Second, nil
}

// All feeds faced no pipeline error
// If any feed request returned an error code, return the error code with empty values, else return the values
if !allFeedsReturnedValues {
return encoding.NoPipelineError, nil, errCode, false, 0 * time.Second, reqErr
return encoding.NoPipelineError, nil, errCode, false, 0 * time.Second, nil
}

// All success, return the results
return state, results, encoding.ErrCodeNil, false, 0 * time.Second, nil
return encoding.NoPipelineError, results, encoding.ErrCodeNil, false, 0 * time.Second, nil
}

func (c *client) singleFeedRequest(ctx context.Context, ch chan<- mercury.MercuryData, index int, sl *mercury.StreamsLookup) {
Expand Down Expand Up @@ -174,6 +171,7 @@ func (c *client) singleFeedRequest(ctx context.Context, ch chan<- mercury.Mercur
c.lggr.Warnf("at block %s upkeep %s GET request fails for feed %s: %v", sl.Time.String(), sl.UpkeepId.String(), sl.Feeds[index], err)
retryable = true
state = encoding.MercuryFlakyFailure
errCode = encoding.ErrCodeStreamsUnknownError
return err
}
defer httpResponse.Body.Close()
Expand Down
Loading

0 comments on commit a53eaff

Please sign in to comment.