52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/monlet/agent/internal/config"
|
|
)
|
|
|
|
func mkCheck(id, cmd string, interval, timeout time.Duration) config.CheckConfig {
|
|
return config.CheckConfig{
|
|
ID: id,
|
|
Command: cmd,
|
|
Interval: config.Duration{Duration: interval},
|
|
Timeout: config.Duration{Duration: timeout},
|
|
}
|
|
}
|
|
|
|
func TestEmitsEvents(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
defer cancel()
|
|
out := make(chan Event, 10)
|
|
go Run(ctx, "a1", []config.CheckConfig{mkCheck("c1", "echo hi", 200*time.Millisecond, 100*time.Millisecond)}, out, Hooks{})
|
|
select {
|
|
case ev := <-out:
|
|
if ev.CheckID != "c1" || ev.Status != "ok" {
|
|
t.Fatalf("unexpected event: %+v", ev)
|
|
}
|
|
if ev.IncidentKey != "a1:c1" {
|
|
t.Fatalf("incident_key: %q", ev.IncidentKey)
|
|
}
|
|
case <-ctx.Done():
|
|
t.Fatal("no event")
|
|
}
|
|
}
|
|
|
|
func TestNoOverlapSkips(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Millisecond)
|
|
defer cancel()
|
|
out := make(chan Event, 10)
|
|
var skipped int32
|
|
hooks := Hooks{OnSkipped: func(string) { atomic.AddInt32(&skipped, 1) }}
|
|
c := mkCheck("slow", "sleep 1", 100*time.Millisecond, 900*time.Millisecond)
|
|
go Run(ctx, "a1", []config.CheckConfig{c}, out, hooks)
|
|
<-ctx.Done()
|
|
if atomic.LoadInt32(&skipped) == 0 {
|
|
t.Fatal("expected at least one skipped tick")
|
|
}
|
|
}
|