2020-06-02 22:37:10 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
fmt "fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
time "time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-06-05 23:36:31 +00:00
|
|
|
// A property-based test to ensure that under heavy concurrent use trivial
|
|
|
|
// correctness properties are not violated (and that -race doesn't complain).
|
2020-06-02 22:37:10 +00:00
|
|
|
func TestEventBufferFuzz(t *testing.T) {
|
2020-06-05 23:36:31 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for short run")
|
|
|
|
}
|
2020-06-02 22:37:10 +00:00
|
|
|
|
|
|
|
nReaders := 1000
|
|
|
|
nMessages := 1000
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
b := newEventBuffer()
|
2020-06-02 22:37:10 +00:00
|
|
|
|
|
|
|
// Start a write goroutine that will publish 10000 messages with sequential
|
|
|
|
// indexes and some jitter in timing (to allow clients to "catch up" and block
|
|
|
|
// waiting for updates).
|
|
|
|
go func() {
|
2020-06-15 20:18:07 +00:00
|
|
|
seed := time.Now().UnixNano()
|
|
|
|
t.Logf("Using seed %d", seed)
|
2020-06-02 22:37:10 +00:00
|
|
|
// z is a Zipfian distribution that gives us a number of milliseconds to
|
|
|
|
// sleep which are mostly low - near zero but occasionally spike up to near
|
|
|
|
// 100.
|
2020-06-15 20:18:07 +00:00
|
|
|
z := rand.NewZipf(rand.New(rand.NewSource(seed)), 1.5, 1.5, 50)
|
2020-06-02 22:37:10 +00:00
|
|
|
|
|
|
|
for i := 0; i < nMessages; i++ {
|
|
|
|
// Event content is arbitrary and not valid for our use of buffers in
|
|
|
|
// streaming - here we only care about the semantics of the buffer.
|
2020-06-05 23:36:31 +00:00
|
|
|
e := Event{
|
|
|
|
Index: uint64(i), // Indexes should be contiguous
|
2020-07-07 00:04:24 +00:00
|
|
|
Topic: testTopic,
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-06-05 23:36:31 +00:00
|
|
|
b.Append([]Event{e})
|
2020-06-02 22:37:10 +00:00
|
|
|
// Sleep sometimes for a while to let some subscribers catch up
|
|
|
|
wait := time.Duration(z.Uint64()) * time.Millisecond
|
|
|
|
time.Sleep(wait)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Run n subscribers following and verifying
|
|
|
|
errCh := make(chan error, nReaders)
|
|
|
|
|
|
|
|
// Load head here so all subscribers start from the same point or they might
|
2020-07-08 18:45:18 +00:00
|
|
|
// not run until several appends have already happened.
|
2020-06-02 22:37:10 +00:00
|
|
|
head := b.Head()
|
|
|
|
|
|
|
|
for i := 0; i < nReaders; i++ {
|
|
|
|
go func(i int) {
|
|
|
|
expect := uint64(0)
|
|
|
|
item := head
|
|
|
|
var err error
|
|
|
|
for {
|
2020-07-08 04:31:22 +00:00
|
|
|
item, err = item.Next(context.Background(), nil)
|
2020-06-02 22:37:10 +00:00
|
|
|
if err != nil {
|
|
|
|
errCh <- fmt.Errorf("subscriber %05d failed getting next %d: %s", i,
|
|
|
|
expect, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if item.Events[0].Index != expect {
|
|
|
|
errCh <- fmt.Errorf("subscriber %05d got bad event want=%d, got=%d", i,
|
|
|
|
expect, item.Events[0].Index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expect++
|
|
|
|
if expect == uint64(nMessages) {
|
|
|
|
// Succeeded
|
|
|
|
errCh <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all readers to finish one way or other
|
|
|
|
for i := 0; i < nReaders; i++ {
|
|
|
|
err := <-errCh
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|