lint, cleanup

This commit is contained in:
Roy Crihfield 2023-04-04 11:42:41 +08:00
parent b275cef114
commit 12590d2045
2 changed files with 9 additions and 32 deletions

View File

@ -19,6 +19,7 @@ package statediff_test
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"math/big" "math/big"
"math/rand" "math/rand"
"reflect" "reflect"
@ -206,7 +207,6 @@ func testErrorInBlockLoop(t *testing.T) {
}() }()
service.Loop(eventsChannel) service.Loop(eventsChannel)
// defaultParams.ComputeWatchedAddressesLeafPaths()
if !reflect.DeepEqual(builder.Params, defaultParams) { if !reflect.DeepEqual(builder.Params, defaultParams) {
t.Error("Test failure:", t.Name()) t.Error("Test failure:", t.Name())
t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams) t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams)
@ -277,7 +277,6 @@ func TestGetStateDiffAt(t *testing.T) {
t.Error(err) t.Error(err)
} }
// defaultParams.ComputeWatchedAddressesLeafPaths()
if !reflect.DeepEqual(builder.Params, defaultParams) { if !reflect.DeepEqual(builder.Params, defaultParams) {
t.Error("Test failure:", t.Name()) t.Error("Test failure:", t.Name())
t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams) t.Logf("Actual params does not equal expected.\nactual:%+v\nexpected: %+v", builder.Params, defaultParams)
@ -321,11 +320,11 @@ func subscribeWrites(ctx context.Context, svc *statediff.Service) (writeSub, err
} }
client := rpc.DialInProc(server) client := rpc.DialInProc(server)
statusChan := make(chan statediff.JobStatus) statusChan := make(chan statediff.JobStatus)
sub, err := client.Subscribe(context.Background(), "statediff", statusChan, "streamWrites") sub, err := client.Subscribe(ctx, "statediff", statusChan, "streamWrites")
return writeSub{sub, statusChan, client}, err return writeSub{sub, statusChan, client}, err
} }
func awaitJob(ws writeSub, job statediff.JobID, ctx context.Context) (bool, error) { func awaitJob(ws writeSub, job statediff.JobID, timeout time.Duration) (bool, error) {
for { for {
select { select {
case err := <-ws.sub.Err(): case err := <-ws.sub.Err():
@ -337,8 +336,8 @@ func awaitJob(ws writeSub, job statediff.JobID, ctx context.Context) (bool, erro
if status.ID == job { if status.ID == job {
return true, nil return true, nil
} }
case <-ctx.Done(): case <-time.After(timeout):
return false, ctx.Err() return false, errors.New("timeout")
} }
} }
} }
@ -358,15 +357,14 @@ func TestWriteStateDiffAt(t *testing.T) {
// delay to avoid subscription request being sent after statediff is written, // delay to avoid subscription request being sent after statediff is written,
// and timeout to prevent hanging just in case it still happens // and timeout to prevent hanging just in case it still happens
writeDelay := time.Second writeDelay := 100 * time.Millisecond
jobTimeout := time.Second jobTimeout := time.Second
ws, err := subscribeWrites(context.Background(), service) ws, err := subscribeWrites(context.Background(), service)
require.NoError(t, err) require.NoError(t, err)
defer ws.close() defer ws.close()
time.Sleep(writeDelay) time.Sleep(writeDelay)
job := service.WriteStateDiffAt(testBlock1.NumberU64(), defaultParams) job := service.WriteStateDiffAt(testBlock1.NumberU64(), defaultParams)
ctx, _ := context.WithTimeout(context.Background(), jobTimeout) ok, err := awaitJob(ws, job, jobTimeout)
ok, err := awaitJob(ws, job, ctx)
require.NoError(t, err) require.NoError(t, err)
require.True(t, ok) require.True(t, ok)

View File

@ -30,31 +30,19 @@ var _ interfaces.StateDiffIndexer = &StateDiffIndexer{}
var _ interfaces.Batch = &batch{} var _ interfaces.Batch = &batch{}
// StateDiffIndexer is a mock state diff indexer // StateDiffIndexer is a mock state diff indexer
type StateDiffIndexer struct { type StateDiffIndexer struct{}
StateNodes []sdtypes.StateLeafNode
IPLDs []sdtypes.IPLD
}
type batch struct { type batch struct{}
sdi *StateDiffIndexer
StateNodes []sdtypes.StateLeafNode
IPLDs []sdtypes.IPLD
}
func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receipts, totalDifficulty *big.Int) (interfaces.Batch, error) { func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receipts, totalDifficulty *big.Int) (interfaces.Batch, error) {
return &batch{}, nil return &batch{}, nil
} }
func (sdi *StateDiffIndexer) PushStateNode(txi interfaces.Batch, stateNode sdtypes.StateLeafNode, headerID string) error { func (sdi *StateDiffIndexer) PushStateNode(txi interfaces.Batch, stateNode sdtypes.StateLeafNode, headerID string) error {
tx := txi.(*batch)
tx.StateNodes = append(tx.StateNodes, stateNode)
return nil return nil
} }
func (sdi *StateDiffIndexer) PushIPLD(txi interfaces.Batch, ipld sdtypes.IPLD) error { func (sdi *StateDiffIndexer) PushIPLD(txi interfaces.Batch, ipld sdtypes.IPLD) error {
tx := txi.(*batch)
tx.IPLDs = append(tx.IPLDs, ipld)
return nil return nil
} }
@ -85,14 +73,5 @@ func (sdi *StateDiffIndexer) Close() error {
} }
func (tx *batch) Submit(err error) error { func (tx *batch) Submit(err error) error {
if err != nil {
return err
}
for _, sn := range tx.StateNodes {
tx.sdi.StateNodes = append(tx.sdi.StateNodes, sn)
}
for _, ipld := range tx.IPLDs {
tx.sdi.IPLDs = append(tx.sdi.IPLDs, ipld)
}
return nil return nil
} }