Skip to content

Commit 73ecd41

Browse files
committed
re-enabling Event Hubs tests; moving extensions tests to their own classes
1 parent 9c35f78 commit 73ecd41

File tree

15 files changed

+317
-261
lines changed

15 files changed

+317
-261
lines changed

sample/EventHubTrigger/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"cardinality": "many",
77
"direction": "in",
88
"connection": "AzureWebJobsEventHubReceiver",
9-
"path": "%AzureWebJobsEventHubPath%"
9+
"eventHubName": "%AzureWebJobsEventHubPath%"
1010
}
1111
]
1212
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Threading.Tasks;
55
using Xunit;
66

7-
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDBTrigger
7+
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB
88
{
9-
public class CosmosDBTriggerCSharpEndToEndTests :
10-
CosmosDBTriggerEndToEndTestsBase<CosmosDBTriggerCSharpEndToEndTests.TestFixture>
9+
public class CosmosDBCSharpEndToEndTests :
10+
CosmosDBEndToEndTestsBase<CosmosDBCSharpEndToEndTests.TestFixture>
1111
{
12-
public CosmosDBTriggerCSharpEndToEndTests(TestFixture fixture) : base(fixture)
12+
public CosmosDBCSharpEndToEndTests(TestFixture fixture) : base(fixture)
1313
{
1414
}
1515

@@ -19,7 +19,13 @@ public Task CosmosDBTrigger()
1919
return CosmosDBTriggerToBlobTest();
2020
}
2121

22-
public class TestFixture : CosmosDBTriggerTestFixture
22+
[Fact]
23+
public Task CosmosDB()
24+
{
25+
return CosmosDBTest();
26+
}
27+
28+
public class TestFixture : CosmosDBTestFixture
2329
{
2430
private const string ScriptRoot = @"TestScripts\CSharp";
2531

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
using Microsoft.Azure.Documents;
99
using Microsoft.Azure.Documents.Client;
1010
using Microsoft.Azure.WebJobs.Host;
11+
using Microsoft.WindowsAzure.Storage.Queue;
1112
using Xunit;
1213

13-
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDBTrigger
14+
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB
1415
{
15-
public abstract class CosmosDBTriggerEndToEndTestsBase<TTestFixture> :
16-
EndToEndTestsBase<TTestFixture> where TTestFixture : CosmosDBTriggerTestFixture, new()
16+
public abstract class CosmosDBEndToEndTestsBase<TTestFixture> :
17+
EndToEndTestsBase<TTestFixture> where TTestFixture : CosmosDBTestFixture, new()
1718
{
18-
public CosmosDBTriggerEndToEndTestsBase(TTestFixture fixture) : base(fixture)
19+
public CosmosDBEndToEndTestsBase(TTestFixture fixture) : base(fixture)
1920
{
2021
}
2122

@@ -53,17 +54,43 @@ protected async Task CosmosDBTriggerToBlobTest()
5354

5455
Assert.False(string.IsNullOrEmpty(result));
5556
}
57+
58+
protected async Task CosmosDBTest()
59+
{
60+
// DocumentDB tests need the following environment vars:
61+
// "AzureWebJobsDocumentDBConnectionString" -- the connection string to the account
62+
string id = Guid.NewGuid().ToString();
63+
64+
await Fixture.Host.BeginFunctionAsync("CosmosDBOut", id);
65+
66+
Document doc = await WaitForDocumentAsync(id);
67+
68+
Assert.Equal(doc.Id, id);
69+
70+
// Now add that Id to a Queue, in an object to test binding
71+
var queue = await Fixture.GetNewQueue("documentdb-input");
72+
string messageContent = string.Format("{{ \"documentId\": \"{0}\" }}", id);
73+
await queue.AddMessageAsync(new CloudQueueMessage(messageContent));
74+
75+
// And wait for the text to be updated
76+
Document updatedDoc = await WaitForDocumentAsync(id, "This was updated!");
77+
78+
Assert.Equal(updatedDoc.Id, doc.Id);
79+
Assert.NotEqual(doc.ETag, updatedDoc.ETag);
80+
}
81+
5682
}
5783

58-
public abstract class CosmosDBTriggerTestFixture : EndToEndTestFixture
84+
public abstract class CosmosDBTestFixture : EndToEndTestFixture
5985
{
60-
protected CosmosDBTriggerTestFixture(string rootPath, string testId) : base(rootPath, testId)
86+
protected CosmosDBTestFixture(string rootPath, string testId) :
87+
base(rootPath, testId, "Microsoft.Azure.WebJobs.Extensions.CosmosDB", "3.0.0-beta7-10602")
6188
{
6289
}
6390

6491
public DocumentClient DocumentClient { get; private set; }
6592

66-
protected override IEnumerable<string> GetActiveFunctions() => new[] { "CosmosDBTrigger" };
93+
protected override IEnumerable<string> GetActiveFunctions() => new[] { "CosmosDBTrigger", "CosmosDBIn", "CosmosDBOut" };
6794

6895
public async Task InitializeDocumentClient()
6996
{
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Threading.Tasks;
55
using Xunit;
66

7-
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDBTrigger
7+
namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB
88
{
9-
public class CosmosDBTriggerNodeEndToEndTests :
10-
CosmosDBTriggerEndToEndTestsBase<CosmosDBTriggerNodeEndToEndTests.TestFixture>
9+
public class CosmosDBNodeEndToEndTests :
10+
CosmosDBEndToEndTestsBase<CosmosDBNodeEndToEndTests.TestFixture>
1111
{
12-
public CosmosDBTriggerNodeEndToEndTests(TestFixture fixture) : base(fixture)
12+
public CosmosDBNodeEndToEndTests(TestFixture fixture) : base(fixture)
1313
{
1414
}
1515

@@ -19,7 +19,13 @@ public Task CosmosDBTrigger()
1919
return CosmosDBTriggerToBlobTest();
2020
}
2121

22-
public class TestFixture : CosmosDBTriggerTestFixture
22+
[Fact]
23+
public Task CosmosDB()
24+
{
25+
return CosmosDBTest();
26+
}
27+
28+
public class TestFixture : CosmosDBTestFixture
2329
{
2430
private const string ScriptRoot = @"TestScripts\Node";
2531

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Azure.EventHubs;
8+
using Microsoft.Azure.WebJobs.Script.Config;
9+
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Linq;
11+
using Xunit;
12+
13+
namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.EventHubs
14+
{
15+
public class EventHubSamplesEndToEndTests : IClassFixture<EventHubSamplesEndToEndTests.TestFixture>
16+
{
17+
private TestFixture _fixture;
18+
19+
public EventHubSamplesEndToEndTests(TestFixture fixture)
20+
{
21+
_fixture = fixture;
22+
}
23+
24+
[Fact]
25+
public async Task EventHubTrigger()
26+
{
27+
// write 3 events
28+
List<EventData> events = new List<EventData>();
29+
string[] ids = new string[3];
30+
for (int i = 0; i < 3; i++)
31+
{
32+
ids[i] = Guid.NewGuid().ToString();
33+
JObject jo = new JObject
34+
{
35+
{ "value", ids[i] }
36+
};
37+
var evt = new EventData(Encoding.UTF8.GetBytes(jo.ToString(Formatting.None)));
38+
evt.Properties.Add("TestIndex", i);
39+
events.Add(evt);
40+
}
41+
42+
string connectionString = Environment.GetEnvironmentVariable("AzureWebJobsEventHubSender");
43+
EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(connectionString);
44+
45+
if (string.IsNullOrWhiteSpace(builder.EntityPath))
46+
{
47+
string eventHubPath = ScriptSettingsManager.Instance.GetSetting("AzureWebJobsEventHubPath");
48+
builder.EntityPath = eventHubPath;
49+
}
50+
51+
EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
52+
53+
await eventHubClient.SendAsync(events);
54+
55+
string logs = null;
56+
await TestHelpers.Await(() =>
57+
{
58+
// wait until all of the 3 of the unique IDs sent
59+
// above have been processed
60+
logs = _fixture.Host.GetLog();
61+
return ids.All(p => logs.Contains(p));
62+
});
63+
64+
Assert.True(logs.Contains("IsArray true"));
65+
}
66+
67+
public class TestFixture : EndToEndTestFixture
68+
{
69+
public TestFixture() :
70+
base(Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\sample"), "samples", "Microsoft.Azure.WebJobs.Extensions.EventHubs", "3.0.0-beta4-11268")
71+
{
72+
}
73+
74+
protected override IEnumerable<string> GetActiveFunctions() => new[] { "EventHubTrigger" };
75+
}
76+
}
77+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Newtonsoft.Json.Linq;
5+
using Xunit;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.Tests.Integration.EventHubs
8+
{
9+
public class EventHubsEndToEndTestsBase : IClassFixture<EventHubsEndToEndTestsBase.TestFixture>
10+
{
11+
private EndToEndTestFixture _fixture;
12+
13+
public EventHubsEndToEndTestsBase(TestFixture fixture)
14+
{
15+
_fixture = fixture;
16+
}
17+
18+
[Fact]
19+
public async Task EventHub()
20+
{
21+
// Event Hub needs the following environment vars:
22+
// "AzureWebJobsEventHubSender" - the connection string for the send rule
23+
// "AzureWebJobsEventHubReceiver" - the connection string for the receiver rule
24+
// "AzureWebJobsEventHubPath" - the path
25+
26+
// Test both sending and receiving from an EventHub.
27+
// First, manually invoke a function that has an output binding to send EventDatas to an EventHub.
28+
// This tests the ability to queue eventhhubs
29+
string testData = Guid.NewGuid().ToString();
30+
31+
await _fixture.Host.BeginFunctionAsync("EventHubSender", testData);
32+
33+
// Second, there's an EventHub trigger listener on the events which will write a blob.
34+
// Once the blob is written, we know both sender & listener are working.
35+
var resultBlob = _fixture.TestOutputContainer.GetBlockBlobReference(testData);
36+
string result = await TestHelpers.WaitForBlobAndGetStringAsync(resultBlob);
37+
38+
var payload = JObject.Parse(result);
39+
Assert.Equal(testData, (string)payload["id"]);
40+
41+
var bindingData = payload["bindingData"];
42+
int sequenceNumber = (int)bindingData["sequenceNumber"];
43+
var systemProperties = bindingData["systemProperties"];
44+
Assert.Equal(sequenceNumber, (int)systemProperties["SequenceNumber"]);
45+
}
46+
47+
public class TestFixture : EndToEndTestFixture
48+
{
49+
public TestFixture() :
50+
base(@"TestScripts\Node", "node", "Microsoft.Azure.WebJobs.Extensions.EventHubs", "3.0.0-beta4-11268")
51+
{
52+
}
53+
54+
protected override IEnumerable<string> GetActiveFunctions() => new[] { "EventHubSender", "EventHubTrigger" };
55+
}
56+
}
57+
}

test/WebJobs.Script.Tests.Integration/TestScripts/Node/EventHubSender/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"name": "output",
1111
"direction": "out",
1212
"connection": "AzureWebJobsEventHubSender",
13-
"path": "%AzureWebJobsEventHubPath%"
13+
"eventHubName": "%AzureWebJobsEventHubPath%"
1414
}
1515
]
1616
}

test/WebJobs.Script.Tests.Integration/TestScripts/Node/EventHubTrigger/function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "workItem",
66
"direction": "in",
77
"connection": "AzureWebJobsEventHubReceiver",
8-
"path": "%AzureWebJobsEventHubPath%",
8+
"eventHubName": "%AzureWebJobsEventHubPath%",
99
"consumerGroup": "$Default"
1010
},
1111
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"id": "function-tests-node",
2+
"id": "function-tests-node"
33
}

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/CSharpEndToEndTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ public void MobileTables()
5252
// await MobileTablesTest(isDotNet: true);
5353
}
5454

55-
[Fact]
56-
public async Task CosmosDB()
57-
{
58-
await CosmosDBTest();
59-
}
60-
6155
[Fact(Skip = "Not yet enabled.")]
6256
public void NotificationHub()
6357
{

0 commit comments

Comments
 (0)