2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-12-15 17:52:48 +00:00
|
|
|
package logdrop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-06 18:33:53 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-12-15 17:52:48 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-06 18:33:53 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2022-12-15 17:52:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLogDrop(t *testing.T) {
|
2023-01-06 18:33:53 +00:00
|
|
|
mockLogger := NewMockLogger(t)
|
|
|
|
mockLogger.On("Log", hclog.Info, "hello", "test", 0).Return()
|
|
|
|
ld := NewLogDropSink(context.Background(), 10, mockLogger, func(_ Log) {})
|
2022-12-15 17:52:48 +00:00
|
|
|
require.NotNil(t, ld)
|
2023-01-06 18:33:53 +00:00
|
|
|
ld.Accept("test Log", hclog.Info, "hello", "test", 0)
|
2022-12-15 17:52:48 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2023-01-06 18:33:53 +00:00
|
|
|
mockLogger.AssertNumberOfCalls(r, "Log", 1)
|
2022-12-15 17:52:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogDroppedWhenChannelFilled(t *testing.T) {
|
2023-01-06 18:33:53 +00:00
|
|
|
mockLogger := NewMockLogger(t)
|
2022-12-15 17:52:48 +00:00
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
defer cancelFunc()
|
|
|
|
block := make(chan interface{})
|
2023-01-06 18:33:53 +00:00
|
|
|
mockLogger.On("Log", hclog.Debug, "hello").Run(func(args mock.Arguments) {
|
2022-12-15 17:52:48 +00:00
|
|
|
<-block
|
|
|
|
})
|
|
|
|
|
|
|
|
var called = make(chan interface{})
|
2023-01-06 18:33:53 +00:00
|
|
|
ld := NewLogDropSink(ctx, 1, mockLogger, func(l Log) {
|
2022-12-15 17:52:48 +00:00
|
|
|
close(called)
|
|
|
|
})
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
ld.Accept("test", hclog.Debug, "hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-called:
|
|
|
|
close(block)
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Fatal("timeout waiting for drop func to be called")
|
|
|
|
}
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2023-01-06 18:33:53 +00:00
|
|
|
mockLogger.AssertNumberOfCalls(r, "Log", 1)
|
2022-12-15 17:52:48 +00:00
|
|
|
})
|
|
|
|
}
|