|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/PavelAgarkov/rate-pool/pkg" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_Acceptance(t *testing.T) { |
| 13 | + patent, cancel := context.WithCancel(context.Background()) |
| 14 | + defer cancel() |
| 15 | + |
| 16 | + emailTask := &pkg.PoolTask{Id: 1, Interval: 5 * time.Second, Deadline: 3 * time.Second, Type: "email", |
| 17 | + Call: func(ctx context.Context) error { |
| 18 | + time.Sleep(5 * time.Second) |
| 19 | + fmt.Println("📧 Email v1", time.Now()) |
| 20 | + return nil |
| 21 | + }} |
| 22 | + tasks := map[string]*pkg.PoolTask{ |
| 23 | + "Email": emailTask, |
| 24 | + "Metrics": {Id: 2, Interval: 3 * time.Second, Deadline: 1 * time.Second, Type: "metrics", |
| 25 | + Call: func(ctx context.Context) error { |
| 26 | + fmt.Println("📧 Metrics", time.Now()) |
| 27 | + return nil |
| 28 | + }}, |
| 29 | + "Food": {Id: 3, Interval: 2 * time.Second, Deadline: 1 * time.Second, Type: "food", |
| 30 | + Call: func(ctx context.Context) error { |
| 31 | + fmt.Println("📧Fooding", time.Now()) |
| 32 | + return nil |
| 33 | + }}, |
| 34 | + } |
| 35 | + pool := pkg.NewPool( |
| 36 | + pkg.WithLimitOption(3), |
| 37 | + pkg.WithWaitingOption(true), |
| 38 | + pkg.WithStopModeOption(pkg.Drain), |
| 39 | + pkg.WithLimiterOption(nil), |
| 40 | + pkg.WithWorkqueueConfigOption(nil), |
| 41 | + ) |
| 42 | + |
| 43 | + pool.Start(patent) |
| 44 | + err := pool.Add(tasks["Email"], tasks["Metrics"], tasks["Food"], emailTask) |
| 45 | + if err != nil { |
| 46 | + fmt.Println("add err:", err) |
| 47 | + } |
| 48 | + |
| 49 | + time.Sleep(1 * time.Second) |
| 50 | + pool.Stop() |
| 51 | + |
| 52 | + pool = pkg.NewPool( |
| 53 | + pkg.WithLimitOption(3), |
| 54 | + pkg.WithWaitingOption(true), |
| 55 | + pkg.WithStopModeOption(pkg.Drain), |
| 56 | + pkg.WithLimiterOption(nil), |
| 57 | + pkg.WithWorkqueueConfigOption(nil), |
| 58 | + ) |
| 59 | + |
| 60 | + pool.Start(patent) |
| 61 | + err = pool.Add(tasks["Email"], tasks["Metrics"], tasks["Food"]) |
| 62 | + if err != nil { |
| 63 | + fmt.Println("add err:", err) |
| 64 | + } |
| 65 | + |
| 66 | + go func() { |
| 67 | + select { |
| 68 | + case <-time.After(15 * time.Second): |
| 69 | + fmt.Println("main: timeout") |
| 70 | + cancel() |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + <-patent.Done() |
| 75 | + fmt.Println("parent: done") |
| 76 | + pool.Stop() |
| 77 | + //time.Sleep(5 * time.Second) |
| 78 | + fmt.Println("queue: done") |
| 79 | +} |
0 commit comments