|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/rs/zerolog" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewGasOracle(t *testing.T) { |
| 13 | + t.Run("creates gas oracle with valid params", func(t *testing.T) { |
| 14 | + logger := zerolog.Nop() |
| 15 | + chainID := "eip155:1" |
| 16 | + interval := 30 |
| 17 | + |
| 18 | + oracle := NewGasOracle(nil, nil, chainID, interval, logger) |
| 19 | + |
| 20 | + require.NotNil(t, oracle) |
| 21 | + assert.Equal(t, chainID, oracle.chainID) |
| 22 | + assert.Equal(t, interval, oracle.gasPriceIntervalSeconds) |
| 23 | + assert.Nil(t, oracle.rpcClient) |
| 24 | + assert.Nil(t, oracle.pushSigner) |
| 25 | + assert.NotNil(t, oracle.stopCh) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("creates gas oracle with different chain IDs", func(t *testing.T) { |
| 29 | + logger := zerolog.Nop() |
| 30 | + |
| 31 | + testCases := []string{ |
| 32 | + "eip155:1", |
| 33 | + "eip155:97", |
| 34 | + "eip155:137", |
| 35 | + "eip155:42161", |
| 36 | + } |
| 37 | + |
| 38 | + for _, chainID := range testCases { |
| 39 | + oracle := NewGasOracle(nil, nil, chainID, 30, logger) |
| 40 | + assert.Equal(t, chainID, oracle.chainID) |
| 41 | + } |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func TestGasOracleGetGasOracleFetchInterval(t *testing.T) { |
| 46 | + logger := zerolog.Nop() |
| 47 | + |
| 48 | + t.Run("returns configured interval", func(t *testing.T) { |
| 49 | + oracle := NewGasOracle(nil, nil, "eip155:1", 60, logger) |
| 50 | + interval := oracle.getGasOracleFetchInterval() |
| 51 | + assert.Equal(t, 60*time.Second, interval) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("returns default for zero interval", func(t *testing.T) { |
| 55 | + oracle := NewGasOracle(nil, nil, "eip155:1", 0, logger) |
| 56 | + interval := oracle.getGasOracleFetchInterval() |
| 57 | + assert.Equal(t, 30*time.Second, interval) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("returns default for negative interval", func(t *testing.T) { |
| 61 | + oracle := NewGasOracle(nil, nil, "eip155:1", -10, logger) |
| 62 | + interval := oracle.getGasOracleFetchInterval() |
| 63 | + assert.Equal(t, 30*time.Second, interval) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("respects custom intervals", func(t *testing.T) { |
| 67 | + testCases := []struct { |
| 68 | + input int |
| 69 | + expected time.Duration |
| 70 | + }{ |
| 71 | + {10, 10 * time.Second}, |
| 72 | + {30, 30 * time.Second}, |
| 73 | + {60, 60 * time.Second}, |
| 74 | + {120, 120 * time.Second}, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range testCases { |
| 78 | + oracle := NewGasOracle(nil, nil, "eip155:1", tc.input, logger) |
| 79 | + interval := oracle.getGasOracleFetchInterval() |
| 80 | + assert.Equal(t, tc.expected, interval, "interval %d should result in %v", tc.input, tc.expected) |
| 81 | + } |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func TestGasOracleStop(t *testing.T) { |
| 86 | + t.Run("stop waits for goroutine", func(t *testing.T) { |
| 87 | + logger := zerolog.Nop() |
| 88 | + oracle := NewGasOracle(nil, nil, "eip155:1", 30, logger) |
| 89 | + |
| 90 | + // Should not panic or hang |
| 91 | + oracle.Stop() |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func TestGasOracleStruct(t *testing.T) { |
| 96 | + t.Run("struct has expected fields", func(t *testing.T) { |
| 97 | + oracle := &GasOracle{} |
| 98 | + assert.Nil(t, oracle.rpcClient) |
| 99 | + assert.Nil(t, oracle.pushSigner) |
| 100 | + assert.Empty(t, oracle.chainID) |
| 101 | + assert.Equal(t, 0, oracle.gasPriceIntervalSeconds) |
| 102 | + assert.Nil(t, oracle.stopCh) |
| 103 | + }) |
| 104 | +} |
0 commit comments