-
Notifications
You must be signed in to change notification settings - Fork 3
/
gas_price_test.go
71 lines (58 loc) · 2.18 KB
/
gas_price_test.go
1
2
3
4
5
6
7
8
9
10
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
61
62
63
64
65
66
67
68
69
70
71
package gas
import (
"math/big"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSuggestGasPrice(t *testing.T) {
// 1. normal case, check that the value is greater or equal to 1 gwei and no error
// - it is hard to test against a raw conversion due to the API updating frequently
gasPrice, err := SuggestGasPrice(GasPriorityFast)
require.NoError(t, err)
baseline := big.NewInt(int64(1000000000))
assert.GreaterOrEqual(t, gasPrice.Cmp(baseline), 0)
// 2. check that we handle invalid priority selections
_, err = SuggestGasPrice(GasPriority("foo"))
assert.Error(t, err)
}
func TestParseGasPriceToWei(t *testing.T) {
oneGweiInGasStationUnits := 10.0
oneGweiInBaseUnits := big.NewInt(int64(1e9))
parsed, err := parseGasPriceToWei(oneGweiInGasStationUnits)
require.NoError(t, err)
assert.Equal(t, 0, oneGweiInBaseUnits.Cmp(parsed))
}
func TestLoadGasPrices(t *testing.T) {
rawPrices, err := loadGasPrices()
require.NoError(t, err)
require.GreaterOrEqual(t, rawPrices.Fastest, rawPrices.Fast)
require.GreaterOrEqual(t, rawPrices.Fast, rawPrices.Average)
require.GreaterOrEqual(t, rawPrices.Average, rawPrices.SafeLow)
require.GreaterOrEqual(t, rawPrices.SafeLow, 0.0)
}
func TestGasPriceManager(t *testing.T) {
// create "phony" negative price result so we know the cache is being used
prices := ethGasStationResponse{
Fast: -1.0,
Fastest: -1.0,
SafeLow: -1.0,
Average: -1.0,
}
mgr := gasPriceManager{
latestResponse: prices,
fetchedAt: time.Now(),
maxResultAge: 50 * time.Millisecond,
}
// 1. should use a cached result up til duration has passed
// - we can ensure a cached result is used by manually setting a cached result as -1
cachedResult, err := mgr.suggestCachedGasPrice(GasPriorityFast)
require.NoError(t, err)
assert.Equal(t, "-100000000", cachedResult.String(), "cached result should be negative since we manually set the result")
// 2. should fetch a new result after duration has passed
time.Sleep(51 * time.Millisecond)
newResult, err := mgr.suggestCachedGasPrice(GasPriorityFast)
require.NoError(t, err)
assert.Equal(t, newResult.Cmp(big.NewInt(0)), 1, "new result should be greater than 0")
}