-
-
Notifications
You must be signed in to change notification settings - Fork 4
test: verify automatic flush on exit without Stop-Sentry #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jamescrosswell
wants to merge
3
commits into
main
Choose a base branch
from
test/issue-38-flush-on-exit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Regression script for https://github.com/getsentry/sentry-powershell/issues/38 | ||
| # | ||
| # Starts Sentry, captures a message, and exits WITHOUT calling Stop-Sentry to verify that | ||
| # events are still flushed/delivered automatically on process exit (and that the process | ||
| # exits cleanly without hanging). | ||
| # | ||
| # A file-writing transport (FileTransport, defined in utils.ps1) is used instead of a network | ||
| # call so that delivery can be observed from the parent process after this one exits, without | ||
| # relying on networking/open ports in CI. | ||
| # | ||
| # Usage: pwsh -File exit-flush-test-script.ps1 <output-file> | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string] $OutputFile | ||
| ) | ||
|
|
||
| Set-StrictMode -Version latest | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| # When this script is launched cross-edition - e.g. powershell.exe (Windows PowerShell) spawned | ||
| # from a pwsh (PowerShell Core) host - the inherited $env:PSModulePath points only at the launching | ||
| # edition's module directories. That prevents Windows PowerShell from autoloading its built-in | ||
| # modules (notably Microsoft.PowerShell.Utility, which provides Import-PowerShellDataFile used while | ||
| # importing the Sentry module). Reset to the machine default so built-in modules are discoverable. | ||
| if ($PSVersionTable.PSEdition -eq 'Desktop') { | ||
| $env:PSModulePath = [System.Environment]::GetEnvironmentVariable('PSModulePath', 'Machine') | ||
| } | ||
|
|
||
| Import-Module "$PSScriptRoot/../modules/Sentry/Sentry.psd1" | ||
| . "$PSScriptRoot/utils.ps1" | ||
|
|
||
| Start-Sentry { | ||
| $_.Dsn = 'https://key@127.0.0.1/1' | ||
| $_.Transport = [FileTransport]::new($OutputFile) | ||
| } | ||
|
|
||
| $null = [Sentry.SentrySdk]::CaptureMessage('hello-from-exit-flush-test') | ||
|
|
||
| # Intentionally NO Stop-Sentry call here - delivery must happen automatically on exit. | ||
| Write-Host 'CHILD_DONE' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Regression tests for https://github.com/getsentry/sentry-powershell/issues/38 | ||
| # | ||
| # Verifies that a script which captures an event and exits WITHOUT calling Stop-Sentry: | ||
| # * exits cleanly (exit code 0) within a timeout (i.e. does not hang on exit), and | ||
| # * still delivers the captured event. | ||
| # | ||
| # Historically the .NET SDK's automatic flush-on-exit hook (AppDomain.ProcessExit) was disabled | ||
| # in this module because it could hang/crash the process (sentry-dotnet#3141). That was fixed and | ||
| # the workaround was removed in #85, so omitting Stop-Sentry must now Just Work. | ||
|
|
||
| BeforeAll { | ||
| $script:childScript = "$PSScriptRoot$([IO.Path]::DirectorySeparatorChar)exit-flush-test-script.ps1" | ||
|
|
||
| # Runs the exit-flush child script in a separate process and returns whether it exited cleanly | ||
| # within the timeout, its exit code, and the delivered envelope content. | ||
| function Invoke-ExitFlushChild { | ||
| param( | ||
| [Parameter(Mandatory)] [string] $Executable, | ||
| [int] $TimeoutSeconds = 60 | ||
| ) | ||
|
|
||
| $outputFile = [IO.Path]::GetTempFileName() | ||
| # FileTransport appends; start from an empty file. | ||
| Remove-Item $outputFile -ErrorAction SilentlyContinue | ||
|
|
||
| # Use System.Diagnostics.Process directly rather than Start-Process: on Windows PowerShell 5.1 | ||
| # the object returned by `Start-Process -PassThru` does not reliably populate .ExitCode after | ||
| # WaitForExit(timeout), whereas reading it from a Process we started ourselves works on both | ||
| # editions. | ||
| $psi = [System.Diagnostics.ProcessStartInfo]::new() | ||
| $psi.FileName = $Executable | ||
| # ArgumentList isn't available on .NET Framework (WinPS 5.1), so build the argument string. | ||
| $psi.Arguments = '-NoProfile -File "{0}" "{1}"' -f $script:childScript, $outputFile | ||
| $psi.UseShellExecute = $false | ||
| $psi.RedirectStandardOutput = $true | ||
| $psi.RedirectStandardError = $true | ||
| $psi.CreateNoWindow = $true | ||
|
|
||
| $proc = [System.Diagnostics.Process]::Start($psi) | ||
| # Read the streams asynchronously to avoid deadlocking if a pipe buffer fills. | ||
| $stdoutTask = $proc.StandardOutput.ReadToEndAsync() | ||
| $stderrTask = $proc.StandardError.ReadToEndAsync() | ||
|
|
||
| try { | ||
| $exited = $proc.WaitForExit($TimeoutSeconds * 1000) | ||
| if (-not $exited) { | ||
| try { $proc.Kill() } catch {} | ||
Check warningCode scanning / PSScriptAnalyzer Empty catch block is used. Please use Write-Error or throw statements in catch blocks. Warning test
Empty catch block is used. Please use Write-Error or throw statements in catch blocks.
|
||
|
|
||
| return [PSCustomObject]@{ | ||
| Exited = $false | ||
| ExitCode = $null | ||
| Envelope = '' | ||
| StdOut = $stdoutTask.Result | ||
| StdErr = $stderrTask.Result | ||
| } | ||
| } | ||
|
|
||
| return [PSCustomObject]@{ | ||
| Exited = $true | ||
| ExitCode = $proc.ExitCode | ||
| Envelope = (Get-Content -Raw $outputFile -ErrorAction SilentlyContinue) | ||
| StdOut = $stdoutTask.Result | ||
| StdErr = $stderrTask.Result | ||
| } | ||
| } finally { | ||
| $proc.Dispose() | ||
| Remove-Item $outputFile -ErrorAction SilentlyContinue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe 'Automatic flush on exit (without Stop-Sentry)' { | ||
| It 'Windows PowerShell' -Skip:($env:OS -ne 'Windows_NT') { | ||
| $result = Invoke-ExitFlushChild -Executable 'powershell.exe' | ||
| $result.Exited | Should -BeTrue -Because "the process must not hang on exit. StdErr: $($result.StdErr)" | ||
| $result.ExitCode | Should -Be 0 -Because "the process must exit cleanly. StdErr: $($result.StdErr)" | ||
| $result.Envelope | Should -Match 'hello-from-exit-flush-test' -Because 'the captured event must be delivered even without Stop-Sentry' | ||
| } | ||
|
|
||
| It 'PowerShell' { | ||
| $result = Invoke-ExitFlushChild -Executable 'pwsh' | ||
| $result.Exited | Should -BeTrue -Because "the process must not hang on exit. StdErr: $($result.StdErr)" | ||
| $result.ExitCode | Should -Be 0 -Because "the process must exit cleanly. StdErr: $($result.StdErr)" | ||
| $result.Envelope | Should -Match 'hello-from-exit-flush-test' -Because 'the captured event must be delivered even without Stop-Sentry' | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.