2020-08-31 17:19:28 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-08 18:27:52 +00:00
|
|
|
"sync/atomic"
|
2020-08-31 17:19:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-06 20:21:58 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
|
2020-08-31 17:19:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
func TestEventBroker_PublishChangesAndSubscribe(t *testing.T) {
|
2020-08-31 17:19:28 +00:00
|
|
|
subscription := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 18:27:52 +00:00
|
|
|
"Test": {"sub-key"},
|
2020-08-31 17:19:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
publisher := NewEventBroker(ctx, EventBrokerCfg{EventBufferSize: 100})
|
2020-08-31 17:19:28 +00:00
|
|
|
sub, err := publisher.Subscribe(subscription)
|
|
|
|
require.NoError(t, err)
|
|
|
|
eventCh := consumeSubscription(ctx, sub)
|
|
|
|
|
|
|
|
// Now subscriber should block waiting for updates
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
events := []structs.Event{{
|
2020-08-31 17:19:28 +00:00
|
|
|
Index: 1,
|
|
|
|
Topic: "Test",
|
|
|
|
Key: "sub-key",
|
|
|
|
Payload: "sample payload",
|
|
|
|
}}
|
2020-10-06 20:21:58 +00:00
|
|
|
publisher.Publish(&structs.Events{Index: 1, Events: events})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
// Subscriber should see the published event
|
|
|
|
result := nextResult(t, eventCh)
|
|
|
|
require.NoError(t, result.Err)
|
2020-10-04 19:12:35 +00:00
|
|
|
expected := []structs.Event{{Payload: "sample payload", Key: "sub-key", Topic: "Test", Index: 1}}
|
2020-08-31 17:19:28 +00:00
|
|
|
require.Equal(t, expected, result.Events)
|
|
|
|
|
|
|
|
// Now subscriber should block waiting for updates
|
|
|
|
assertNoResult(t, eventCh)
|
|
|
|
|
|
|
|
// Publish a second event
|
2020-10-04 19:12:35 +00:00
|
|
|
events = []structs.Event{{
|
2020-08-31 17:19:28 +00:00
|
|
|
Index: 2,
|
|
|
|
Topic: "Test",
|
|
|
|
Key: "sub-key",
|
|
|
|
Payload: "sample payload 2",
|
|
|
|
}}
|
2020-10-06 20:21:58 +00:00
|
|
|
publisher.Publish(&structs.Events{Index: 2, Events: events})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
result = nextResult(t, eventCh)
|
|
|
|
require.NoError(t, result.Err)
|
2020-10-04 19:12:35 +00:00
|
|
|
expected = []structs.Event{{Payload: "sample payload 2", Key: "sub-key", Topic: "Test", Index: 2}}
|
2020-08-31 17:19:28 +00:00
|
|
|
require.Equal(t, expected, result.Events)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
func TestEventBroker_ShutdownClosesSubscriptions(t *testing.T) {
|
2020-08-31 17:19:28 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
publisher := NewEventBroker(ctx, EventBrokerCfg{})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
sub1, err := publisher.Subscribe(&SubscribeRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub1.Unsubscribe()
|
|
|
|
|
|
|
|
sub2, err := publisher.Subscribe(&SubscribeRequest{})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-10-08 18:27:52 +00:00
|
|
|
// TestEventBroker_EmptyReqToken_DistinctSubscriptions tests subscription
|
|
|
|
// hanlding behavior when ACLs are disabled (request Token is empty).
|
|
|
|
// Subscriptions are mapped by their request token. when that token is empty,
|
|
|
|
// the subscriptions should still be handled indeppendtly of each other when
|
|
|
|
// unssubscribing.
|
|
|
|
func TestEventBroker_EmptyReqToken_DistinctSubscriptions(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
publisher := NewEventBroker(ctx, EventBrokerCfg{})
|
|
|
|
|
|
|
|
// first subscription, empty token
|
|
|
|
sub1, err := publisher.Subscribe(&SubscribeRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer sub1.Unsubscribe()
|
|
|
|
|
|
|
|
// second subscription, empty token
|
|
|
|
sub2, err := publisher.Subscribe(&SubscribeRequest{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, sub2)
|
|
|
|
|
|
|
|
sub1.Unsubscribe()
|
|
|
|
|
|
|
|
require.Equal(t, subscriptionStateOpen, atomic.LoadUint32(&sub2.state))
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:19:28 +00:00
|
|
|
func consumeSubscription(ctx context.Context, sub *Subscription) <-chan subNextResult {
|
|
|
|
eventCh := make(chan subNextResult, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
es, err := sub.Next(ctx)
|
|
|
|
eventCh <- subNextResult{
|
2020-10-01 18:43:28 +00:00
|
|
|
Events: es.Events,
|
2020-08-31 17:19:28 +00:00
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return eventCh
|
|
|
|
}
|
|
|
|
|
|
|
|
type subNextResult struct {
|
2020-10-04 19:12:35 +00:00
|
|
|
Events []structs.Event
|
2020-08-31 17:19:28 +00:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextResult(t *testing.T, eventCh <-chan subNextResult) subNextResult {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case next := <-eventCh:
|
|
|
|
return next
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatalf("no event after 100ms")
|
|
|
|
}
|
|
|
|
return subNextResult{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNoResult(t *testing.T, eventCh <-chan subNextResult) {
|
|
|
|
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)
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func consumeSub(ctx context.Context, sub *Subscription) error {
|
|
|
|
for {
|
|
|
|
_, err := sub.Next(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|