service: remove stop method and use contexts (#7292)

This commit is contained in:
Sam Kleinman
2021-11-18 17:56:21 -05:00
committed by GitHub
parent 1c34d17240
commit 6ab62fe7b6
115 changed files with 3613 additions and 2271 deletions
+3 -2
View File
@@ -12,8 +12,9 @@ import (
)
func TestExample(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: "example-client",
+1 -1
View File
@@ -341,7 +341,7 @@ func (s *Server) OnStop() { s.stop() }
func (s *Server) Wait() { <-s.exited; s.BaseService.Wait() }
// OnStart implements Service.OnStart by starting the server.
func (s *Server) OnStart() error { s.run(); return nil }
func (s *Server) OnStart(ctx context.Context) error { s.run(); return nil }
// OnReset implements Service.OnReset. It has no effect for this service.
func (s *Server) OnReset() error { return nil }
+58 -34
View File
@@ -7,7 +7,6 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
@@ -20,8 +19,10 @@ const (
)
func TestSubscribeWithArgs(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
t.Run("DefaultLimit", func(t *testing.T) {
sub := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
@@ -47,8 +48,10 @@ func TestSubscribeWithArgs(t *testing.T) {
}
func TestObserver(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
done := make(chan struct{})
var got interface{}
@@ -65,8 +68,10 @@ func TestObserver(t *testing.T) {
}
func TestObserverErrors(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
require.Error(t, s.Observe(ctx, nil, query.Empty{}))
require.NoError(t, s.Observe(ctx, func(pubsub.Message) error { return nil }))
@@ -74,8 +79,10 @@ func TestObserverErrors(t *testing.T) {
}
func TestPublishDoesNotBlock(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: clientID,
@@ -100,8 +107,10 @@ func TestPublishDoesNotBlock(t *testing.T) {
}
func TestSubscribeErrors(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
t.Run("EmptyQueryErr", func(t *testing.T) {
_, err := s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{ClientID: clientID})
@@ -118,8 +127,10 @@ func TestSubscribeErrors(t *testing.T) {
}
func TestSlowSubscriber(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: clientID,
@@ -137,8 +148,10 @@ func TestSlowSubscriber(t *testing.T) {
}
func TestDifferentClients(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub1 := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: "client-1",
@@ -188,8 +201,10 @@ func TestDifferentClients(t *testing.T) {
}
func TestSubscribeDuplicateKeys(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
testCases := []struct {
query string
@@ -241,8 +256,10 @@ func TestSubscribeDuplicateKeys(t *testing.T) {
}
func TestClientSubscribesTwice(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
q := query.MustParse("tm.events.type='NewBlock'")
events := []abci.Event{{
@@ -274,8 +291,10 @@ func TestClientSubscribesTwice(t *testing.T) {
}
func TestUnsubscribe(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: clientID,
@@ -296,8 +315,10 @@ func TestUnsubscribe(t *testing.T) {
}
func TestClientUnsubscribesTwice(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: clientID,
@@ -315,8 +336,10 @@ func TestClientUnsubscribesTwice(t *testing.T) {
}
func TestResubscribe(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
args := pubsub.SubscribeArgs{
ClientID: clientID,
@@ -336,8 +359,10 @@ func TestResubscribe(t *testing.T) {
}
func TestUnsubscribeAll(t *testing.T) {
s := newTestServer(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := newTestServer(ctx, t)
sub1 := newTestSub(t).must(s.SubscribeWithArgs(ctx, pubsub.SubscribeArgs{
ClientID: clientID,
@@ -364,28 +389,27 @@ func TestBufferCapacity(t *testing.T) {
require.Equal(t, 2, s.BufferCapacity())
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
require.NoError(t, s.Publish(ctx, "Nighthawk"))
require.NoError(t, s.Publish(ctx, "Sage"))
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
ctx, cancel = context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
require.ErrorIs(t, s.Publish(ctx, "Ironclad"), context.DeadlineExceeded)
}
func newTestServer(t testing.TB) *pubsub.Server {
func newTestServer(ctx context.Context, t testing.TB) *pubsub.Server {
t.Helper()
s := pubsub.NewServer(func(s *pubsub.Server) {
s.Logger = log.TestingLogger()
})
require.NoError(t, s.Start())
t.Cleanup(func() {
assert.NoError(t, s.Stop())
})
require.NoError(t, s.Start(ctx))
t.Cleanup(s.Wait)
return s
}