pipeline_test.go 2.22 KB
Newer Older
protolambda's avatar
protolambda committed
1 2 3 4 5 6 7
package derive

import (
	"context"
	"io"
	"testing"

8
	"github.com/ethereum-optimism/optimism/op-node/eth"
protolambda's avatar
protolambda committed
9
	"github.com/ethereum-optimism/optimism/op-node/testutils"
10
	"github.com/stretchr/testify/mock"
protolambda's avatar
protolambda committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
)

var _ Engine = (*testutils.MockEngine)(nil)

var _ L1Fetcher = (*testutils.MockL1Source)(nil)

type MockOriginStage struct {
	mock.Mock
	progress Progress
}

func (m *MockOriginStage) Progress() Progress {
	return m.progress
}

var _ StageProgress = (*MockOriginStage)(nil)

// RepeatResetStep is a test util that will repeat the ResetStep function until an error.
// If the step runs too many times, it will fail the test.
func RepeatResetStep(t *testing.T, step func(ctx context.Context, l1Fetcher L1Fetcher) error, l1Fetcher L1Fetcher, max int) error {
	ctx := context.Background()
	for i := 0; i < max; i++ {
		err := step(ctx, l1Fetcher)
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
	}
	t.Fatal("ran out of steps")
	return nil
}

// RepeatStep is a test util that will repeat the Step function until an error.
// If the step runs too many times, it will fail the test.
func RepeatStep(t *testing.T, step func(ctx context.Context, outer Progress) error, outer Progress, max int) error {
	ctx := context.Background()
	for i := 0; i < max; i++ {
		err := step(ctx, outer)
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
	}
	t.Fatal("ran out of steps")
	return nil
}
61 62 63 64

// TestMetrics implements the metrics used in the derivation pipeline as no-op operations.
// Optionally a test may hook into the metrics
type TestMetrics struct {
65 66 67
	recordL1Ref          func(name string, ref eth.L1BlockRef)
	recordL2Ref          func(name string, ref eth.L2BlockRef)
	recordUnsafePayloads func(length uint64, memSize uint64, next eth.BlockID)
68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

func (t *TestMetrics) RecordL1Ref(name string, ref eth.L1BlockRef) {
	if t.recordL1Ref != nil {
		t.recordL1Ref(name, ref)
	}
}

func (t *TestMetrics) RecordL2Ref(name string, ref eth.L2BlockRef) {
	if t.recordL2Ref != nil {
		t.recordL2Ref(name, ref)
	}
}

82 83 84 85 86 87
func (t *TestMetrics) RecordUnsafePayloadsBuffer(length uint64, memSize uint64, next eth.BlockID) {
	if t.recordUnsafePayloads != nil {
		t.recordUnsafePayloads(length, memSize, next)
	}
}

88
var _ Metrics = (*TestMetrics)(nil)