Skip to content

Commit

Permalink
added Http with timeout example
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Nov 26, 2023
1 parent f8075f1 commit ab2dbfc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ jobs:
- name: Restore dependencies
run: dotnet restore NBomber.sln
- name: Build
run: dotnet build NBomber.sln --no-restore
- name: Test
run: dotnet test tests/NBomber.IntegrationTests/NBomber.IntegrationTests.fsproj --filter CI!=disable --configuration Release --verbosity normal
run: dotnet build NBomber.sln --no-restore
2 changes: 2 additions & 0 deletions examples/Demo/Features/Timeouts/ScenarioCompletionTimeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class ScenarioCompletionTimeout
{
public void Run()
{
// Docs: https://nbomber.com/docs/nbomber/timeouts

// When NBomber finishes load tests, it waits for all running scenarios to complete their tasks.

var scenario = Scenario.Create("scenario_1", async context =>
Expand Down
48 changes: 48 additions & 0 deletions examples/Demo/HTTP/HttpWithTimeoutExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NBomber.CSharp;
using NBomber.Http;
using NBomber.Http.CSharp;
using NBomber.Plugins.Network.Ping;

namespace Demo.HTTP;

public class HttpWithTimeoutExample
{
public void Run()
{
// Docs: https://nbomber.com/docs/nbomber/timeouts

using var httpClient = new HttpClient();

var scenario = Scenario.Create("http_scenario", async context =>
{
using var timeout = new CancellationTokenSource();
timeout.CancelAfter(50); // the operation will be canceled after 50 ms
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");
//.WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));
// HttpCompletionOption: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcompletionoption?view=net-7.0
var clientArgs = new HttpClientArgs(
httpCompletion: HttpCompletionOption.ResponseContentRead, // or ResponseHeadersRead
cancellationToken: timeout.Token
);
var response = await Http.Send(httpClient, clientArgs, request);
return response;
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.Inject(rate: 100, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)));

NBomberRunner
.RegisterScenarios(scenario)
.WithWorkerPlugins(
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
new HttpMetricsPlugin(new [] { HttpVersion.Version1 })
)
.Run();
}
}
5 changes: 4 additions & 1 deletion examples/Demo/HelloWorld/ScenarioWithTimeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ public void Run()
var scenario = Scenario.Create("scenario_with_timeout", async context =>
{
using var timeout = new CancellationTokenSource();
timeout.CancelAfter(600);
timeout.CancelAfter(600); // the operation will be canceled after 600 ms
// here, we pass CancellationToken via timeout.Token
// and because Task.Delay(1000) is bigger than 600 ms
// the operation will be cancelled
await Task.Delay(1000, timeout.Token);
return Response.Ok(statusCode: "200", sizeBytes: 1000);
Expand Down
1 change: 1 addition & 0 deletions examples/Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

// ---- Timeouts ----
// new ScenarioCompletionTimeout().Run();
// new HttpWithTimeoutExample().Run();

// ----------------
// ----- HTTP -----
Expand Down

0 comments on commit ab2dbfc

Please sign in to comment.