@@ -13,67 +13,77 @@ func Test_Acceptance(t *testing.T) {
1313 patent , cancel := context .WithCancel (context .Background ())
1414 defer cancel ()
1515
16- emailTask := & pkg.PoolTask {Id : 1 , Interval : 5 * time .Second , Deadline : 3 * time .Second , Type : "email" ,
17- Call : func (ctx context.Context ) error {
16+ emailEnvelope := & pkg.Envelope {Id : 1 , Interval : 5 * time .Second , Deadline : 3 * time .Second , Type : "email" ,
17+ Invoke : func (ctx context.Context ) error {
1818 time .Sleep (5 * time .Second )
1919 fmt .Println ("📧 Email v1" , time .Now ())
2020 return nil
21- }}
22- tasks := map [string ]* pkg.PoolTask {
23- "Email" : emailTask ,
21+ },
22+ BeforeHook : func (ctx context.Context , item * pkg.Envelope ) error {
23+ fmt .Println ("hook before email" , item .Id , time .Now ())
24+ //return pkg.ErrStopTask
25+ return nil
26+ },
27+ AfterHook : func (ctx context.Context , item * pkg.Envelope ) error {
28+ fmt .Println ("hook after email" , item .Id , time .Now ())
29+ return pkg .ErrStopEnvelope
30+ },
31+ }
32+ envelops := map [string ]* pkg.Envelope {
33+ "Email" : emailEnvelope ,
2434 "Metrics" : {Id : 2 , Interval : 3 * time .Second , Deadline : 1 * time .Second , Type : "metrics" ,
25- Call : func (ctx context.Context ) error {
35+ Invoke : func (ctx context.Context ) error {
2636 fmt .Println ("📧 Metrics" , time .Now ())
2737 return nil
2838 }},
2939 "Food" : {Id : 3 , Interval : 2 * time .Second , Deadline : 1 * time .Second , Type : "food" ,
30- Call : func (ctx context.Context ) error {
40+ Invoke : func (ctx context.Context ) error {
3141 fmt .Println ("📧Fooding" , time .Now ())
3242 return nil
3343 }},
3444 }
35- pool := pkg .NewPool (
45+ envelopeQueue := pkg .NewRateEnvelopeQueue (
3646 pkg .WithLimitOption (3 ),
3747 pkg .WithWaitingOption (true ),
3848 pkg .WithStopModeOption (pkg .Drain ),
3949 pkg .WithLimiterOption (nil ),
4050 pkg .WithWorkqueueConfigOption (nil ),
4151 )
4252
43- pool .Start (patent )
44- err := pool .Add (tasks ["Email" ], tasks ["Metrics" ], tasks ["Food" ], emailTask )
53+ envelopeQueue .Start (patent )
54+ err := envelopeQueue .Add (envelops ["Email" ], envelops ["Metrics" ], envelops ["Food" ], emailEnvelope )
4555 if err != nil {
4656 fmt .Println ("add err:" , err )
4757 }
4858
4959 time .Sleep (1 * time .Second )
50- pool .Stop ()
60+ envelopeQueue .Stop ()
5161
52- pool = pkg .NewPool (
62+ envelopeQueue = pkg .NewRateEnvelopeQueue (
5363 pkg .WithLimitOption (3 ),
5464 pkg .WithWaitingOption (true ),
5565 pkg .WithStopModeOption (pkg .Drain ),
5666 pkg .WithLimiterOption (nil ),
5767 pkg .WithWorkqueueConfigOption (nil ),
5868 )
5969
60- pool .Start (patent )
61- err = pool .Add (tasks ["Email" ], tasks ["Metrics" ], tasks ["Food" ])
70+ envelopeQueue .Start (patent )
71+ err = envelopeQueue .Add (envelops ["Email" ], envelops ["Metrics" ], envelops ["Food" ])
6272 if err != nil {
6373 fmt .Println ("add err:" , err )
6474 }
6575
6676 go func () {
6777 select {
68- case <- time .After (15 * time .Second ):
78+ case <- time .After (25 * time .Second ):
6979 fmt .Println ("main: timeout" )
7080 cancel ()
7181 }
7282 }()
7383
7484 <- patent .Done ()
7585 fmt .Println ("parent: done" )
76- pool .Stop ()
86+ envelopeQueue .Stop ()
7787 //time.Sleep(5 * time.Second)
7888 fmt .Println ("queue: done" )
7989}
0 commit comments