go-zero/core/collection/ring_test.go

32 lines
537 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package collection
import (
"testing"
"github.com/stretchr/testify/assert"
)
2020-10-15 14:25:10 +08:00
func TestNewRing(t *testing.T) {
assert.Panics(t, func() {
NewRing(0)
})
}
2020-07-26 17:09:05 +08:00
func TestRingLess(t *testing.T) {
ring := NewRing(5)
for i := 0; i < 3; i++ {
ring.Add(i)
}
elements := ring.Take()
assert.ElementsMatch(t, []interface{}{0, 1, 2}, elements)
}
func TestRingMore(t *testing.T) {
ring := NewRing(5)
for i := 0; i < 11; i++ {
ring.Add(i)
}
elements := ring.Take()
assert.ElementsMatch(t, []interface{}{6, 7, 8, 9, 10}, elements)
}