wtclient: demo addr iterator panic

This commit adds a test that shows that it is possible to cause the
AddressIterator to panic if the `Next` method is ever called twice when
the iterator is at the end of its list without Reset first being called.
This commit is contained in:
Elle Mouton 2023-03-30 11:45:34 +02:00
parent de80fffa6c
commit 1047514515
No known key found for this signature in database
GPG key ID: D7D916376026F177

View file

@ -216,4 +216,27 @@ func TestAddrIterator(t *testing.T) {
iter.ReleaseLock(addr3)
require.False(t, iter.HasLocked())
})
t.Run("calling Next twice without Reset panics", func(t *testing.T) {
t.Parallel()
// This defer-function asserts that a panic does occur.
defer func() {
r := recover()
require.NotNilf(t, r, "the code did not panic")
}()
// Initialise the iterator with addr1.
iter, err := newAddressIterator(addr1)
require.NoError(t, err)
a1 := iter.Peek()
require.Equal(t, addr1, a1)
_, err = iter.Next()
require.ErrorIs(t, err, ErrAddressesExhausted)
_, err = iter.Next()
require.ErrorIs(t, err, ErrAddressesExhausted)
})
}