open-nomad/command/agent/monitor/monitor_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
1.5 KiB
Go
Raw Normal View History

package monitor
import (
"fmt"
"strings"
"testing"
"time"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/ci"
"github.com/stretchr/testify/require"
)
func TestMonitor_Start(t *testing.T) {
ci.Parallel(t)
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Error,
})
m := New(512, logger, &log.LoggerOptions{
Level: log.Debug,
})
logCh := m.Start()
defer m.Stop()
go func() {
logger.Debug("test log")
time.Sleep(10 * time.Millisecond)
}()
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")
}
}
}
// Ensure number of dropped messages are logged
func TestMonitor_DroppedMessages(t *testing.T) {
ci.Parallel(t)
logger := log.NewInterceptLogger(&log.LoggerOptions{
Level: log.Warn,
})
m := new(5, logger, &log.LoggerOptions{
Level: log.Debug,
})
m.droppedDuration = 5 * time.Millisecond
doneCh := make(chan struct{})
defer close(doneCh)
logCh := m.Start()
for i := 0; i <= 100; i++ {
logger.Debug(fmt.Sprintf("test message %d", i))
}
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") {
close(passed)
}
}
}
}()
TEST:
for {
select {
case <-passed:
break TEST
2019-11-04 15:07:03 +00:00
case <-time.After(2 * time.Second):
require.Fail(t, "expected to see warn dropped messages")
}
}
}