all: Implement suture v4-api (#6947)
This commit is contained in:
@@ -15,10 +15,9 @@ import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/thejerf/suture"
|
||||
"github.com/thejerf/suture/v4"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/sync"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
)
|
||||
|
||||
type EventType int64
|
||||
@@ -219,7 +218,6 @@ type Logger interface {
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
suture.Service
|
||||
subs []*subscription
|
||||
nextSubscriptionIDs []int
|
||||
nextGlobalID int
|
||||
@@ -267,7 +265,6 @@ func NewLogger() Logger {
|
||||
funcs: make(chan func(context.Context)),
|
||||
toUnsubscribe: make(chan *subscription),
|
||||
}
|
||||
l.Service = util.AsService(l.serve, l.String())
|
||||
// Make sure the timer is in the stopped state and hasn't fired anything
|
||||
// into the channel.
|
||||
if !l.timeout.Stop() {
|
||||
@@ -276,7 +273,7 @@ func NewLogger() Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *logger) serve(ctx context.Context) {
|
||||
func (l *logger) Serve(ctx context.Context) error {
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
@@ -302,6 +299,8 @@ loop:
|
||||
for _, s := range l.subs {
|
||||
close(s.events)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *logger) Log(t EventType, data interface{}) {
|
||||
@@ -535,7 +534,7 @@ type noopLogger struct{}
|
||||
|
||||
var NoopLogger Logger = &noopLogger{}
|
||||
|
||||
func (*noopLogger) Serve() {}
|
||||
func (*noopLogger) Serve(ctx context.Context) error { return nil }
|
||||
|
||||
func (*noopLogger) Stop() {}
|
||||
|
||||
|
||||
+36
-42
@@ -7,6 +7,7 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
@@ -27,10 +28,16 @@ func TestNewLogger(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubscriber(t *testing.T) {
|
||||
func setupLogger() (Logger, context.CancelFunc) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
go l.Serve(ctx)
|
||||
return l, cancel
|
||||
}
|
||||
|
||||
func TestSubscriber(t *testing.T) {
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(0)
|
||||
defer s.Unsubscribe()
|
||||
@@ -40,9 +47,8 @@ func TestSubscriber(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTimeout(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(0)
|
||||
defer s.Unsubscribe()
|
||||
@@ -53,9 +59,8 @@ func TestTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEventBeforeSubscribe(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
l.Log(DeviceConnected, "foo")
|
||||
s := l.Subscribe(0)
|
||||
@@ -68,9 +73,8 @@ func TestEventBeforeSubscribe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEventAfterSubscribe(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
@@ -95,9 +99,8 @@ func TestEventAfterSubscribe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(DeviceDisconnected)
|
||||
defer s.Unsubscribe()
|
||||
@@ -110,9 +113,8 @@ func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBufferOverflow(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
@@ -135,9 +137,8 @@ func TestBufferOverflow(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnsubscribe(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
l.Log(DeviceConnected, "foo")
|
||||
@@ -157,9 +158,8 @@ func TestUnsubscribe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGlobalIDs(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
@@ -189,9 +189,8 @@ func TestGlobalIDs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSubscriptionIDs(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(DeviceConnected)
|
||||
defer s.Unsubscribe()
|
||||
@@ -231,9 +230,8 @@ func TestSubscriptionIDs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBufferedSub(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
@@ -262,9 +260,8 @@ func TestBufferedSub(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkBufferedSub(b *testing.B) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
@@ -318,9 +315,8 @@ func BenchmarkBufferedSub(b *testing.B) {
|
||||
}
|
||||
|
||||
func TestSinceUsesSubscriptionId(t *testing.T) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(DeviceConnected)
|
||||
defer s.Unsubscribe()
|
||||
@@ -375,9 +371,8 @@ func TestUnsubscribeContention(t *testing.T) {
|
||||
senders = 1000
|
||||
)
|
||||
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
// Start listeners. These will poll until the stop channel is closed,
|
||||
// then exit and unsubscribe.
|
||||
@@ -444,9 +439,8 @@ func TestUnsubscribeContention(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkLogEvent(b *testing.B) {
|
||||
l := NewLogger()
|
||||
defer l.Stop()
|
||||
go l.Serve()
|
||||
l, cancel := setupLogger()
|
||||
defer cancel()
|
||||
|
||||
s := l.Subscribe(AllEvents)
|
||||
defer s.Unsubscribe()
|
||||
|
||||
Reference in New Issue
Block a user