Files
tasks/service/history/queues/scheuduler_monitor_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

138 lines
4.1 KiB
Go

package queues
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/tasks"
"go.temporal.io/server/service/history/tests"
"go.uber.org/mock/gomock"
)
var (
testSchedulerMonitorOptions = schedulerMonitorOptions{
aggregationCount: 10,
aggregationDuration: 200 * time.Millisecond,
}
)
type (
schedulerMonitorSuite struct {
suite.Suite
*require.Assertions
controller *gomock.Controller
mockNamespaceRegistry *namespace.MockRegistry
mockMetricsHandler *metrics.MockHandler
mockTimerMetric *metrics.MockTimerIface
mockTimeSource *clock.EventTimeSource
schedulerMonitor *schedulerMonitor
}
)
func TestSchedulerMonitorSuite(t *testing.T) {
s := new(schedulerMonitorSuite)
suite.Run(t, s)
}
func (s *schedulerMonitorSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.controller = gomock.NewController(s.T())
s.mockNamespaceRegistry = namespace.NewMockRegistry(s.controller)
s.mockMetricsHandler = metrics.NewMockHandler(s.controller)
s.mockTimerMetric = metrics.NewMockTimerIface(s.controller)
s.mockTimeSource = clock.NewEventTimeSource()
s.mockNamespaceRegistry.EXPECT().GetNamespaceName(gomock.Any()).Return(tests.Namespace, nil).AnyTimes()
s.mockMetricsHandler.EXPECT().WithTags(gomock.Any()).Return(s.mockMetricsHandler).AnyTimes()
s.mockMetricsHandler.EXPECT().Timer(metrics.QueueScheduleLatency.Name()).Return(s.mockTimerMetric).AnyTimes()
s.schedulerMonitor = newSchedulerMonitor(
func(e Executable) TaskChannelKey {
return TaskChannelKey{
NamespaceID: tests.NamespaceID.String(),
Priority: tasks.PriorityHigh,
}
},
s.mockNamespaceRegistry,
s.mockTimeSource,
s.mockMetricsHandler,
testSchedulerMonitorOptions,
)
}
func (s *schedulerMonitorSuite) TearDownTest() {
s.controller.Finish()
}
func (s *schedulerMonitorSuite) TestRecordStart_AggregationCount() {
s.schedulerMonitor.Start()
defer s.schedulerMonitor.Stop()
now := clock.NewRealTimeSource().Now()
scheduledTime := now
singleScheduleLatency := time.Millisecond * 10
s.mockTimeSource.Update(now)
totalExecutables := 2*testSchedulerMonitorOptions.aggregationCount + 10
s.mockTimerMetric.EXPECT().Record(
time.Duration(testSchedulerMonitorOptions.aggregationCount) * singleScheduleLatency,
).Times(totalExecutables / testSchedulerMonitorOptions.aggregationCount)
for numExecutables := 0; numExecutables != totalExecutables; numExecutables++ {
mockExecutable := NewMockExecutable(s.controller)
mockExecutable.EXPECT().GetScheduledTime().Return(scheduledTime).Times(1)
now = now.Add(singleScheduleLatency)
s.mockTimeSource.Update(now)
s.schedulerMonitor.RecordStart(mockExecutable)
}
}
func (s *schedulerMonitorSuite) TestRecordStart_AggregationDuration() {
s.schedulerMonitor.Start()
defer s.schedulerMonitor.Stop()
now := clock.NewRealTimeSource().Now()
singleScheduleLatency := time.Millisecond * 10
s.mockTimeSource.Update(now)
totalExecutables := testSchedulerMonitorOptions.aggregationCount / 2
done := make(chan struct{})
s.mockTimerMetric.EXPECT().Record(
// although # of executable is less than aggregationCount
// the value emitted should be scaled
time.Duration(testSchedulerMonitorOptions.aggregationCount) * singleScheduleLatency,
).Do(func(time.Duration, ...metrics.Tag) {
close(done)
}).Times(1)
for numExecutables := 0; numExecutables != totalExecutables; numExecutables++ {
mockExecutable := NewMockExecutable(s.controller)
mockExecutable.EXPECT().GetScheduledTime().Return(now).Times(1)
now = now.Add(singleScheduleLatency)
s.mockTimeSource.Update(now)
s.schedulerMonitor.RecordStart(mockExecutable)
// simulate the case where task submission is very low frequency
now = now.Add(10 * singleScheduleLatency)
}
// wait for the metric emission ticker
select {
case <-done:
case <-time.NewTimer(3 * testSchedulerMonitorOptions.aggregationDuration).C:
s.Fail("metric emission ticker should fire earlier")
}
}