summary refs log tree commit diff
path: root/testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-03 01:01:40 +0200
committerRory& <root@rory.gay>2025-06-03 01:01:40 +0200
commit6f3f08ed340e59a62a2d0428a5c32f99551ef1ce (patch)
treeff77390b1d3ea61414c14c94ac1fa2a05030879b /testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs
parentMore alarm testing (diff)
downloadnodejs-final-assignment-6f3f08ed340e59a62a2d0428a5c32f99551ef1ce.tar.xz
Fix performance issues, add fake user bot to test client, more testing
Diffstat (limited to 'testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs')
-rw-r--r--testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs b/testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs
new file mode 100644

index 0000000..7835f89 --- /dev/null +++ b/testFrontend/SafeNSound.FakeUser/RandomAlarmService.cs
@@ -0,0 +1,35 @@ +namespace SafeNSound.FakeUser; + +public class RandomAlarmService(UserStore userStore): IHostedService { + private Task? _listenerTask; + private readonly CancellationTokenSource _cts = new(); + + public async Task StartAsync(CancellationToken cancellationToken) { + _listenerTask = Run(_cts.Token); + } + + private async Task Run(CancellationToken cancellationToken) { + while (!cancellationToken.IsCancellationRequested) { + try { + var user = userStore.GetRandomUser(); + if (Random.Shared.Next(100) > 90) { + await user.Client!.SetAlarm(new Sdk.AlarmDto { + Reason = "fall" + }); + } + else { + await user.Client!.DeleteAlarm(); + } + } + catch (Exception ex) { + Console.WriteLine($"Error setting/deleting alarm: {ex.Message}"); + } + + await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken); + } + } + + public async Task StopAsync(CancellationToken cancellationToken) { + await _cts.CancelAsync(); + } +} \ No newline at end of file