File size: 2,015 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "Event Subscriber Tests" -Tags "Feature" {
    BeforeEach {
        Get-EventSubscriber | Unregister-Event
    }
    AfterEach {
        Get-EventSubscriber | Unregister-Event
    }

    # can't let this case to work
    It "Register an event with no action, trigger it and wait for it to be raised." -Pending:$true{
        Get-EventSubscriber | Should -BeNullOrEmpty
        $messageData = New-Object psobject
        $job = Start-Job { Start-Sleep -Seconds 5; 1..5 }
        $null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData
        New-Event EventSIDTest

        Wait-Event EventSIDTest
        $eventdata = Get-Event EventSIDTest
        $eventdata.MessageData | Should -Be $messageData
        Remove-Event EventSIDTest
        Unregister-Event EventSIDTest
        Get-EventSubscriber | Should -BeNullOrEmpty
    }

    It "Access a global variable from an event action." {
        Get-EventSubscriber | Should -BeNullOrEmpty
        Set-Variable incomingGlobal -Scope global -Value globVarValue
        $null = Register-EngineEvent -SourceIdentifier foo -Action {Set-Variable -Scope global -Name aglobalvariable -Value $incomingGlobal}
        New-Event foo
        $getvar = Get-Variable aglobalvariable -Scope global
        $getvar.Name | Should -Be aglobalvariable
        $getvar.Value | Should -Be globVarValue
        Unregister-Event foo
        Get-EventSubscriber | Should -BeNullOrEmpty
    }

    It 'Should not throw when having finally block in Powershell.Exiting Action scriptblock' {
        $pwsh = "$PSHOME/pwsh"
        $output = & $pwsh {
        Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {
            try{
                try{} finally{}
            }
            catch{ Write-Host "Exception" -NoNewline }
        }
        } | Out-String

        $output | Should -Not -BeLike "*Exception*"
    }
}