-
Notifications
You must be signed in to change notification settings - Fork 18
/
ParallelRunspace.Tests.ps1
93 lines (74 loc) · 3.02 KB
/
ParallelRunspace.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
#requires -Module PowerShellLogging, ThreadJob
param($Count = 2)
Describe "Working when called simultaneously in parallel runspaces" -Tag "ThreadJob", "WIP" {
$Path = "TestDrive:\log{0}.txt"
$Path = (Join-Path (Convert-Path (Split-Path $Path)) (Split-Path $Path -Leaf))
$TestScript = "
Import-Module PowerShellLogging
`$Logging = Enable-LogFile -Path '${Path}'
Write-Host 'This is a host test from attempt {0}'
'${Path}'
Start-Sleep 2
Write-Verbose 'This is a verbose test from attempt {0}' -Verbose
Disable-LogFile `$Logging
"
$TestScript = {
param($Path, $Index)
Import-Module PowerShellLogging
$Logging = Enable-LogFile -Path $Path
Microsoft.PowerShell.Utility\Write-Host "This is a host test from attempt $index"
$Path
Microsoft.PowerShell.Utility\Write-Verbose 'This is a verbose test' -Verbose
# Does not output, because Verbose is suppressed
Microsoft.PowerShell.Utility\Write-Verbose "This is a verbose test from attempt $index" -Verbose
Disable-LogFile $Logging
}
It "Should not crash when used" {
$script:Results = & {
foreach ($i in 1..$Count) {
Start-ThreadJob $TestScript -ArgumentList ($Path -f $i), $i
}
} | Wait-Job | Receive-Job -ErrorVariable Ev
$Script:Ev = $Ev
}
It "Should not interfere with output" {
$script:Results.Count | Should -Be $Count
$i = 0
foreach ($resultPath in $script:Results) {
$i += 1
$resultPath | Should -Be ($Path -f $i)
}
}
It "Should not cause Enable-LogFile to fail" {
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Enable-LogFile"
}
It "Should not cause Write-Host to fail" {
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Host"
}
It "Should not cause Write-Verbose to fail" {
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Write-Verbose"
}
It "Should not cause Disable-LogFile to fail" {
$script:Ev.InvocationInfo.MyCommand.Name | Should -Not -Contain "Disable-LogFile"
}
It "Should not cause any errors" {
$script:Ev.Count | Should -Be 0
}
Write-Warning "Expecting $($script:Results.Count) log files!"
It "Should create the log file" {
# this is enough to prove the logging works
foreach ($i in 1..$Count) {
($Path -f $i) | Should -Exist
}
}
foreach ($i in 1..$Count) {
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
(Get-Content ($Path -f $i)) -match "This is a host test from attempt $i$" | Should -Not -BeNullOrEmpty
}
}
foreach ($i in 1..$Count) {
It "Should log host output to $($Path -f $i)" -Skip:(!(Test-Path ($Path -f $i))) {
(Get-Content ($Path -f $i)) -match "This is a verbose test from attempt $i$" | Should -Not -BeNullOrEmpty
}
}
}