Skip to content

Commit

Permalink
test(integration):add update integration test
Browse files Browse the repository at this point in the history
Add test to verify the whole update flow
  • Loading branch information
hannahhoward committed Apr 20, 2020
1 parent 30b6697 commit 4645efe
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions impl/graphsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,65 @@ func TestPauseResume(t *testing.T) {

}

func TestPauseResumeViaUpdate(t *testing.T) {
// create network
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
td := newGsTestData(ctx, t)

var receivedReponseData []byte
var receivedUpdateData []byte
// initialize graphsync on first node to make requests
requestor := td.GraphSyncHost1()

requestor.RegisterIncomingResponseHook(func(p peer.ID, response graphsync.ResponseData, hookActions graphsync.IncomingResponseHookActions) {
if response.Status() == graphsync.RequestPaused {
var has bool
receivedReponseData, has = response.Extension(td.extensionName)
if has {
hookActions.UpdateRequestWithExtensions(td.extensionUpdate)
}
}
})

// setup receiving peer to just record message coming in
blockChainLength := 100
blockChain := testutil.SetupBlockChain(ctx, t, td.loader2, td.storer2, 100, blockChainLength)

// initialize graphsync on second node to response to requests
responder := td.GraphSyncHost2()
stopPoint := 50
blocksSent := 0
responder.RegisterOutgoingBlockHook(func(p peer.ID, requestData graphsync.RequestData, blockData graphsync.BlockData, hookActions graphsync.OutgoingBlockHookActions) {
_, has := requestData.Extension(td.extensionName)
if has {
blocksSent++
if blocksSent == stopPoint {
hookActions.SendExtensionData(td.extensionResponse)
hookActions.PauseResponse()
}
} else {
hookActions.TerminateWithError(errors.New("should have sent extension"))
}
})
responder.RegisterRequestUpdatedHook(func(p peer.ID, request graphsync.RequestData, update graphsync.RequestData, hookActions graphsync.RequestUpdatedHookActions) {
var has bool
receivedUpdateData, has = update.Extension(td.extensionName)
if has {
hookActions.UnpauseResponse()
}
})
progressChan, errChan := requestor.Request(ctx, td.host2.ID(), blockChain.TipLink, blockChain.Selector(), td.extension)

blockChain.VerifyWholeChain(ctx, progressChan)
testutil.VerifyEmptyErrors(ctx, t, errChan)
require.Len(t, td.blockStore1, blockChainLength, "did not store all blocks")

require.Equal(t, td.extensionResponseData, receivedReponseData, "did not receive correct extension response data")
require.Equal(t, td.extensionUpdateData, receivedUpdateData, "did not receive correct extension update data")
}

func TestGraphsyncRoundTripAlternatePersistenceAndNodes(t *testing.T) {
// create network
ctx := context.Background()
Expand Down Expand Up @@ -538,6 +597,8 @@ type gsTestData struct {
extension graphsync.ExtensionData
extensionResponseData []byte
extensionResponse graphsync.ExtensionData
extensionUpdateData []byte
extensionUpdate graphsync.ExtensionData
}

func newGsTestData(ctx context.Context, t *testing.T) *gsTestData {
Expand Down Expand Up @@ -570,6 +631,11 @@ func newGsTestData(ctx context.Context, t *testing.T) *gsTestData {
Name: td.extensionName,
Data: td.extensionResponseData,
}
td.extensionUpdateData = testutil.RandomBytes(100)
td.extensionUpdate = graphsync.ExtensionData{
Name: td.extensionName,
Data: td.extensionUpdateData,
}

return td
}
Expand Down

0 comments on commit 4645efe

Please sign in to comment.