2020-07-06 20:15:13 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
type intTopic int
|
|
|
|
|
|
|
|
func (i intTopic) String() string {
|
|
|
|
return fmt.Sprintf("%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testTopic Topic = intTopic(999)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
func TestEventPublisher_SubscribeWithIndex0(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
2020-07-06 20:15:13 +00:00
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-09-09 20:26:11 +00:00
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), 0)
|
|
|
|
go publisher.Run(ctx)
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
sub, err := publisher.Subscribe(req)
|
2020-07-06 20:15:13 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-01 20:34:00 +00:00
|
|
|
eventCh := runSubscription(ctx, sub)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
expected := []Event{testSnapshotEvent}
|
|
|
|
require.Equal(t, expected, next)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Len(t, next, 1)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
2020-07-06 20:15:13 +00:00
|
|
|
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
2020-07-06 22:44:51 +00:00
|
|
|
events := []Event{{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Payload: "the-published-event-payload",
|
|
|
|
}}
|
2020-07-08 04:31:22 +00:00
|
|
|
publisher.Publish(events)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
|
|
|
// Subscriber should see the published event
|
2020-10-01 20:34:00 +00:00
|
|
|
next = getNextEvents(t, eventCh)
|
2020-07-06 20:15:13 +00:00
|
|
|
expected = []Event{{Payload: "the-published-event-payload", Key: "sub-key", Topic: testTopic}}
|
2020-10-01 20:34:00 +00:00
|
|
|
require.Equal(t, expected, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testSnapshotEvent = Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Payload: "snapshot-event-payload",
|
|
|
|
Key: "sub-key",
|
|
|
|
Index: 1,
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 22:44:51 +00:00
|
|
|
func newTestSnapshotHandlers() SnapshotHandlers {
|
|
|
|
return SnapshotHandlers{
|
2020-07-14 23:23:44 +00:00
|
|
|
testTopic: func(req SubscribeRequest, buf SnapshotAppender) (uint64, error) {
|
2020-07-06 22:44:51 +00:00
|
|
|
if req.Topic != testTopic {
|
|
|
|
return 0, fmt.Errorf("unexpected topic: %v", req.Topic)
|
|
|
|
}
|
2020-10-01 20:34:00 +00:00
|
|
|
buf.Append([]Event{testSnapshotEvent})
|
2020-07-06 22:44:51 +00:00
|
|
|
return 1, nil
|
2020-07-06 20:15:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
func runSubscription(ctx context.Context, sub *Subscription) <-chan eventOrErr {
|
|
|
|
eventCh := make(chan eventOrErr, 1)
|
2020-07-06 20:15:13 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2020-07-08 04:31:22 +00:00
|
|
|
es, err := sub.Next(ctx)
|
2020-10-01 20:34:00 +00:00
|
|
|
eventCh <- eventOrErr{
|
2020-07-06 20:15:13 +00:00
|
|
|
Events: es,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return eventCh
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
type eventOrErr struct {
|
2020-07-06 20:15:13 +00:00
|
|
|
Events []Event
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
func getNextEvents(t *testing.T, eventCh <-chan eventOrErr) []Event {
|
2020-07-06 20:15:13 +00:00
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case next := <-eventCh:
|
2020-10-01 20:34:00 +00:00
|
|
|
require.NoError(t, next.Err)
|
|
|
|
return next.Events
|
2020-07-06 20:15:13 +00:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2020-10-01 20:34:00 +00:00
|
|
|
t.Fatalf("timeout waiting for event from subscription")
|
|
|
|
return nil
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
func assertNoResult(t *testing.T, eventCh <-chan eventOrErr) {
|
2020-07-06 20:15:13 +00:00
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case next := <-eventCh:
|
|
|
|
require.NoError(t, next.Err)
|
|
|
|
require.Len(t, next.Events, 1)
|
|
|
|
t.Fatalf("received unexpected event: %#v", next.Events[0].Payload)
|
2020-10-01 20:34:00 +00:00
|
|
|
case <-time.After(25 * time.Millisecond):
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 19:26:26 +00:00
|
|
|
|
|
|
|
func TestEventPublisher_ShutdownClosesSubscriptions(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
handlers := newTestSnapshotHandlers()
|
2020-07-14 23:23:44 +00:00
|
|
|
fn := func(req SubscribeRequest, buf SnapshotAppender) (uint64, error) {
|
2020-07-16 19:26:26 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
handlers[intTopic(22)] = fn
|
|
|
|
handlers[intTopic(33)] = fn
|
|
|
|
|
2020-09-09 20:26:11 +00:00
|
|
|
publisher := NewEventPublisher(handlers, time.Second)
|
|
|
|
go publisher.Run(ctx)
|
2020-07-16 19:26:26 +00:00
|
|
|
|
|
|
|
sub1, err := publisher.Subscribe(&SubscribeRequest{Topic: intTopic(22)})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub1.Unsubscribe()
|
|
|
|
|
|
|
|
sub2, err := publisher.Subscribe(&SubscribeRequest{Topic: intTopic(33)})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub2.Unsubscribe()
|
|
|
|
|
|
|
|
cancel() // Shutdown
|
|
|
|
|
|
|
|
err = consumeSub(context.Background(), sub1)
|
|
|
|
require.Equal(t, err, ErrSubscriptionClosed)
|
|
|
|
|
|
|
|
_, err = sub2.Next(context.Background())
|
|
|
|
require.Equal(t, err, ErrSubscriptionClosed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func consumeSub(ctx context.Context, sub *Subscription) error {
|
|
|
|
for {
|
|
|
|
events, err := sub.Next(ctx)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case len(events) == 1 && events[0].IsEndOfSnapshot():
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
func TestEventPublisher_SubscribeWithIndex0_FromCache(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second)
|
|
|
|
go publisher.Run(ctx)
|
|
|
|
_, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
publisher.snapshotHandlers[testTopic] = func(_ SubscribeRequest, _ SnapshotAppender) (uint64, error) {
|
|
|
|
return 0, fmt.Errorf("error should not be seen, cache should have been used")
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
expected := []Event{testSnapshotEvent}
|
|
|
|
require.Equal(t, expected, next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Len(t, next, 1)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
|
|
|
|
// Now subscriber should block waiting for updates
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
|
|
|
events := []Event{{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Payload: "the-published-event-payload",
|
|
|
|
Index: 3,
|
|
|
|
}}
|
|
|
|
publisher.Publish(events)
|
|
|
|
|
|
|
|
// Subscriber should see the published event
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
expected = []Event{events[0]}
|
|
|
|
require.Equal(t, expected, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventPublisher_SubscribeWithIndexNotZero_CanResume(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second)
|
|
|
|
go publisher.Run(ctx)
|
|
|
|
// Include the same event in the topicBuffer
|
|
|
|
publisher.publishEvent([]Event{testSnapshotEvent})
|
|
|
|
|
|
|
|
runStep(t, "start a subscription and unsub", func(t *testing.T) {
|
|
|
|
sub, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
expected := []Event{testSnapshotEvent}
|
|
|
|
require.Equal(t, expected, next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Len(t, next, 1)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next[0].Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
runStep(t, "resume the subscription", func(t *testing.T) {
|
|
|
|
newReq := *req
|
|
|
|
newReq.Index = 1
|
|
|
|
sub, err := publisher.Subscribe(&newReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
|
|
|
expected := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Index: 3,
|
|
|
|
Payload: "event-3",
|
|
|
|
}
|
|
|
|
publisher.publishEvent([]Event{expected})
|
|
|
|
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
require.Equal(t, []Event{expected}, next)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventPublisher_SubscribeWithIndexNotZero_NewSnapshot(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), 0)
|
|
|
|
go publisher.Run(ctx)
|
|
|
|
// Include the same event in the topicBuffer
|
|
|
|
publisher.publishEvent([]Event{testSnapshotEvent})
|
|
|
|
|
|
|
|
runStep(t, "start a subscription and unsub", func(t *testing.T) {
|
|
|
|
sub, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
expected := []Event{testSnapshotEvent}
|
|
|
|
require.Equal(t, expected, next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Len(t, next, 1)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next[0].Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
nextEvent := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Index: 3,
|
|
|
|
Payload: "event-3",
|
|
|
|
}
|
|
|
|
|
|
|
|
runStep(t, "publish an event while unsubed", func(t *testing.T) {
|
|
|
|
publisher.publishEvent([]Event{nextEvent})
|
|
|
|
})
|
|
|
|
|
|
|
|
runStep(t, "resume the subscription", func(t *testing.T) {
|
|
|
|
newReq := *req
|
|
|
|
newReq.Index = 1
|
|
|
|
sub, err := publisher.Subscribe(&newReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
require.True(t, next[0].IsNewSnapshotToFollow(), next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next[0])
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventPublisher_SubscribeWithIndexNotZero_NewSnapshotFromCache(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second)
|
|
|
|
go publisher.Run(ctx)
|
|
|
|
// Include the same event in the topicBuffer
|
|
|
|
publisher.publishEvent([]Event{testSnapshotEvent})
|
|
|
|
|
|
|
|
runStep(t, "start a subscription and unsub", func(t *testing.T) {
|
|
|
|
sub, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
expected := []Event{testSnapshotEvent}
|
|
|
|
require.Equal(t, expected, next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Len(t, next, 1)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next[0].Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
nextEvent := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Index: 3,
|
|
|
|
Payload: "event-3",
|
|
|
|
}
|
|
|
|
|
|
|
|
runStep(t, "publish an event while unsubed", func(t *testing.T) {
|
|
|
|
publisher.publishEvent([]Event{nextEvent})
|
|
|
|
})
|
|
|
|
|
|
|
|
publisher.snapshotHandlers[testTopic] = func(_ SubscribeRequest, _ SnapshotAppender) (uint64, error) {
|
|
|
|
return 0, fmt.Errorf("error should not be seen, cache should have been used")
|
|
|
|
}
|
|
|
|
|
|
|
|
runStep(t, "resume the subscription", func(t *testing.T) {
|
|
|
|
newReq := *req
|
|
|
|
newReq.Index = 1
|
|
|
|
sub, err := publisher.Subscribe(&newReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
next := getNextEvents(t, eventCh)
|
|
|
|
require.True(t, next[0].IsNewSnapshotToFollow(), next)
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next[0])
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.True(t, next[0].IsEndOfSnapshot())
|
|
|
|
|
|
|
|
next = getNextEvents(t, eventCh)
|
|
|
|
require.Equal(t, nextEvent, next[0])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func runStep(t *testing.T, name string, fn func(t *testing.T)) {
|
|
|
|
if !t.Run(name, fn) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|