Skip to content

Commit

Permalink
Merge pull request #172 from fasaxc/fix-ctrie-traverse
Browse files Browse the repository at this point in the history
RM-19979 Fix that Ctrie iterator did not return entries from tNodes.
  • Loading branch information
brianshannan-wf authored Aug 10, 2017
2 parents f456f73 + b4ec1df commit b9547a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions trie/ctrie/ctrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ func (c *Ctrie) traverse(i *iNode, ch chan<- *Entry, cancel <-chan struct{}) err
return errCanceled
}
}
case main.tNode != nil:
select {
case ch <- main.tNode.Entry:
case <-cancel:
return errCanceled
}
}
return nil
}
Expand Down
18 changes: 18 additions & 0 deletions trie/ctrie/ctrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,24 @@ func TestIterator(t *testing.T) {
assert.False(ok)
}

// TestIteratorCoversTNodes reproduces the scenario of a bug where tNodes weren't being traversed.
func TestIteratorCoversTNodes(t *testing.T) {
assert := assert.New(t)
ctrie := New(mockHashFactory)
// Add a pair of keys that collide (because we're using the mock hash).
ctrie.Insert([]byte("a"), true)
ctrie.Insert([]byte("b"), true)
// Delete one key, leaving exactly one sNode in the cNode. This will
// trigger creation of a tNode.
ctrie.Remove([]byte("b"))
seenKeys := map[string]bool{}
for entry := range ctrie.Iterator(nil) {
seenKeys[string(entry.Key)] = true
}
assert.Contains(seenKeys, "a", "Iterator did not return 'a'.")
assert.Len(seenKeys, 1)
}

func TestSize(t *testing.T) {
ctrie := New(nil)
for i := 0; i < 10; i++ {
Expand Down

0 comments on commit b9547a3

Please sign in to comment.