(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestReactor_Receive_ChunkRequest(t *testing.T) { |
| 21 | testcases := map[string]struct { |
| 22 | request *ssproto.ChunkRequest |
| 23 | chunk []byte |
| 24 | expectResponse *ssproto.ChunkResponse |
| 25 | }{ |
| 26 | "chunk is returned": { |
| 27 | &ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, |
| 28 | []byte{1, 2, 3}, |
| 29 | &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: []byte{1, 2, 3}}}, |
| 30 | "empty chunk is returned, as nil": { |
| 31 | &ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, |
| 32 | []byte{}, |
| 33 | &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Chunk: nil}}, |
| 34 | "nil (missing) chunk is returned as missing": { |
| 35 | &ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1}, |
| 36 | nil, |
| 37 | &ssproto.ChunkResponse{Height: 1, Format: 1, Index: 1, Missing: true}, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | for name, tc := range testcases { |
| 42 | tc := tc |
| 43 | t.Run(name, func(t *testing.T) { |
| 44 | // Mock ABCI connection to return local snapshots |
| 45 | conn := &proxymocks.AppConnSnapshot{} |
| 46 | conn.On("LoadSnapshotChunkSync", abci.RequestLoadSnapshotChunk{ |
| 47 | Height: tc.request.Height, |
| 48 | Format: tc.request.Format, |
| 49 | Chunk: tc.request.Index, |
| 50 | }).Return(&abci.ResponseLoadSnapshotChunk{Chunk: tc.chunk}, nil) |
| 51 | |
| 52 | // Mock peer to store response, if found |
| 53 | peer := &p2pmocks.Peer{} |
| 54 | peer.On("ID").Return(p2p.ID("id")) |
| 55 | var response *ssproto.ChunkResponse |
| 56 | if tc.expectResponse != nil { |
| 57 | peer.On("SendEnvelope", mock.MatchedBy(func(i interface{}) bool { |
| 58 | e, ok := i.(p2p.Envelope) |
| 59 | return ok && e.ChannelID == ChunkChannel |
| 60 | })).Run(func(args mock.Arguments) { |
| 61 | e := args[0].(p2p.Envelope) |
| 62 | |
| 63 | // Marshal to simulate a wire roundtrip. |
| 64 | bz, err := proto.Marshal(e.Message) |
| 65 | require.NoError(t, err) |
| 66 | err = proto.Unmarshal(bz, e.Message) |
| 67 | require.NoError(t, err) |
| 68 | response = e.Message.(*ssproto.ChunkResponse) |
| 69 | }).Return(true) |
| 70 | } |
| 71 | |
| 72 | // Start a reactor and send a ssproto.ChunkRequest, then wait for and check response |
| 73 | cfg := config.DefaultStateSyncConfig() |
| 74 | r := NewReactor(*cfg, conn, nil, "") |
| 75 | err := r.Start() |
| 76 | require.NoError(t, err) |
| 77 | t.Cleanup(func() { |
nothing calls this directly
no test coverage detected