2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-07-25 12:50:14 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
package events
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-11-17 12:19:04 +00:00
|
|
|
"context"
|
2017-11-22 23:25:55 +00:00
|
|
|
"encoding/json"
|
2014-07-13 19:07:24 +00:00
|
|
|
"fmt"
|
2018-12-13 12:42:28 +00:00
|
|
|
"sync"
|
2014-07-13 19:07:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-02-07 07:47:53 +00:00
|
|
|
const timeout = time.Second
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
func init() {
|
|
|
|
runningTests = true
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
func TestNewLogger(t *testing.T) {
|
2016-08-08 18:09:40 +00:00
|
|
|
l := NewLogger()
|
2014-07-13 19:07:24 +00:00
|
|
|
if l == nil {
|
|
|
|
t.Fatal("Unexpected nil Logger")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func setupLogger() (Logger, context.CancelFunc) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-08-08 18:09:40 +00:00
|
|
|
l := NewLogger()
|
2020-11-17 12:19:04 +00:00
|
|
|
go l.Serve(ctx)
|
|
|
|
return l, cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubscriber(t *testing.T) {
|
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2018-12-13 12:42:28 +00:00
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
s := l.Subscribe(0)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2014-07-13 19:07:24 +00:00
|
|
|
if s == nil {
|
|
|
|
t.Fatal("Unexpected nil Subscription")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeout(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2018-12-13 12:42:28 +00:00
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
s := l.Subscribe(0)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2014-07-13 19:07:24 +00:00
|
|
|
_, err := s.Poll(timeout)
|
2016-08-08 18:09:40 +00:00
|
|
|
if err != ErrTimeout {
|
2014-07-13 19:07:24 +00:00
|
|
|
t.Fatal("Unexpected non-Timeout error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventBeforeSubscribe(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
s := l.Subscribe(0)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
_, err := s.Poll(timeout)
|
2016-08-08 18:09:40 +00:00
|
|
|
if err != ErrTimeout {
|
2014-07-13 19:07:24 +00:00
|
|
|
t.Fatal("Unexpected non-Timeout error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventAfterSubscribe(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
ev, err := s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
2016-08-08 18:09:40 +00:00
|
|
|
if ev.Type != DeviceConnected {
|
2014-07-13 19:07:24 +00:00
|
|
|
t.Error("Incorrect event type", ev.Type)
|
|
|
|
}
|
|
|
|
switch v := ev.Data.(type) {
|
|
|
|
case string:
|
|
|
|
if v != "foo" {
|
|
|
|
t.Error("Incorrect Data string", v)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Errorf("Incorrect Data type %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventAfterSubscribeIgnoreMask(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(DeviceDisconnected)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
_, err := s.Poll(timeout)
|
2016-08-08 18:09:40 +00:00
|
|
|
if err != ErrTimeout {
|
2014-07-13 19:07:24 +00:00
|
|
|
t.Fatal("Unexpected non-Timeout error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufferOverflow(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2017-02-07 07:57:39 +00:00
|
|
|
// The first BufferSize events will be logged pretty much
|
|
|
|
// instantaneously. The next BufferSize events will each block for up to
|
|
|
|
// 15ms, plus overhead from race detector and thread scheduling latency
|
|
|
|
// etc. This latency can sometimes be significant and is incurred for
|
|
|
|
// each call. We just verify that the whole test completes in a
|
|
|
|
// reasonable time, taking no more than 15 seconds in total.
|
|
|
|
|
2014-07-13 19:07:24 +00:00
|
|
|
t0 := time.Now()
|
2017-02-07 07:47:53 +00:00
|
|
|
const nEvents = BufferSize * 2
|
|
|
|
for i := 0; i < nEvents; i++ {
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
2017-02-07 07:57:39 +00:00
|
|
|
if d := time.Since(t0); d > 15*time.Second {
|
2017-02-07 07:47:53 +00:00
|
|
|
t.Fatal("Logging took too long,", d, "avg", d/nEvents, "expected <", eventLogTimeout)
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsubscribe(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
_, err := s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
_, err = s.Poll(timeout)
|
2016-08-08 18:09:40 +00:00
|
|
|
if err != ErrClosed {
|
2014-07-13 19:07:24 +00:00
|
|
|
t.Fatal("Unexpected non-Closed error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:18:58 +00:00
|
|
|
func TestGlobalIDs(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "foo")
|
2019-02-02 11:16:27 +00:00
|
|
|
l.Subscribe(AllEvents)
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "bar")
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
ev, err := s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
if ev.Data.(string) != "foo" {
|
|
|
|
t.Fatal("Incorrect event:", ev)
|
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
id := ev.GlobalID
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
ev, err = s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
if ev.Data.(string) != "bar" {
|
|
|
|
t.Fatal("Incorrect event:", ev)
|
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
if ev.GlobalID != id+1 {
|
|
|
|
t.Fatalf("ID not incremented (%d != %d)", ev.GlobalID, id+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubscriptionIDs(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2016-06-27 21:18:58 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(DeviceConnected)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-06-27 21:18:58 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceDisconnected, "a")
|
|
|
|
l.Log(DeviceConnected, "b")
|
|
|
|
l.Log(DeviceConnected, "c")
|
|
|
|
l.Log(DeviceDisconnected, "d")
|
2016-06-27 21:18:58 +00:00
|
|
|
|
|
|
|
ev, err := s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ev.GlobalID != 2 {
|
|
|
|
t.Fatal("Incorrect GlobalID:", ev.GlobalID)
|
|
|
|
}
|
|
|
|
if ev.SubscriptionID != 1 {
|
|
|
|
t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
ev, err = s.Poll(timeout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
if ev.GlobalID != 3 {
|
|
|
|
t.Fatal("Incorrect GlobalID:", ev.GlobalID)
|
|
|
|
}
|
|
|
|
if ev.SubscriptionID != 2 {
|
|
|
|
t.Fatal("Incorrect SubscriptionID:", ev.SubscriptionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
ev, err = s.Poll(timeout)
|
2016-08-08 18:09:40 +00:00
|
|
|
if err != ErrTimeout {
|
2016-06-27 21:18:58 +00:00
|
|
|
t.Fatal("Unexpected error:", err)
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufferedSub(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
bs := NewBufferedSubscription(s, 10*BufferSize)
|
2014-07-13 19:07:24 +00:00
|
|
|
|
|
|
|
go func() {
|
2016-08-08 18:09:40 +00:00
|
|
|
for i := 0; i < 10*BufferSize; i++ {
|
|
|
|
l.Log(DeviceConnected, fmt.Sprintf("event-%d", i))
|
2014-07-13 19:07:24 +00:00
|
|
|
if i%30 == 0 {
|
|
|
|
// Give the buffer routine time to pick up the events
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
recv := 0
|
2016-08-08 18:09:40 +00:00
|
|
|
for recv < 10*BufferSize {
|
2017-01-31 12:04:29 +00:00
|
|
|
evs := bs.Since(recv, nil, time.Minute)
|
2014-07-13 19:07:24 +00:00
|
|
|
for _, ev := range evs {
|
2016-06-27 21:18:58 +00:00
|
|
|
if ev.GlobalID != recv+1 {
|
|
|
|
t.Fatalf("Incorrect ID; %d != %d", ev.GlobalID, recv+1)
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
recv = ev.GlobalID
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-27 08:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBufferedSub(b *testing.B) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2016-06-27 08:26:43 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
bufferSize := BufferSize
|
|
|
|
bs := NewBufferedSubscription(s, bufferSize)
|
2016-06-27 08:26:43 +00:00
|
|
|
|
|
|
|
// The coord channel paces the sender according to the receiver,
|
|
|
|
// ensuring that no events are dropped. The benchmark measures sending +
|
|
|
|
// receiving + synchronization overhead.
|
|
|
|
|
|
|
|
coord := make(chan struct{}, bufferSize)
|
|
|
|
for i := 0; i < bufferSize-1; i++ {
|
|
|
|
coord <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive the events
|
2016-12-18 18:57:41 +00:00
|
|
|
done := make(chan error)
|
2016-06-27 08:26:43 +00:00
|
|
|
go func() {
|
|
|
|
recv := 0
|
2016-08-08 18:09:40 +00:00
|
|
|
var evs []Event
|
2016-06-27 08:26:43 +00:00
|
|
|
for i := 0; i < b.N; {
|
2017-01-31 12:04:29 +00:00
|
|
|
evs = bs.Since(recv, evs[:0], time.Minute)
|
2016-06-27 08:26:43 +00:00
|
|
|
for _, ev := range evs {
|
2016-06-27 21:18:58 +00:00
|
|
|
if ev.GlobalID != recv+1 {
|
2016-12-18 18:57:41 +00:00
|
|
|
done <- fmt.Errorf("skipped event %v %v", ev.GlobalID, recv)
|
|
|
|
return
|
2016-06-27 08:26:43 +00:00
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
recv = ev.GlobalID
|
2016-06-27 08:26:43 +00:00
|
|
|
coord <- struct{}{}
|
|
|
|
}
|
|
|
|
i += len(evs)
|
|
|
|
}
|
2016-12-18 18:57:41 +00:00
|
|
|
done <- nil
|
2016-06-27 08:26:43 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Send the events
|
|
|
|
eventData := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"other": "data",
|
|
|
|
"and": "something else",
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, eventData)
|
2016-06-27 08:26:43 +00:00
|
|
|
<-coord
|
|
|
|
}
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2016-12-18 18:57:41 +00:00
|
|
|
if err := <-done; err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
2016-06-27 08:26:43 +00:00
|
|
|
b.ReportAllocs()
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
|
|
|
|
func TestSinceUsesSubscriptionId(t *testing.T) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2016-06-27 21:18:58 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
s := l.Subscribe(DeviceConnected)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2016-08-08 18:09:40 +00:00
|
|
|
bs := NewBufferedSubscription(s, 10*BufferSize)
|
2016-06-27 21:18:58 +00:00
|
|
|
|
2016-08-08 18:09:40 +00:00
|
|
|
l.Log(DeviceConnected, "a") // SubscriptionID = 1
|
|
|
|
l.Log(DeviceDisconnected, "b")
|
|
|
|
l.Log(DeviceDisconnected, "c")
|
|
|
|
l.Log(DeviceConnected, "d") // SubscriptionID = 2
|
2016-06-27 21:18:58 +00:00
|
|
|
|
2016-06-28 06:29:49 +00:00
|
|
|
// We need to loop for the events, as they may not all have been
|
|
|
|
// delivered to the buffered subscription when we get here.
|
|
|
|
t0 := time.Now()
|
|
|
|
for time.Since(t0) < time.Second {
|
2017-01-31 12:04:29 +00:00
|
|
|
events := bs.Since(0, nil, time.Minute)
|
2016-06-28 06:29:49 +00:00
|
|
|
if len(events) == 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(events) > 2 {
|
|
|
|
t.Fatal("Incorrect number of events:", len(events))
|
|
|
|
}
|
2016-06-27 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 12:04:29 +00:00
|
|
|
events := bs.Since(1, nil, time.Minute)
|
2016-06-27 21:18:58 +00:00
|
|
|
if len(events) != 1 {
|
|
|
|
t.Fatal("Incorrect number of events:", len(events))
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 23:25:55 +00:00
|
|
|
|
|
|
|
func TestUnmarshalEvent(t *testing.T) {
|
|
|
|
var event Event
|
|
|
|
|
|
|
|
s := `
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"globalID": 1,
|
|
|
|
"time": "2006-01-02T15:04:05.999999999Z",
|
|
|
|
"type": "Starting",
|
|
|
|
"data": {}
|
|
|
|
}`
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &event); err != nil {
|
|
|
|
t.Fatal("Failed to unmarshal event:", err)
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 12:42:28 +00:00
|
|
|
|
|
|
|
func TestUnsubscribeContention(t *testing.T) {
|
|
|
|
// Check that we can unsubscribe without blocking the whole system.
|
|
|
|
|
|
|
|
const (
|
|
|
|
listeners = 50
|
|
|
|
senders = 1000
|
|
|
|
)
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2018-12-13 12:42:28 +00:00
|
|
|
|
|
|
|
// Start listeners. These will poll until the stop channel is closed,
|
|
|
|
// then exit and unsubscribe.
|
|
|
|
|
|
|
|
stopListeners := make(chan struct{})
|
|
|
|
var listenerWg sync.WaitGroup
|
|
|
|
listenerWg.Add(listeners)
|
|
|
|
for i := 0; i < listeners; i++ {
|
|
|
|
go func() {
|
|
|
|
defer listenerWg.Done()
|
|
|
|
|
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2018-12-13 12:42:28 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.C():
|
|
|
|
|
|
|
|
case <-stopListeners:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start senders. These send pointless events until the stop channel is
|
|
|
|
// closed.
|
|
|
|
|
|
|
|
stopSenders := make(chan struct{})
|
|
|
|
defer close(stopSenders)
|
|
|
|
var senderWg sync.WaitGroup
|
|
|
|
senderWg.Add(senders)
|
|
|
|
for i := 0; i < senders; i++ {
|
|
|
|
go func() {
|
|
|
|
defer senderWg.Done()
|
|
|
|
|
|
|
|
t := time.NewTicker(time.Millisecond)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
l.Log(StateChanged, nil)
|
|
|
|
|
|
|
|
case <-stopSenders:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give everything time to start up.
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// Stop the listeners and wait for them to exit. This should happen in a
|
|
|
|
// reasonable time frame.
|
|
|
|
|
|
|
|
t0 := time.Now()
|
|
|
|
close(stopListeners)
|
|
|
|
listenerWg.Wait()
|
|
|
|
if d := time.Since(t0); d > time.Minute {
|
|
|
|
t.Error("It should not take", d, "to unsubscribe from an event stream")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLogEvent(b *testing.B) {
|
2020-11-17 12:19:04 +00:00
|
|
|
l, cancel := setupLogger()
|
|
|
|
defer cancel()
|
2018-12-13 12:42:28 +00:00
|
|
|
|
|
|
|
s := l.Subscribe(AllEvents)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer s.Unsubscribe()
|
2018-12-13 12:42:28 +00:00
|
|
|
NewBufferedSubscription(s, 1) // runs in the background
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
l.Log(StateChanged, nil)
|
|
|
|
}
|
|
|
|
}
|