2019-10-15 19:14:25 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2019-10-30 13:55:41 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-10-15 19:14:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMonitor_Start(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
|
|
Level: log.Error,
|
|
|
|
})
|
|
|
|
|
|
|
|
m := New(512, logger, &log.LoggerOptions{
|
|
|
|
Level: log.Debug,
|
|
|
|
})
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
logCh := m.Start()
|
|
|
|
defer m.Stop()
|
2019-11-01 15:06:49 +00:00
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
go func() {
|
2019-11-01 15:06:49 +00:00
|
|
|
logger.Debug("test log")
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2019-10-15 19:14:25 +00:00
|
|
|
}()
|
2019-11-01 15:06:49 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-logCh:
|
|
|
|
require.Contains(t, string(log), "[DEBUG] test log")
|
|
|
|
return
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Fatal("Expected to receive from log channel")
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
// Ensure number of dropped messages are logged
|
2019-10-15 19:14:25 +00:00
|
|
|
func TestMonitor_DroppedMessages(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
logger := log.NewInterceptLogger(&log.LoggerOptions{
|
|
|
|
Level: log.Warn,
|
|
|
|
})
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
m := new(5, logger, &log.LoggerOptions{
|
2019-10-15 19:14:25 +00:00
|
|
|
Level: log.Debug,
|
|
|
|
})
|
2019-10-30 13:55:41 +00:00
|
|
|
m.droppedDuration = 5 * time.Millisecond
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
logCh := m.Start()
|
2019-10-15 19:14:25 +00:00
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
for i := 0; i <= 100; i++ {
|
|
|
|
logger.Debug(fmt.Sprintf("test message %d", i))
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:55:41 +00:00
|
|
|
received := ""
|
|
|
|
passed := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case recv := <-logCh:
|
|
|
|
received += string(recv)
|
2019-11-04 15:07:03 +00:00
|
|
|
if strings.Contains(received, "[WARN] Monitor dropped") {
|
2019-10-30 13:55:41 +00:00
|
|
|
close(passed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
TEST:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-passed:
|
|
|
|
break TEST
|
2019-11-04 15:07:03 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
2019-10-30 13:55:41 +00:00
|
|
|
require.Fail(t, "expected to see warn dropped messages")
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|