github.com/matrixorigin/matrixone@v1.2.0/pkg/taskservice/cron_state_test.go (about) 1 // Copyright 2024 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package taskservice 16 17 import ( 18 "github.com/stretchr/testify/assert" 19 "testing" 20 ) 21 22 func TestCronServiceState(t *testing.T) { 23 s := &cronServiceState{} 24 // start 25 assert.True(t, s.canStart()) 26 // cannot start when started 27 assert.False(t, s.canStart()) 28 // check inner state 29 assert.True(t, s.started) 30 assert.False(t, s.stopping) 31 // stop 32 assert.True(t, s.canStop()) 33 // cannot stop when stopping 34 assert.False(t, s.canStop()) 35 // check inner state 36 assert.True(t, s.started) 37 assert.True(t, s.stopping) 38 // cannot start during stop 39 assert.False(t, s.canStart()) 40 // end stop 41 s.endStop() 42 // check inner state 43 assert.False(t, s.started) 44 assert.False(t, s.stopping) 45 46 // cannot stop when stopped 47 assert.False(t, s.canStop()) 48 // can start after stop 49 assert.True(t, s.canStart()) 50 } 51 52 func TestCronJobState(t *testing.T) { 53 s := &cronJobState{} 54 55 // can run when not updating 56 assert.True(t, s.canRun()) 57 // cannot run when running 58 assert.False(t, s.canRun()) 59 // check inner state 60 assert.True(t, s.running) 61 assert.False(t, s.updating) 62 63 // cannot update when running 64 assert.False(t, s.canUpdate()) 65 // endRun 66 s.endRun() 67 // check inner state 68 assert.False(t, s.running) 69 assert.False(t, s.updating) 70 71 // can update when not running 72 assert.True(t, s.canUpdate()) 73 // check inner state 74 assert.False(t, s.running) 75 assert.True(t, s.updating) 76 // cannot run when updating 77 assert.False(t, s.canRun()) 78 // endUpdate 79 s.endUpdate() 80 81 // can run after update 82 assert.True(t, s.canRun()) 83 }