2020-07-06 20:15:13 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-05 22:57:25 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2020-07-06 20:15:13 +00:00
|
|
|
)
|
|
|
|
|
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-15 22:06:04 +00:00
|
|
|
defer sub.Unsubscribe()
|
2020-10-01 20:34:00 +00:00
|
|
|
eventCh := runSubscription(ctx, sub)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-07-06 20:15:13 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
2020-07-06 20:15:13 +00:00
|
|
|
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
2020-07-06 22:44:51 +00:00
|
|
|
events := []Event{{
|
|
|
|
Topic: testTopic,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "the-published-event-payload"},
|
2020-07-06 22:44:51 +00:00
|
|
|
}}
|
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-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
2020-10-27 18:40:06 +00:00
|
|
|
expected := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Payload: simplePayload{key: "sub-key", value: "the-published-event-payload"},
|
|
|
|
}
|
2020-10-01 20:34:00 +00:00
|
|
|
require.Equal(t, expected, next)
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
// Subscriber should not see events for other keys
|
|
|
|
publisher.Publish([]Event{{
|
|
|
|
Topic: testTopic,
|
|
|
|
Payload: simplePayload{key: "other-key", value: "this-should-not-reach-the-subscriber"},
|
|
|
|
}})
|
|
|
|
assertNoResult(t, eventCh)
|
2020-10-01 20:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var testSnapshotEvent = Event{
|
|
|
|
Topic: testTopic,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "snapshot-event-payload"},
|
2020-10-01 20:34:00 +00:00
|
|
|
Index: 1,
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:40:06 +00:00
|
|
|
type simplePayload struct {
|
2020-11-05 22:57:25 +00:00
|
|
|
key string
|
|
|
|
value string
|
|
|
|
noReadPerm bool
|
2020-10-27 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 22:57:25 +00:00
|
|
|
func (p simplePayload) HasReadPermission(acl.Authorizer) bool {
|
|
|
|
return !p.noReadPerm
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
func (p simplePayload) Subject() Subject { return Subject("default/default/" + p.key) }
|
|
|
|
|
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-10-05 16:38:38 +00:00
|
|
|
Event: es,
|
|
|
|
Err: err,
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return eventCh
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
type eventOrErr struct {
|
2020-10-05 16:38:38 +00:00
|
|
|
Event Event
|
|
|
|
Err error
|
2020-07-06 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
func getNextEvent(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)
|
2020-10-05 16:38:38 +00:00
|
|
|
return next.Event
|
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")
|
2020-10-05 16:38:38 +00:00
|
|
|
return Event{}
|
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)
|
2020-10-05 16:38:38 +00:00
|
|
|
t.Fatalf("received unexpected event: %#v", next.Event.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)
|
2020-10-15 22:06:04 +00:00
|
|
|
require.Equal(t, err, ErrSubForceClosed)
|
2020-07-16 19:26:26 +00:00
|
|
|
|
|
|
|
_, err = sub2.Next(context.Background())
|
2020-10-15 22:06:04 +00:00
|
|
|
require.Equal(t, err, ErrSubForceClosed)
|
2020-07-16 19:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func consumeSub(ctx context.Context, sub *Subscription) error {
|
|
|
|
for {
|
2020-10-05 16:38:38 +00:00
|
|
|
event, err := sub.Next(ctx)
|
2020-07-16 19:26:26 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
2020-10-05 16:38:38 +00:00
|
|
|
case event.IsEndOfSnapshot():
|
2020-07-16 19:26:26 +00:00
|
|
|
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)
|
2022-01-28 12:27:00 +00:00
|
|
|
|
2020-10-15 22:06:04 +00:00
|
|
|
sub, err := publisher.Subscribe(req)
|
2020-10-01 20:34:00 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-28 12:27:00 +00:00
|
|
|
defer sub.Unsubscribe()
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
publisher.snapshotHandlers[testTopic] = func(_ SubscribeRequest, _ SnapshotAppender) (uint64, error) {
|
|
|
|
return 0, fmt.Errorf("error should not be seen, cache should have been used")
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:06:04 +00:00
|
|
|
sub, err = publisher.Subscribe(req)
|
2020-10-01 20:34:00 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-28 12:27:00 +00:00
|
|
|
defer sub.Unsubscribe()
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
// Now subscriber should block waiting for updates
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
expected := Event{
|
2020-10-01 20:34:00 +00:00
|
|
|
Topic: testTopic,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "the-published-event-payload"},
|
2020-10-01 20:34:00 +00:00
|
|
|
Index: 3,
|
2020-10-05 16:38:38 +00:00
|
|
|
}
|
|
|
|
publisher.Publish([]Event{expected})
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
// Subscriber should see the published event
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
2020-10-01 20:34:00 +00:00
|
|
|
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)
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
simulateExistingSubscriber(t, publisher, req)
|
|
|
|
|
|
|
|
// Publish the testSnapshotEvent, to ensure that it is skipped over when
|
|
|
|
// splicing the topic buffer onto the snapshot.
|
2020-10-01 20:34:00 +00:00
|
|
|
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)
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next.Index)
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
|
|
|
Index: 3,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "event-3"},
|
2020-10-01 20:34:00 +00:00
|
|
|
}
|
|
|
|
publisher.publishEvent([]Event{expected})
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, expected, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next.Index)
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
nextEvent := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Index: 3,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "event-3"},
|
2020-10-01 20:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsNewSnapshotToFollow(), next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
simulateExistingSubscriber(t, publisher, req)
|
|
|
|
|
|
|
|
// Publish the testSnapshotEvent, to ensure that it is skipped over when
|
|
|
|
// splicing the topic buffer onto the snapshot.
|
2020-10-01 20:34:00 +00:00
|
|
|
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)
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
|
|
|
require.Equal(t, uint64(1), next.Index)
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
nextEvent := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Index: 3,
|
2020-10-27 18:40:06 +00:00
|
|
|
Payload: simplePayload{key: "sub-key", value: "event-3"},
|
2020-10-01 20:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-10-15 22:06:04 +00:00
|
|
|
defer sub.Unsubscribe()
|
2020-10-01 20:34:00 +00:00
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
2020-10-05 16:38:38 +00:00
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsNewSnapshotToFollow(), next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
2020-10-01 20:34:00 +00:00
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, nextEvent, next)
|
2020-10-01 20:34:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-16 16:54:51 +00:00
|
|
|
func TestEventPublisher_SubscribeWithIndexNotZero_NewSnapshot_WithCache(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
Index: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
nextEvent := Event{
|
|
|
|
Topic: testTopic,
|
|
|
|
Index: 3,
|
|
|
|
Payload: simplePayload{key: "sub-key", value: "event-3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers := SnapshotHandlers{
|
|
|
|
testTopic: func(req SubscribeRequest, buf SnapshotAppender) (uint64, error) {
|
|
|
|
if req.Topic != testTopic {
|
|
|
|
return 0, fmt.Errorf("unexpected topic: %v", req.Topic)
|
|
|
|
}
|
|
|
|
buf.Append([]Event{testSnapshotEvent})
|
|
|
|
buf.Append([]Event{nextEvent})
|
|
|
|
return 3, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(handlers, time.Second)
|
|
|
|
go publisher.Run(ctx)
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
simulateExistingSubscriber(t, publisher, req)
|
|
|
|
|
|
|
|
// Publish the events, to ensure they are is skipped over when splicing the
|
|
|
|
// topic buffer onto the snapshot.
|
2021-02-16 16:54:51 +00:00
|
|
|
publisher.publishEvent([]Event{testSnapshotEvent})
|
|
|
|
publisher.publishEvent([]Event{nextEvent})
|
|
|
|
|
|
|
|
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 := getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsNewSnapshotToFollow(), next)
|
|
|
|
|
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
|
|
|
|
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, nextEvent, next)
|
|
|
|
|
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot(), next)
|
|
|
|
require.Equal(t, uint64(3), next.Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
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 = 0
|
|
|
|
sub, err := publisher.Subscribe(&newReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
eventCh := runSubscription(ctx, sub)
|
|
|
|
next := getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, testSnapshotEvent, next)
|
|
|
|
|
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.Equal(t, nextEvent, next)
|
|
|
|
|
|
|
|
next = getNextEvent(t, eventCh)
|
|
|
|
require.True(t, next.IsEndOfSnapshot())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:34:00 +00:00
|
|
|
func runStep(t *testing.T, name string, fn func(t *testing.T)) {
|
2020-10-06 17:52:02 +00:00
|
|
|
t.Helper()
|
2020-10-01 20:34:00 +00:00
|
|
|
if !t.Run(name, fn) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 22:06:04 +00:00
|
|
|
|
|
|
|
func TestEventPublisher_Unsubscribe_ClosesSubscription(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second)
|
|
|
|
|
|
|
|
sub, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = sub.Next(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sub.Unsubscribe()
|
|
|
|
_, err = sub.Next(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "subscription was closed by unsubscribe")
|
|
|
|
}
|
2022-01-28 12:27:00 +00:00
|
|
|
|
|
|
|
func TestEventPublisher_Unsubscribe_FreesResourcesWhenThereAreNoSubscribers(t *testing.T) {
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topic: testTopic,
|
|
|
|
Key: "sub-key",
|
|
|
|
}
|
|
|
|
|
|
|
|
publisher := NewEventPublisher(newTestSnapshotHandlers(), time.Second)
|
|
|
|
|
|
|
|
sub1, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect a topic buffer and snapshot to have been created.
|
|
|
|
publisher.lock.Lock()
|
|
|
|
require.NotNil(t, publisher.topicBuffers[req.topicSubject()])
|
|
|
|
require.NotNil(t, publisher.snapCache[req.topicSubject()])
|
|
|
|
publisher.lock.Unlock()
|
|
|
|
|
|
|
|
// Create another subscription and close the old one, to ensure the buffer and
|
|
|
|
// snapshot stick around as long as there's at least one subscriber.
|
|
|
|
sub2, err := publisher.Subscribe(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sub1.Unsubscribe()
|
|
|
|
|
|
|
|
publisher.lock.Lock()
|
|
|
|
require.NotNil(t, publisher.topicBuffers[req.topicSubject()])
|
|
|
|
require.NotNil(t, publisher.snapCache[req.topicSubject()])
|
|
|
|
publisher.lock.Unlock()
|
|
|
|
|
|
|
|
// Close the other subscription and expect the buffer and snapshot to have
|
|
|
|
// been cleaned up.
|
|
|
|
sub2.Unsubscribe()
|
|
|
|
|
|
|
|
publisher.lock.Lock()
|
|
|
|
require.Nil(t, publisher.topicBuffers[req.topicSubject()])
|
|
|
|
require.Nil(t, publisher.snapCache[req.topicSubject()])
|
|
|
|
publisher.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulateExistingSubscriber creates a subscription that remains open throughout
|
|
|
|
// a test to prevent the topic buffer getting garbage-collected.
|
|
|
|
//
|
|
|
|
// It evicts the created snapshot from the cache immediately (simulating an
|
|
|
|
// existing subscription that has been open long enough the snapshot's TTL has
|
|
|
|
// been reached) so you can test snapshots getting created afresh.
|
|
|
|
func simulateExistingSubscriber(t *testing.T, p *EventPublisher, r *SubscribeRequest) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
sub, err := p.Subscribe(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(sub.Unsubscribe)
|
|
|
|
|
|
|
|
p.lock.Lock()
|
|
|
|
delete(p.snapCache, r.topicSubject())
|
|
|
|
p.lock.Unlock()
|
|
|
|
}
|