fix flaky test

This commit is contained in:
Ian Norden 2019-09-30 15:12:02 -05:00
parent b789ab69e6
commit 83fd76bc8a
2 changed files with 28 additions and 16 deletions

View File

@ -16,7 +16,11 @@
package mocks package mocks
import "errors" import (
"errors"
"github.com/ethereum/go-ethereum/common"
)
// DagPutter is a mock for testing the ipfs publisher // DagPutter is a mock for testing the ipfs publisher
type DagPutter struct { type DagPutter struct {
@ -29,19 +33,21 @@ func (dp *DagPutter) DagPut(raw interface{}) ([]string, error) {
return dp.CIDsToReturn, dp.ErrToReturn return dp.CIDsToReturn, dp.ErrToReturn
} }
// IncrementingDagPutter is a mock for testing the ipfs publisher // MappedDagPutter is a mock for testing the ipfs publisher
type IncrementingDagPutter struct { type MappedDagPutter struct {
CIDsToReturn []string CIDsToReturn map[common.Hash][]string
iterator int
ErrToReturn error ErrToReturn error
} }
// DagPut returns the pre-loaded CIDs or error // DagPut returns the pre-loaded CIDs or error
func (dp *IncrementingDagPutter) DagPut(raw interface{}) ([]string, error) { func (mdp *MappedDagPutter) DagPut(raw interface{}) ([]string, error) {
if len(dp.CIDsToReturn) >= dp.iterator+1 { if mdp.CIDsToReturn == nil {
cid := dp.CIDsToReturn[dp.iterator] return nil, errors.New("mapped dag putter needs to be initialized with a map of cids to return")
dp.iterator++
return []string{cid}, dp.ErrToReturn
} }
return nil, errors.New("dag putter iterator is out of range") by, ok := raw.([]byte)
if !ok {
return nil, errors.New("mapped dag putters can only dag put []byte values")
}
hash := common.BytesToHash(by)
return mdp.CIDsToReturn[hash], nil
} }

View File

@ -17,6 +17,7 @@
package ipfs_test package ipfs_test
import ( import (
"github.com/ethereum/go-ethereum/common"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
@ -28,8 +29,8 @@ var (
mockHeaderDagPutter *mocks.DagPutter mockHeaderDagPutter *mocks.DagPutter
mockTrxDagPutter *mocks.DagPutter mockTrxDagPutter *mocks.DagPutter
mockRctDagPutter *mocks.DagPutter mockRctDagPutter *mocks.DagPutter
mockStateDagPutter *mocks.IncrementingDagPutter mockStateDagPutter *mocks.MappedDagPutter
mockStorageDagPutter *mocks.IncrementingDagPutter mockStorageDagPutter *mocks.DagPutter
) )
var _ = Describe("Publisher", func() { var _ = Describe("Publisher", func() {
@ -37,8 +38,8 @@ var _ = Describe("Publisher", func() {
mockHeaderDagPutter = new(mocks.DagPutter) mockHeaderDagPutter = new(mocks.DagPutter)
mockTrxDagPutter = new(mocks.DagPutter) mockTrxDagPutter = new(mocks.DagPutter)
mockRctDagPutter = new(mocks.DagPutter) mockRctDagPutter = new(mocks.DagPutter)
mockStateDagPutter = new(mocks.IncrementingDagPutter) mockStateDagPutter = new(mocks.MappedDagPutter)
mockStorageDagPutter = new(mocks.IncrementingDagPutter) mockStorageDagPutter = new(mocks.DagPutter)
}) })
Describe("Publish", func() { Describe("Publish", func() {
@ -46,7 +47,12 @@ var _ = Describe("Publisher", func() {
mockHeaderDagPutter.CIDsToReturn = []string{"mockHeaderCID"} mockHeaderDagPutter.CIDsToReturn = []string{"mockHeaderCID"}
mockTrxDagPutter.CIDsToReturn = []string{"mockTrxCID1", "mockTrxCID2"} mockTrxDagPutter.CIDsToReturn = []string{"mockTrxCID1", "mockTrxCID2"}
mockRctDagPutter.CIDsToReturn = []string{"mockRctCID1", "mockRctCID2"} mockRctDagPutter.CIDsToReturn = []string{"mockRctCID1", "mockRctCID2"}
mockStateDagPutter.CIDsToReturn = []string{"mockStateCID1", "mockStateCID2"} val1 := common.BytesToHash(mocks.MockIPLDPayload.StateNodes[mocks.ContractLeafKey].Value)
val2 := common.BytesToHash(mocks.MockIPLDPayload.StateNodes[mocks.AnotherContractLeafKey].Value)
mockStateDagPutter.CIDsToReturn = map[common.Hash][]string{
val1: {"mockStateCID1"},
val2: {"mockStateCID2"},
}
mockStorageDagPutter.CIDsToReturn = []string{"mockStorageCID"} mockStorageDagPutter.CIDsToReturn = []string{"mockStorageCID"}
publisher := ipfs.Publisher{ publisher := ipfs.Publisher{
HeaderPutter: mockHeaderDagPutter, HeaderPutter: mockHeaderDagPutter,