(b *testing.B)
| 757 | } |
| 758 | |
| 759 | func Benchmark_ShutdownServices_withContextCancellation(b *testing.B) { |
| 760 | benchmarkFn := func(b *testing.B, services []Service, timeout time.Duration) { |
| 761 | b.Helper() |
| 762 | |
| 763 | for b.Loop() { |
| 764 | app := New(Config{ |
| 765 | Services: services, |
| 766 | }) |
| 767 | |
| 768 | err := app.startServices(context.Background()) |
| 769 | if err != nil { |
| 770 | b.Fatal("Expected no error during startup but got", err) |
| 771 | } |
| 772 | |
| 773 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 774 | err = app.shutdownServices(ctx) |
| 775 | // We expect an error here due to the short timeout |
| 776 | if err == nil && timeout < time.Second { |
| 777 | b.Fatal("Expected error due to context cancellation but got none") |
| 778 | } |
| 779 | cancel() |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | b.Run("single-service/immediate-cancellation", func(b *testing.B) { |
| 784 | benchmarkFn(b, []Service{ |
| 785 | &mockService{name: "dep1", terminateDelay: 100 * time.Millisecond}, |
| 786 | }, 10*time.Millisecond) |
| 787 | }) |
| 788 | |
| 789 | b.Run("multiple-services/immediate-cancellation", func(b *testing.B) { |
| 790 | benchmarkFn(b, []Service{ |
| 791 | &mockService{name: "dep1", terminateDelay: 100 * time.Millisecond}, |
| 792 | &mockService{name: "dep2", terminateDelay: 200 * time.Millisecond}, |
| 793 | &mockService{name: "dep3", terminateDelay: 300 * time.Millisecond}, |
| 794 | }, 10*time.Millisecond) |
| 795 | }) |
| 796 | |
| 797 | b.Run("multiple-services/successful-completion", func(b *testing.B) { |
| 798 | const timeout = 500 * time.Millisecond |
| 799 | |
| 800 | for b.Loop() { |
| 801 | app := New(Config{ |
| 802 | Services: []Service{ |
| 803 | &mockService{name: "dep1", terminateDelay: 10 * time.Millisecond}, |
| 804 | &mockService{name: "dep2", terminateDelay: 20 * time.Millisecond}, |
| 805 | &mockService{name: "dep3", terminateDelay: 30 * time.Millisecond}, |
| 806 | }, |
| 807 | }) |
| 808 | |
| 809 | err := app.startServices(context.Background()) |
| 810 | if err != nil { |
| 811 | b.Fatal("Expected no error but got", err) |
| 812 | } |
| 813 | |
| 814 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 815 | err = app.shutdownServices(ctx) |
| 816 | if err != nil { |
nothing calls this directly
no test coverage detected