Skip to content

Commit bb4d8f9

Browse files
committed
Minor test tweaks
1 parent 7fc5d53 commit bb4d8f9

File tree

5 files changed

+28
-57
lines changed

5 files changed

+28
-57
lines changed

src/WebJobs.Script/Host/ScriptHostManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public ScriptHost Instance
8686
while (!_stopped);
8787
}
8888

89-
// Let the existing host instance finsih currently executing functions.
89+
// Let the existing host instance finish currently executing functions.
9090
private async Task Orphan(ScriptHost instance)
9191
{
9292
lock (_liveInstances)

test/WebJobs.Script.Tests/NodeEndToEndTests.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.IO;
78
using System.Net;
89
using System.Net.Http;
910
using System.Net.Http.Headers;
1011
using System.Threading.Tasks;
1112
using Newtonsoft.Json.Linq;
1213
using Xunit;
13-
using System.Diagnostics;
1414

1515
namespace WebJobs.Script.Tests
1616
{
1717
public class NodeEndToEndTests : EndToEndTestsBase<NodeEndToEndTests.TestFixture>
1818
{
19+
private const string JobLogTestFileName = "joblog.txt";
20+
1921
public NodeEndToEndTests(TestFixture fixture) : base(fixture)
2022
{
2123
}
@@ -144,39 +146,27 @@ public async Task WebHookTrigger_GenericJson()
144146
[Fact]
145147
public async Task TimerTrigger()
146148
{
147-
var stopwatch = Stopwatch.StartNew();
148-
149-
while (true)
149+
// job is running every second, so give it a few seconds to
150+
// generate some output
151+
await TestHelpers.Await(() =>
150152
{
151-
// Time will write to this file.
152-
try
153-
{
154-
string[] lines = File.ReadAllLines("joblog.txt");
155-
if (lines.Length > 2)
156-
{
157-
return;
158-
}
159-
}
160-
catch
153+
if (File.Exists(JobLogTestFileName))
161154
{
162-
// File may be missing if timer hasn't written yet.
155+
string[] lines = File.ReadAllLines(JobLogTestFileName);
156+
return lines.Length > 2;
163157
}
164-
165-
if (stopwatch.ElapsedMilliseconds > 6*1000)
158+
else
166159
{
167-
Assert.True(false, "Timeout waiting for timer to fire.");
160+
return false;
168161
}
169-
170-
await Task.Delay(TimeSpan.FromSeconds(1));
171-
}
172-
162+
}, timeout: 10 * 1000);
173163
}
174164

175165
public class TestFixture : EndToEndTestFixture
176166
{
177167
public TestFixture() : base(@"TestScripts\Node")
178168
{
179-
File.Delete("joblog.txt");
169+
File.Delete(JobLogTestFileName);
180170
}
181171
}
182172
}

test/WebJobs.Script.Tests/ScriptHostManagerTests.cs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
7-
using System.Net;
8-
using System.Net.Http;
9-
using System.Net.Http.Headers;
6+
using System.Threading;
107
using System.Threading.Tasks;
11-
using Newtonsoft.Json.Linq;
12-
using Xunit;
13-
using System.Diagnostics;
148
using Microsoft.Azure.WebJobs.Script;
15-
using System.Threading;
169
using Microsoft.WindowsAzure.Storage.Blob;
10+
using Xunit;
1711

1812
namespace WebJobs.Script.Tests
1913
{
@@ -30,7 +24,7 @@ public async Task UpdateFileAndRestart()
3024
CancellationTokenSource cts = new CancellationTokenSource();
3125

3226
var fixture = new NodeEndToEndTests.TestFixture();
33-
var blob1 = UpdateOutputName("outputblobname", "first", fixture);
27+
var blob1 = UpdateOutputName("testblob", "first", fixture);
3428

3529
await fixture.Host.StopAsync();
3630
var config = fixture.Host.ScriptConfig;
@@ -41,13 +35,19 @@ public async Task UpdateFileAndRestart()
4135
Thread t = new Thread(_ =>
4236
{
4337
// Wait for initial execution.
44-
Wait(blob1);
38+
TestHelpers.Await(() =>
39+
{
40+
return blob1.Exists();
41+
}, timeout: 10 * 1000).Wait();
4542

4643
// This changes the bindings so that we now write to blob2
4744
var blob2 = UpdateOutputName("first", "second", fixture);
4845

49-
// wait for newly executed
50-
Wait(blob2);
46+
// wait for newly executed
47+
TestHelpers.Await(() =>
48+
{
49+
return blob2.Exists();
50+
}, timeout: 10 * 1000).Wait();
5151

5252
manager.Stop();
5353
});
@@ -59,24 +59,6 @@ public async Task UpdateFileAndRestart()
5959
}
6060
}
6161

62-
// For a blob to appear. This is evidence that the function ran.
63-
void Wait(CloudBlockBlob blob)
64-
{
65-
Stopwatch sw = Stopwatch.StartNew();
66-
const int timeoutMs = 10 * 1000; //
67-
68-
while (!blob.Exists())
69-
{
70-
Thread.Sleep(500);
71-
if (sw.ElapsedMilliseconds > timeoutMs)
72-
{
73-
// If no blob appeared yet, then the function didn't run.
74-
// It may have not picked up the new changes
75-
Assert.True(false, "Timeout waiting for blob to appear. " + timeoutMs + "ms");
76-
}
77-
}
78-
}
79-
8062
// Update the manifest for the timer function
8163
// - this will cause a file touch which cause ScriptHostManager to notice and update
8264
// - set to a new output location so that we can ensure we're getting new changes.
@@ -92,7 +74,6 @@ static CloudBlockBlob UpdateOutputName(string prev, string hint, EndToEndTestFix
9274
var blob = fixture.TestContainer.GetBlockBlobReference(name);
9375
blob.DeleteIfExists();
9476
return blob;
95-
9677
}
9778
}
9879
}

test/WebJobs.Script.Tests/TestScripts/Node/TimerTrigger/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{
1111
"type": "blob",
1212
"name": "output",
13-
"path": "test-output/outputblobname"
13+
"path": "test-output/testblob"
1414
}
1515
]
1616
}

test/WebJobs.Script.Tests/TestScripts/Node/TimerTrigger/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module.exports = function (context) {
44
var timeStamp = new Date().toISOString();
55
fs.appendFile('joblog.txt', timeStamp + '\r\n', function (err) {
6-
var blobOutputContents = "from timer trigger: " + timeStamp;
6+
var blobOutputContents = "From timer trigger: " + timeStamp;
77
context.done(err, blobOutputContents);
88
});
99
}

0 commit comments

Comments
 (0)