(b *testing.B)
| 702 | } |
| 703 | |
| 704 | func Benchmark_StartServices_withContextCancellation(b *testing.B) { |
| 705 | benchmarkFn := func(b *testing.B, services []Service, timeout time.Duration) { |
| 706 | b.Helper() |
| 707 | |
| 708 | for b.Loop() { |
| 709 | app := New(Config{ |
| 710 | Services: services, |
| 711 | }) |
| 712 | |
| 713 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 714 | err := app.startServices(ctx) |
| 715 | // We expect an error here due to the short timeout |
| 716 | if err == nil && timeout < time.Second { |
| 717 | b.Fatal("Expected error due to context cancellation but got none") |
| 718 | } |
| 719 | cancel() |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | b.Run("single-service/immediate-cancellation", func(b *testing.B) { |
| 724 | benchmarkFn(b, []Service{ |
| 725 | &mockService{name: "dep1", startDelay: 100 * time.Millisecond}, |
| 726 | }, 10*time.Millisecond) |
| 727 | }) |
| 728 | |
| 729 | b.Run("multiple-services/immediate-cancellation", func(b *testing.B) { |
| 730 | benchmarkFn(b, []Service{ |
| 731 | &mockService{name: "dep1", startDelay: 100 * time.Millisecond}, |
| 732 | &mockService{name: "dep2", startDelay: 200 * time.Millisecond}, |
| 733 | &mockService{name: "dep3", startDelay: 300 * time.Millisecond}, |
| 734 | }, 10*time.Millisecond) |
| 735 | }) |
| 736 | |
| 737 | b.Run("multiple-services/successful-completion", func(b *testing.B) { |
| 738 | const timeout = 500 * time.Millisecond |
| 739 | |
| 740 | for b.Loop() { |
| 741 | app := New(Config{ |
| 742 | Services: []Service{ |
| 743 | &mockService{name: "dep1", startDelay: 10 * time.Millisecond}, |
| 744 | &mockService{name: "dep2", startDelay: 20 * time.Millisecond}, |
| 745 | &mockService{name: "dep3", startDelay: 30 * time.Millisecond}, |
| 746 | }, |
| 747 | }) |
| 748 | |
| 749 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 750 | err := app.startServices(ctx) |
| 751 | if err != nil { |
| 752 | b.Fatal("Expected no error but got", err) |
| 753 | } |
| 754 | cancel() |
| 755 | } |
| 756 | }) |
| 757 | } |
| 758 | |
| 759 | func Benchmark_ShutdownServices_withContextCancellation(b *testing.B) { |
| 760 | benchmarkFn := func(b *testing.B, services []Service, timeout time.Duration) { |
nothing calls this directly
no test coverage detected