@ -371,7 +371,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
go func ( ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
t . Cleanup ( cancel )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan struct { } ) )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan ruleVersion ) )
} ( )
expectedTime := time . UnixMicro ( rand . Int63 ( ) )
@ -487,7 +487,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
go func ( ) {
err := sch . ruleRoutine ( ctx , models . AlertRuleKey { } , make ( chan * evaluation ) , make ( chan struct { } ) )
err := sch . ruleRoutine ( ctx , models . AlertRuleKey { } , make ( chan * evaluation ) , make ( chan ruleVersion ) )
stoppedChan <- err
} ( )
@ -509,7 +509,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
go func ( ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
t . Cleanup ( cancel )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan struct { } ) )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan ruleVersion ) )
} ( )
expectedTime := time . UnixMicro ( rand . Int63 ( ) )
@ -561,7 +561,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
go func ( ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
t . Cleanup ( cancel )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan struct { } ) )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan ruleVersion ) )
} ( )
expectedTime := time . UnixMicro ( rand . Int63 ( ) )
@ -604,7 +604,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
t . Run ( "should fetch the alert rule from database" , func ( t * testing . T ) {
evalChan := make ( chan * evaluation )
evalAppliedChan := make ( chan time . Time )
updateChan := make ( chan struct { } )
updateChan := make ( chan ruleVersion )
sch , ruleStore , _ , _ , _ , _ := createSchedule ( evalAppliedChan )
@ -615,7 +615,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
t . Cleanup ( cancel )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , updateChan )
} ( )
updateChan <- struct { } { }
updateChan <- ruleVersion ( rule . Version )
// wait for command to be executed
var queries [ ] interface { }
@ -647,7 +647,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
t . Run ( "should retry when database fails" , func ( t * testing . T ) {
evalAppliedChan := make ( chan time . Time )
updateChan := make ( chan struct { } )
updateChan := make ( chan ruleVersion )
sch , ruleStore , _ , _ , _ , _ := createSchedule ( evalAppliedChan )
sch . maxAttempts = rand . Int63n ( 4 ) + 1
@ -666,7 +666,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
}
return errors . New ( "TEST" )
}
updateChan <- struct { } { }
updateChan <- ruleVersion ( rule . Version )
var queries [ ] interface { }
require . Eventuallyf ( t , func ( ) bool {
@ -699,7 +699,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
evalChan := make ( chan * evaluation )
evalAppliedChan := make ( chan time . Time )
updateChan := make ( chan struct { } )
updateChan := make ( chan ruleVersion )
ctx := context . Background ( )
sch , ruleStore , _ , _ , _ , alertsRouter := createSchedule ( evalAppliedChan )
@ -751,14 +751,14 @@ func TestSchedule_ruleRoutine(t *testing.T) {
return nil
}
updateChan <- struct { } { }
updateChan <- ruleVersion ( rule . Version )
wg . Wait ( )
newRule := rule
newRule . Version ++
ruleStore . PutRule ( ctx , & newRule )
wg . Add ( 1 )
updateChan <- struct { } { }
updateChan <- ruleVersion ( newRule . Version )
wg . Wait ( )
require . Eventually ( t , func ( ) bool {
@ -821,7 +821,7 @@ func TestSchedule_ruleRoutine(t *testing.T) {
go func ( ) {
ctx , cancel := context . WithCancel ( context . Background ( ) )
t . Cleanup ( cancel )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan struct { } ) )
_ = sch . ruleRoutine ( ctx , rule . GetKey ( ) , evalChan , make ( chan ruleVersion ) )
} ( )
evalChan <- & evaluation {
@ -850,12 +850,14 @@ func TestSchedule_UpdateAlertRule(t *testing.T) {
sch := setupSchedulerWithFakeStores ( t )
key := generateRuleKey ( )
info , _ := sch . registry . getOrCreateInfo ( context . Background ( ) , key )
version := rand . Int63 ( )
go func ( ) {
sch . UpdateAlertRule ( key )
sch . UpdateAlertRule ( key , version )
} ( )
select {
case <- info . updateCh :
case v := <- info . updateCh :
require . Equal ( t , ruleVersion ( version ) , v )
case <- time . After ( 5 * time . Second ) :
t . Fatal ( "No message was received on update channel" )
}
@ -865,14 +867,14 @@ func TestSchedule_UpdateAlertRule(t *testing.T) {
key := generateRuleKey ( )
info , _ := sch . registry . getOrCreateInfo ( context . Background ( ) , key )
info . stop ( )
sch . UpdateAlertRule ( key )
sch . UpdateAlertRule ( key , rand . Int63 ( ) )
} )
} )
t . Run ( "when rule does not exist" , func ( t * testing . T ) {
t . Run ( "should exit" , func ( t * testing . T ) {
sch := setupSchedulerWithFakeStores ( t )
key := generateRuleKey ( )
sch . UpdateAlertRule ( key )
sch . UpdateAlertRule ( key , rand . Int63 ( ) )
} )
} )
}
@ -884,7 +886,7 @@ func TestSchedule_DeleteAlertRule(t *testing.T) {
key := generateRuleKey ( )
info , _ := sch . registry . getOrCreateInfo ( context . Background ( ) , key )
sch . DeleteAlertRule ( key )
require . False ( t , info . update ( ) )
require . False ( t , info . update ( ruleVersion ( rand . Int63 ( ) ) ) )
success , dropped := info . eval ( time . Now ( ) , 1 )
require . False ( t , success )
require . Nilf ( t , dropped , "expected no dropped evaluations but got one" )
@ -896,7 +898,7 @@ func TestSchedule_DeleteAlertRule(t *testing.T) {
info , _ := sch . registry . getOrCreateInfo ( context . Background ( ) , key )
info . stop ( )
sch . DeleteAlertRule ( key )
require . False ( t , info . update ( ) )
require . False ( t , info . update ( ruleVersion ( rand . Int63 ( ) ) ) )
success , dropped := info . eval ( time . Now ( ) , 1 )
require . False ( t , success )
require . Nilf ( t , dropped , "expected no dropped evaluations but got one" )