@@ -8427,7 +8427,96 @@ func TestStateStore_DeleteScalingPolicies(t *testing.T) {
8427
8427
require .False (watchFired (ws ))
8428
8428
}
8429
8429
8430
- func TestStateStore_DeleteJob_ChildScalingPolicies (t * testing.T ) {
8430
+ func TestStateStore_StopJob_DeleteScalingPolicies (t * testing.T ) {
8431
+ t .Parallel ()
8432
+
8433
+ require := require .New (t )
8434
+
8435
+ state := testStateStore (t )
8436
+
8437
+ job := mock .Job ()
8438
+
8439
+ err := state .UpsertJob (1000 , job )
8440
+ require .NoError (err )
8441
+
8442
+ policy := mock .ScalingPolicy ()
8443
+ policy .Target [structs .ScalingTargetJob ] = job .ID
8444
+ err = state .UpsertScalingPolicies (1100 , []* structs.ScalingPolicy {policy })
8445
+ require .NoError (err )
8446
+
8447
+ // Ensure the scaling policy is present and start some watches
8448
+ wsGet := memdb .NewWatchSet ()
8449
+ out , err := state .ScalingPolicyByTarget (wsGet , policy .Target )
8450
+ require .NoError (err )
8451
+ require .NotNil (out )
8452
+ wsList := memdb .NewWatchSet ()
8453
+ _ , err = state .ScalingPolicies (wsList )
8454
+ require .NoError (err )
8455
+
8456
+ // Stop the job
8457
+ job , err = state .JobByID (nil , job .Namespace , job .ID )
8458
+ require .NoError (err )
8459
+ job .Stop = true
8460
+ err = state .UpsertJob (1200 , job )
8461
+ require .NoError (err )
8462
+
8463
+ // Ensure:
8464
+ // * the scaling policy was deleted
8465
+ // * the watches were fired
8466
+ // * the table index was advanced
8467
+ require .True (watchFired (wsGet ))
8468
+ require .True (watchFired (wsList ))
8469
+ out , err = state .ScalingPolicyByTarget (nil , policy .Target )
8470
+ require .NoError (err )
8471
+ require .Nil (out )
8472
+ index , err := state .Index ("scaling_policy" )
8473
+ require .GreaterOrEqual (index , uint64 (1200 ))
8474
+ }
8475
+
8476
+ func TestStateStore_UnstopJob_UpsertScalingPolicies (t * testing.T ) {
8477
+ t .Parallel ()
8478
+
8479
+ require := require .New (t )
8480
+
8481
+ state := testStateStore (t )
8482
+
8483
+ job , policy := mock .JobWithScalingPolicy ()
8484
+ job .Stop = true
8485
+
8486
+ // establish watcher, verify there are no scaling policies yet
8487
+ ws := memdb .NewWatchSet ()
8488
+ list , err := state .ScalingPolicies (ws )
8489
+ require .NoError (err )
8490
+ require .Nil (list .Next ())
8491
+
8492
+ // upsert a stopped job, verify that we don't fire the watcher or add any scaling policies
8493
+ err = state .UpsertJob (1000 , job )
8494
+ require .NoError (err )
8495
+ require .False (watchFired (ws ))
8496
+ // stopped job should have no scaling policies, watcher doesn't fire
8497
+ list , err = state .ScalingPolicies (ws )
8498
+ require .NoError (err )
8499
+ require .Nil (list .Next ())
8500
+
8501
+ // Establish a new watcher
8502
+ ws = memdb .NewWatchSet ()
8503
+ _ , err = state .ScalingPolicies (ws )
8504
+ require .NoError (err )
8505
+ // Unstop this job, say you'll run it again...
8506
+ job .Stop = false
8507
+ err = state .UpsertJob (1100 , job )
8508
+ require .NoError (err )
8509
+
8510
+ // Ensure the scaling policy was added, watch was fired, index was advanced
8511
+ require .True (watchFired (ws ))
8512
+ out , err := state .ScalingPolicyByTarget (nil , policy .Target )
8513
+ require .NoError (err )
8514
+ require .NotNil (out )
8515
+ index , err := state .Index ("scaling_policy" )
8516
+ require .GreaterOrEqual (index , uint64 (1100 ))
8517
+ }
8518
+
8519
+ func TestStateStore_DeleteJob_DeleteScalingPolicies (t * testing.T ) {
8431
8520
t .Parallel ()
8432
8521
8433
8522
require := require .New (t )
0 commit comments