2020-02-28 02:59:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-23 22:53:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-02-28 02:59:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeadBuffer(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("Straight push through", func(t *testing.T) {
|
|
|
|
hb := NewHeadBuffer(5)
|
2020-04-23 22:53:24 +00:00
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "1"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "2"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "3"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "4"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "5"}))
|
2020-02-28 02:59:43 +00:00
|
|
|
|
2020-04-23 22:53:24 +00:00
|
|
|
hc := hb.Push(&api.HeadChange{Type: "6"})
|
2020-02-28 02:59:43 +00:00
|
|
|
require.Equal(t, hc.Type, "1")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Reverts", func(t *testing.T) {
|
|
|
|
hb := NewHeadBuffer(5)
|
2020-04-23 22:53:24 +00:00
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "1"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "2"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "3"}))
|
2020-02-28 02:59:43 +00:00
|
|
|
hb.Pop()
|
2020-04-23 22:53:24 +00:00
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "3a"}))
|
2020-02-28 02:59:43 +00:00
|
|
|
hb.Pop()
|
2020-04-23 22:53:24 +00:00
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "3b"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "4"}))
|
|
|
|
require.Nil(t, hb.Push(&api.HeadChange{Type: "5"}))
|
2020-02-28 02:59:43 +00:00
|
|
|
|
2020-04-23 22:53:24 +00:00
|
|
|
hc := hb.Push(&api.HeadChange{Type: "6"})
|
2020-02-28 02:59:43 +00:00
|
|
|
require.Equal(t, hc.Type, "1")
|
2020-04-23 22:53:24 +00:00
|
|
|
hc = hb.Push(&api.HeadChange{Type: "7"})
|
2020-02-28 02:59:43 +00:00
|
|
|
require.Equal(t, hc.Type, "2")
|
2020-04-23 22:53:24 +00:00
|
|
|
hc = hb.Push(&api.HeadChange{Type: "8"})
|
2020-02-28 02:59:43 +00:00
|
|
|
require.Equal(t, hc.Type, "3b")
|
|
|
|
})
|
|
|
|
}
|