Skip to content

Commit

Permalink
Add test to make sure cancel works
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Aug 6, 2020
1 parent 486a590 commit e0f3626
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions syncer/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package syncer

import (
"context"
"errors"
"fmt"
"testing"
"time"

mocks "github.com/coinbase/rosetta-sdk-go/mocks/syncer"
"github.com/coinbase/rosetta-sdk-go/types"
Expand Down Expand Up @@ -407,6 +409,65 @@ func TestSync_NoReorg(t *testing.T) {
mockHandler.AssertExpectations(t)
}

func TestSync_Cancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

mockHelper := &mocks.Helper{}
mockHandler := &mocks.Handler{}
syncer := New(networkIdentifier, mockHelper, mockHandler, cancel, WithConcurrency(16))

// Force syncer to only get part of the way through the full range
mockHelper.On("NetworkStatus", ctx, networkIdentifier).Return(&types.NetworkStatusResponse{
CurrentBlockIdentifier: &types.BlockIdentifier{
Hash: "block 200",
Index: 200,
},
GenesisBlockIdentifier: &types.BlockIdentifier{
Hash: "block 0",
Index: 0,
},
}, nil).Twice()

mockHelper.On("NetworkStatus", ctx, networkIdentifier).Return(&types.NetworkStatusResponse{
CurrentBlockIdentifier: &types.BlockIdentifier{
Hash: "block 1300",
Index: 1300,
},
GenesisBlockIdentifier: &types.BlockIdentifier{
Hash: "block 0",
Index: 0,
},
}, nil).Twice()

blocks := createBlocks(0, 1200, "")
for _, b := range blocks {
mockHelper.On(
"Block",
mock.AnythingOfType("*context.cancelCtx"),
networkIdentifier,
&types.PartialBlockIdentifier{Index: &b.BlockIdentifier.Index},
).Return(
b,
nil,
).Once()
mockHandler.On(
"BlockAdded",
mock.AnythingOfType("*context.cancelCtx"),
b,
).Return(
nil,
).Once()
}

go func() {
time.Sleep(1 * time.Second)
cancel()
}()

err := syncer.Sync(ctx, -1, 1200)
assert.True(t, errors.Is(err, context.Canceled))
}

func TestSync_Reorg(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit e0f3626

Please sign in to comment.