gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lifecycle.v2/wait_test.go (about) 1 package lifecycle 2 3 import ( 4 "fmt" 5 "gitee.com/sy_183/go-common/errors" 6 "math/rand" 7 "testing" 8 "time" 9 ) 10 11 var ErrRandom = errors.New("random error") 12 13 type RandomLifecycle struct { 14 Lifecycle 15 name string 16 } 17 18 func newRandomLifecycle(name string) *RandomLifecycle { 19 l := &RandomLifecycle{name: name} 20 l.Lifecycle = NewInterruptedWithFunc(l.start, l.run) 21 l.Before(BeforeStart, func(Lifecycle) error { 22 fmt.Printf("%s starting...\n", l.name) 23 return nil 24 }) 25 l.After(AfterStart, func(_ Lifecycle, err error) { 26 if err != nil { 27 fmt.Printf("%s started with error\n", l.name) 28 } else { 29 fmt.Printf("%s started\n", l.name) 30 } 31 }) 32 l.After(AfterClose, func(Lifecycle, error) { 33 fmt.Printf("%s closing...\n", l.name) 34 }) 35 l.After(AfterRun, func(_ Lifecycle, err error) { 36 if err != nil { 37 fmt.Printf("%s closed with error\n", l.name) 38 } else { 39 fmt.Printf("%s closed\n", l.name) 40 } 41 }) 42 return l 43 } 44 45 func (l *RandomLifecycle) randomDuration(start, end time.Duration) time.Duration { 46 return start + time.Duration(rand.Int63n(int64(end-start))) 47 } 48 49 func (l *RandomLifecycle) randomError(rate float64) error { 50 if rand.Float64() < rate { 51 return ErrRandom 52 } 53 return nil 54 } 55 56 func (l *RandomLifecycle) start(_ Lifecycle, interrupter chan struct{}) error { 57 select { 58 case <-time.After(l.randomDuration(time.Second, time.Second*2)): 59 return l.randomError(0.0) 60 case <-interrupter: 61 fmt.Printf("%s starting interrupted\n", l.name) 62 time.Sleep(l.randomDuration(time.Millisecond*500, time.Second)) 63 return ErrInterrupted 64 } 65 } 66 67 func (l *RandomLifecycle) run(_ Lifecycle, interrupter chan struct{}) error { 68 <-interrupter 69 time.Sleep(l.randomDuration(time.Millisecond*500, time.Second)) 70 return l.randomError(0.5) 71 } 72 73 func TestRelationStarter(t *testing.T) { 74 abc_a := NewRelationStarter(newRandomLifecycle("abc_a"), false) 75 aabaca_a := NewRelationStarter(newRandomLifecycle("aabaca_a"), false) 76 aa := NewRelationStarter(newRandomLifecycle("aa"), false, aabaca_a) 77 a := NewRelationStarter(newRandomLifecycle("a"), false, abc_a, aa) 78 ba := NewRelationStarter(newRandomLifecycle("ba"), false, aabaca_a) 79 b := NewRelationStarter(newRandomLifecycle("b"), false, abc_a, ba) 80 ca := NewRelationStarter(newRandomLifecycle("ca"), false, aabaca_a) 81 c := NewRelationStarter(newRandomLifecycle("c"), false, abc_a, ca) 82 starter := NewRelationStarter(nil, false, abc_a, aabaca_a, aa, a, ba, b, ca, c) 83 starter.Start() 84 } 85 86 func TestRelationCloser(t *testing.T) { 87 abc_a := NewRelationCloser(newRandomLifecycle("abc_a")) 88 aabaca_a := NewRelationCloser(newRandomLifecycle("aabaca_a")) 89 aa := NewRelationCloser(newRandomLifecycle("aa"), aabaca_a) 90 a := NewRelationCloser(newRandomLifecycle("a"), abc_a, aa) 91 ba := NewRelationCloser(newRandomLifecycle("ba"), aabaca_a) 92 b := NewRelationCloser(newRandomLifecycle("b"), abc_a, ba) 93 ca := NewRelationCloser(newRandomLifecycle("ca"), aabaca_a) 94 c := NewRelationCloser(newRandomLifecycle("c"), abc_a, ca) 95 96 ParallelStart(nil, 97 abc_a.Lifecycle, 98 aabaca_a.Lifecycle, 99 aa.Lifecycle, 100 a.Lifecycle, 101 ba.Lifecycle, 102 b.Lifecycle, 103 ca.Lifecycle, 104 c.Lifecycle, 105 ) 106 107 closer := NewRelationCloser(nil, abc_a, aabaca_a, aa, a, ba, b, ca, c) 108 closer.Shutdown() 109 }