github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/operator/status_tracker_test.go (about) 1 // Copyright 2020 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Portions of this file are additionally subject to the following 15 // copyright. 16 // 17 // Copyright (C) 2021 Matrix Origin. 18 // 19 // Modified the behavior of the status tracker. 20 21 package operator 22 23 import ( 24 "reflect" 25 "testing" 26 "time" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestSTARTED(t *testing.T) { 32 before := time.Now() 33 trk := NewOpStatusTracker() 34 assert.Equal(t, STARTED, trk.Status()) 35 assert.True(t, reflect.DeepEqual(trk.ReachTimeOf(STARTED), trk.ReachTime())) 36 checkTimeOrder(t, before, trk.ReachTime(), time.Now()) 37 checkReachTime(t, &trk, STARTED) 38 } 39 40 func TestEndStatusTrans(t *testing.T) { 41 allStatus := make([]OpStatus, 0, statusCount) 42 for st := OpStatus(0); st < statusCount; st++ { 43 allStatus = append(allStatus, st) 44 } 45 for from := firstEndStatus; from < statusCount; from++ { 46 trk := NewOpStatusTracker() 47 trk.current = from 48 assert.True(t, trk.IsEnd()) 49 checkInvalidTrans(t, &trk, allStatus...) 50 } 51 } 52 53 func TestStatusCheckExpired(t *testing.T) { 54 // Not expired 55 { 56 before := time.Now() 57 trk := NewOpStatusTracker() 58 after := time.Now() 59 assert.False(t, trk.CheckExpired(10*time.Second)) 60 assert.Equal(t, STARTED, trk.Status()) 61 checkTimeOrder(t, before, trk.ReachTime(), after) 62 } 63 { 64 // Expired but status not changed 65 trk := NewOpStatusTracker() 66 trk.setTime(STARTED, time.Now().Add(-10*time.Second)) 67 assert.True(t, trk.CheckExpired(5*time.Second)) 68 assert.Equal(t, EXPIRED, trk.Status()) 69 } 70 { 71 // Expired and status changed 72 trk := NewOpStatusTracker() 73 before := time.Now() 74 assert.True(t, trk.To(EXPIRED)) 75 after := time.Now() 76 assert.True(t, trk.CheckExpired(0)) 77 assert.Equal(t, EXPIRED, trk.Status()) 78 checkTimeOrder(t, before, trk.ReachTime(), after) 79 } 80 } 81 82 func checkTimeOrder(t *testing.T, t1, t2, t3 time.Time) { 83 assert.True(t, t1.Before(t2)) 84 assert.True(t, t3.After(t2)) 85 } 86 87 func checkInvalidTrans(t *testing.T, trk *OpStatusTracker, sts ...OpStatus) { 88 origin := trk.Status() 89 originTime := trk.ReachTime() 90 sts = append(sts, statusCount, statusCount+1, statusCount+10) 91 for _, st := range sts { 92 assert.False(t, trk.To(st)) 93 assert.Equal(t, origin, trk.Status()) 94 assert.True(t, reflect.DeepEqual(originTime, trk.ReachTime())) 95 } 96 } 97 98 func checkReachTime(t *testing.T, trk *OpStatusTracker, reached ...OpStatus) { 99 reachedMap := make(map[OpStatus]struct{}, len(reached)) 100 for _, st := range reached { 101 assert.False(t, trk.ReachTimeOf(st).IsZero()) 102 reachedMap[st] = struct{}{} 103 } 104 for st := OpStatus(0); st <= statusCount+10; st++ { 105 if _, ok := reachedMap[st]; ok { 106 continue 107 } 108 assert.True(t, trk.ReachTimeOf(st).IsZero()) 109 } 110 }