Skip to content

Commit

Permalink
Merge pull request #2960 from oasislabs/kostko/fix/rt-history-test
Browse files Browse the repository at this point in the history
go/runtime/history: Fix flaky TestHistoryPrune test
  • Loading branch information
kostko authored Jun 1, 2020
2 parents b1af4b5 + b6bd2db commit 2761bf8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Empty file added .changelog/2960.trivial.md
Empty file.
6 changes: 6 additions & 0 deletions go/runtime/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (h *runtimeHistory) LastConsensusHeight() (int64, error) {
}

func (h *runtimeHistory) GetBlock(ctx context.Context, round uint64) (*block.Block, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
annBlk, err := h.db.getBlock(round)
if err != nil {
return nil, err
Expand All @@ -149,6 +152,9 @@ func (h *runtimeHistory) GetBlock(ctx context.Context, round uint64) (*block.Blo
}

func (h *runtimeHistory) GetLatestBlock(ctx context.Context) (*block.Block, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
meta, err := h.db.metadata()
if err != nil {
return nil, err
Expand Down
16 changes: 16 additions & 0 deletions go/runtime/history/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ func TestHistoryPrune(t *testing.T) {
t.Fatalf("failed to wait for prune to complete")
}

// Wait until the pruning transaction has been committed. This is needed because doneCh may be
// closed before the database transaction commits so the pruning may not yet be visible.
ctx, cancel := context.WithTimeout(context.Background(), recvTimeout)
defer cancel()
for {
_, err = history.GetBlock(ctx, 0)
if err == nil {
time.Sleep(10 * time.Millisecond)
continue
}

require.Error(err, "GetBlock should fail for pruned block 0")
require.Equal(roothash.ErrNotFound, err)
break
}

// Ensure we can only lookup the last 10 blocks.
for i := 0; i <= 50; i++ {
_, err = history.GetBlock(context.Background(), uint64(i))
Expand Down

0 comments on commit 2761bf8

Please sign in to comment.