[bugfix] flaky paging test (#2888)
This commit is contained in:
parent
a8254a40e7
commit
eb61c783ed
|
@ -20,16 +20,14 @@ package paging_test
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/oklog/ulid"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/paging"
|
"github.com/superseriousbusiness/gotosocial/internal/paging"
|
||||||
)
|
)
|
||||||
|
|
||||||
// random reader according to current-time source seed.
|
|
||||||
var randRd = rand.New(rand.NewSource(time.Now().Unix()))
|
|
||||||
|
|
||||||
type Case struct {
|
type Case struct {
|
||||||
// Name is the test case name.
|
// Name is the test case name.
|
||||||
Name string
|
Name string
|
||||||
|
@ -63,13 +61,9 @@ func TestPage(t *testing.T) {
|
||||||
// Page the input slice.
|
// Page the input slice.
|
||||||
out := c.Page.Page(c.Input)
|
out := c.Page.Page(c.Input)
|
||||||
|
|
||||||
// Log the results for case of error returns.
|
// Check paged output is expected.
|
||||||
t.Logf("%s\npage=%+v input=%v expect=%v output=%v", c.Name, c.Page, c.Input, c.Expect, out)
|
assert.Equal(t, c.Expect, out,
|
||||||
|
"input=%#v page=%v", c.Input, c.Page)
|
||||||
// Check paged output is as expected.
|
|
||||||
if !slices.Equal(out, c.Expect) {
|
|
||||||
t.Error("unexpected paged output")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,8 +74,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, ascending)
|
slices.SortFunc(ids, ascending)
|
||||||
|
|
||||||
// Select random indices in slice.
|
// Select random indices in slice.
|
||||||
minIdx := randRd.Intn(len(ids))
|
minIdx, maxIdx, _ := generateParams(len(ids))
|
||||||
maxIdx := randRd.Intn(len(ids))
|
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
minID := ids[minIdx]
|
minID := ids[minIdx]
|
||||||
|
@ -104,9 +97,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, ascending)
|
slices.SortFunc(ids, ascending)
|
||||||
|
|
||||||
// Select random parameters in slice.
|
// Select random parameters in slice.
|
||||||
minIdx := randRd.Intn(len(ids))
|
minIdx, maxIdx, limit := generateParams(len(ids))
|
||||||
maxIdx := randRd.Intn(len(ids))
|
|
||||||
limit := randRd.Intn(len(ids)) + 1
|
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
minID := ids[minIdx]
|
minID := ids[minIdx]
|
||||||
|
@ -116,12 +107,10 @@ var cases = []Case{
|
||||||
expect := slices.Clone(ids)
|
expect := slices.Clone(ids)
|
||||||
expect = cutLower(expect, minID)
|
expect = cutLower(expect, minID)
|
||||||
expect = cutUpper(expect, maxID)
|
expect = cutUpper(expect, maxID)
|
||||||
slices.Reverse(expect)
|
|
||||||
|
|
||||||
// Now limit the slice.
|
|
||||||
if limit < len(expect) {
|
if limit < len(expect) {
|
||||||
expect = expect[:limit]
|
expect = expect[:limit]
|
||||||
}
|
}
|
||||||
|
slices.Reverse(expect)
|
||||||
|
|
||||||
// Return page and expected IDs.
|
// Return page and expected IDs.
|
||||||
return ids, &paging.Page{
|
return ids, &paging.Page{
|
||||||
|
@ -135,8 +124,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, ascending)
|
slices.SortFunc(ids, ascending)
|
||||||
|
|
||||||
// Select random parameters in slice.
|
// Select random parameters in slice.
|
||||||
minIdx := randRd.Intn(len(ids))
|
minIdx, maxIdx, _ := generateParams(len(ids))
|
||||||
maxIdx := randRd.Intn(len(ids))
|
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
minID := ids[minIdx]
|
minID := ids[minIdx]
|
||||||
|
@ -160,8 +148,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, descending)
|
slices.SortFunc(ids, descending)
|
||||||
|
|
||||||
// Select random indices in slice.
|
// Select random indices in slice.
|
||||||
sinceIdx := randRd.Intn(len(ids))
|
sinceIdx, maxIdx, _ := generateParams(len(ids))
|
||||||
maxIdx := randRd.Intn(len(ids))
|
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
sinceID := ids[sinceIdx]
|
sinceID := ids[sinceIdx]
|
||||||
|
@ -183,7 +170,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, descending)
|
slices.SortFunc(ids, descending)
|
||||||
|
|
||||||
// Select random indices in slice.
|
// Select random indices in slice.
|
||||||
maxIdx := randRd.Intn(len(ids))
|
_, maxIdx, _ := generateParams(len(ids))
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
maxID := ids[maxIdx]
|
maxID := ids[maxIdx]
|
||||||
|
@ -202,7 +189,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, descending)
|
slices.SortFunc(ids, descending)
|
||||||
|
|
||||||
// Select random indices in slice.
|
// Select random indices in slice.
|
||||||
sinceIdx := randRd.Intn(len(ids))
|
sinceIdx, _, _ := generateParams(len(ids))
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
sinceID := ids[sinceIdx]
|
sinceID := ids[sinceIdx]
|
||||||
|
@ -221,7 +208,7 @@ var cases = []Case{
|
||||||
slices.SortFunc(ids, ascending)
|
slices.SortFunc(ids, ascending)
|
||||||
|
|
||||||
// Select random indices in slice.
|
// Select random indices in slice.
|
||||||
minIdx := randRd.Intn(len(ids))
|
minIdx, _, _ := generateParams(len(ids))
|
||||||
|
|
||||||
// Select the boundaries.
|
// Select the boundaries.
|
||||||
minID := ids[minIdx]
|
minID := ids[minIdx]
|
||||||
|
@ -258,32 +245,34 @@ func cutUpper(in []string, bound string) []string {
|
||||||
return in
|
return in
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// random reader according to current-time source seed.
|
||||||
|
var randRd = rand.New(rand.NewSource(time.Now().Unix()))
|
||||||
|
|
||||||
|
// generateParams ...
|
||||||
|
func generateParams(n int) (minIdx int, maxIdx int, limit int) {
|
||||||
|
maxIdx = max(1, randRd.Intn(n))
|
||||||
|
minIdx = randRd.Intn(maxIdx)
|
||||||
|
limit = randRd.Intn(max(1, maxIdx-minIdx)) + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// generateSlice generates a new slice of len containing ascending sorted slice.
|
// generateSlice generates a new slice of len containing ascending sorted slice.
|
||||||
func generateSlice(len int) []string {
|
func generateSlice(len int) []string {
|
||||||
if len <= 0 {
|
if len <= 1 {
|
||||||
// minimum testable
|
// minimum testable
|
||||||
// pageable amount
|
// pageable amount
|
||||||
len = 2
|
len = 2
|
||||||
}
|
}
|
||||||
now := time.Now()
|
|
||||||
in := make([]string, len)
|
in := make([]string, len)
|
||||||
for i := 0; i < len; i++ {
|
for i := 0; i < len; i++ {
|
||||||
// Convert now to timestamp.
|
in[i] = strconv.Itoa(i)
|
||||||
t := ulid.Timestamp(now)
|
|
||||||
|
|
||||||
// Create anew ulid for now.
|
|
||||||
u := ulid.MustNew(t, randRd)
|
|
||||||
|
|
||||||
// Add to slice.
|
|
||||||
in[i] = u.String()
|
|
||||||
|
|
||||||
// Bump now by 1 second.
|
|
||||||
now = now.Add(time.Second)
|
|
||||||
}
|
}
|
||||||
return in
|
return in
|
||||||
}
|
}
|
||||||
|
|
||||||
func ascending(a, b string) int {
|
func ascending(sa, sb string) int {
|
||||||
|
a, _ := strconv.Atoi(sa)
|
||||||
|
b, _ := strconv.Atoi(sb)
|
||||||
if a > b {
|
if a > b {
|
||||||
return 1
|
return 1
|
||||||
} else if a < b {
|
} else if a < b {
|
||||||
|
@ -292,7 +281,9 @@ func ascending(a, b string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func descending(a, b string) int {
|
func descending(sa, sb string) int {
|
||||||
|
a, _ := strconv.Atoi(sa)
|
||||||
|
b, _ := strconv.Atoi(sb)
|
||||||
if a < b {
|
if a < b {
|
||||||
return 1
|
return 1
|
||||||
} else if a > b {
|
} else if a > b {
|
||||||
|
|
Loading…
Reference in New Issue