Skip to content

Commit 3eaad48

Browse files
committed
feat: ensure exception is recorded
1 parent e2faa36 commit 3eaad48

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

projects/Test/Common/ActivityRecorder.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public static KeyValuePair<string, object> HasTag(Activity activity, string name
130130
{
131131
Assert.Fail($"The Activity tags should contain {name}.");
132132
}
133+
133134
return tag;
134135
}
135136

@@ -139,6 +140,14 @@ public static void HasTag<T>(Activity activity, string name, T expectedValue)
139140
Assert.Equal(expectedValue, (T)tag.Value);
140141
}
141142

143+
public static void HasRecordedException(this Activity activity, Exception exception)
144+
{
145+
var exceptionEvent = activity.Events.First();
146+
Assert.Equal("exception", activity.Events.First().Name);
147+
Assert.Equal(exception.GetType().ToString(),
148+
exceptionEvent.Tags.SingleOrDefault(t => t.Key == "exception.type").Value);
149+
}
150+
142151
public static void HasNoTag(Activity activity, string name)
143152
{
144153
bool contains = activity.TagObjects.Any(t => t.Key == name);
@@ -147,7 +156,8 @@ public static void HasNoTag(Activity activity, string name)
147156

148157
public static void FinishedInOrder(Activity first, Activity second)
149158
{
150-
Assert.True(first.StartTimeUtc + first.Duration < second.StartTimeUtc + second.Duration, $"{first.OperationName} should stop before {second.OperationName}");
159+
Assert.True(first.StartTimeUtc + first.Duration < second.StartTimeUtc + second.Duration,
160+
$"{first.OperationName} should stop before {second.OperationName}");
151161
}
152162

153163
public static string CamelToSnake(string camel)
@@ -162,8 +172,10 @@ public static string CamelToSnake(string camel)
162172
{
163173
bld.Append('_');
164174
}
175+
165176
bld.Append(char.ToLower(c));
166177
}
178+
167179
return bld.ToString();
168180
}
169181
}

projects/Test/Integration/TestConnectionFactory.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,24 @@ public async Task TestCreateConnectionRegisterAnActivity()
444444
ConnectionFactory cf = CreateConnectionFactory();
445445
await using IConnection conn = await cf.CreateConnectionAsync();
446446
recorder.VerifyActivityRecordedOnce();
447-
448447
await conn.CloseAsync();
449448
}
449+
450+
[Fact]
451+
public async Task TestCreateConnectionWithFailureRecordException()
452+
{
453+
using ActivityRecorder recorder =
454+
new ActivityRecorder(RabbitMQActivitySource.ConnectionSourceName, "connection attempt");
455+
ConnectionFactory cf = CreateConnectionFactory();
456+
cf.AutomaticRecoveryEnabled = true;
457+
var unreachablePort = 1234;
458+
var ep = new AmqpTcpEndpoint("localhost", unreachablePort);
459+
var exception = await Assert.ThrowsAsync<BrokerUnreachableException>(() =>
460+
{
461+
return cf.CreateConnectionAsync(new List<AmqpTcpEndpoint> { ep });
462+
});
463+
Activity activity = recorder.VerifyActivityRecordedOnce();
464+
activity.HasRecordedException(exception);
465+
}
450466
}
451467
}

0 commit comments

Comments
 (0)