forked from StartAutomating/Eventful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eventful.tests.ps1
131 lines (115 loc) · 5.12 KB
/
Eventful.tests.ps1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases", "",
Justification="These are smart aliases and part of syntax, and auto-fixing them is destructive.")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "",
Justification="Global variables are the simplest way to test event updates.")]
param()
describe Eventful {
it 'Helps create events' {
$global:WaitAHalfSecond = $false
On@Delay -Wait "00:00:00.5" -Then { $global:WaitAHalfSecond = $true; "Waited half second" }
foreach ($i in 1..4) {
Start-Sleep -Milliseconds 250
}
$global:WaitAHalfSecond | Should -Be $true
}
it 'Has a natural syntax' {
$global:WaitedABit = $false
on delay -Wait '00:00:00.5' -Then { $global:WaitedABit = $true; "Waited half second"}
foreach ($i in 1..4) {
Start-Sleep -Milliseconds 250
}
$global:WaitedABit | Should -Be $true
}
it 'Can handle an arbitrary signal, and send that signal' {
$Global:Fired = $false
on MySignal -Then { $Global:Fired = $true}
send MySignal
Start-Sleep -Milliseconds 250
$Global:Fired | Should -Be $true
}
it 'Can pipe in arbitrary data to Send-Event' {
$randomSourceId = "sourceId$(Get-Random)"
$inputChecksum = 1..3 |
Send-Event -SourceIdentifier $randomSourceId -PassThru |
Select-Object -ExpandProperty MessageData |
Measure-Object -Sum |
Select-Object -ExpandProperty Sum
$inputChecksum | Should -Be (1 + 2 + 3)
}
it 'Can Receive-Event sent by Send-Event, and -Clear them.' {
$randomSourceId = "sourceId$(Get-Random)"
1..3 |
Send-Event -SourceIdentifier $randomSourceId
$outputchecksum = Receive-Event -SourceIdentifier $randomSourceId -Clear |
Select-Object -ExpandProperty MessageData |
Measure-Object -Sum |
Select-Object -ExpandProperty Sum
$outputchecksum | Should -Be (1 + 2 + 3)
(Receive-Event -SourceIdentifier $randomSourceId) | Should -Be $null
}
it 'Can get a signal when a job finishes' {
$global:JobsIsDone = $false
$j = Start-Job -ScriptBlock { Start-Sleep -Milliseconds 750; "done" }
$j|
On@Job -Then { $global:JobsIsDone = $true }
do {
Start-Sleep -Milliseconds 1000
} while ($j.JobStateInfo.State -ne 'Completed')
Start-Sleep -Milliseconds 250
$global:JobsIsDone | Should -Be $true
}
it 'Can take any piped input with an event, and will treat -SourceIdentifier as the EventName' {
$MyTimer = [Timers.Timer]::new()
$MyTimer | Watch-Event -SourceIdentifier Elapsed -Then { "it's time"}
}
it 'Can broadly signal an event by providing an empty -Then {}' {
on delay "00:00:00.1" -Then {} # Signal in a tenth of a second.
Start-Sleep -Milliseconds 250
$eventTimestamp = Get-Event -SourceIdentifier "System.Timers.Timer.*" |
Sort-Object TimeGenerated -Descending |
Select-Object -First 1 -ExpandProperty TimeGenerated
([DateTime]::Now - $eventTimestamp) |
Should -BeLessOrEqual ([Timespan]"00:00:01")
}
it 'Can receive results from event subscriptions' {
on@repeat -interval "00:00:00.1" -Then {1} # Signal every tenth of a second.
Start-Sleep -Milliseconds 250
$receivedResults = @(Get-EventSource -Name Repeat -Subscription | Receive-Event)
$receivedResults.Length | Should -BeGreaterOrEqual 1
}
context EventSources {
it 'Has a number of built-in event source scripts.' {
$es = Get-EventSource
$es |
Select-Object -ExpandProperty Name |
Should -BeLike '@*'
}
it 'Can clear event sources' {
$esCount = @(Get-EventSubscriber).Length
$activeSubCount = @(Clear-EventSource -WhatIf).Length
$activeSubCount | should -BeGreaterThan 0
Clear-EventSource
$esCount2 = @(Get-EventSubscriber).Length
$esCount | Should -BeGreaterThan $esCount2
}
}
context BuiltInEventSources {
it 'Can run Powershell asynchronously' {
On@PowerShellAsync -ScriptBlock { "hello world" } -Then {}
1..4 | Start-Sleep -Milliseconds { 250 }
Get-Event -SourceIdentifier PowerShell.Async.* |
Sort-Object TimeGenerated -Descending |
Select-Object -First 1 -ExpandProperty MessageData |
Should -Be "hello world"
}
it 'Can get a signal when an HTTPResponse is received' {
On@HttpResponse -Uri https://github.com/ -Then {}
1..4 | Start-Sleep -Milliseconds { 250 }
$responseContent = Get-Event -SourceIdentifier "HttpRequest.Completed.*" |
Sort-Object TimeGenerated -Descending |
Select-Object -First 1 -ExpandProperty MessageData |
Select-Object -ExpandProperty ResponseContent
$responseContent | Should -BeLike "*<html*"
}
}
}