-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathSentryClientTests.cs
More file actions
663 lines (533 loc) · 18.4 KB
/
SentryClientTests.cs
File metadata and controls
663 lines (533 loc) · 18.4 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
using System.Net.Http;
#pragma warning disable CS0618
namespace Sentry.Tests;
[UsesVerify]
public class SentryClientTests
{
private class Fixture
{
public SentryOptions SentryOptions { get; set; } = new();
public IBackgroundWorker BackgroundWorker { get; set; } = Substitute.For<IBackgroundWorker, IDisposable>();
public SentryClient GetSut() => new(SentryOptions, BackgroundWorker);
}
private readonly Fixture _fixture = new();
[Fact]
public void CaptureEvent_ExceptionFiltered_EmptySentryId()
{
_fixture.SentryOptions.AddExceptionFilterForType<SystemException>();
_ = _fixture.BackgroundWorker.EnqueueEnvelope(Arg.Any<Envelope>()).Returns(true);
var sut = _fixture.GetSut();
// Filtered out for it's the exact filtered type
Assert.Equal(default, sut.CaptureException(new SystemException()));
_ = _fixture.BackgroundWorker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
// Filtered for it's a derived type
Assert.Equal(default, sut.CaptureException(new ArithmeticException()));
_ = _fixture.BackgroundWorker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
// Not filtered since it's not in the inheritance chain
Assert.NotEqual(default, sut.CaptureException(new Exception()));
_ = _fixture.BackgroundWorker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureEvent_IdReturnedToString_NoDashes()
{
var sut = _fixture.GetSut();
var evt = new SentryEvent(new Exception());
var actual = sut.CaptureEvent(evt);
var hasDashes = actual.ToString().Contains('-');
Assert.False(hasDashes);
}
[Fact]
public void CaptureEvent_ExceptionProcessorsOnOptions_Invoked()
{
var exceptionProcessor = Substitute.For<ISentryEventExceptionProcessor>();
_fixture.SentryOptions.AddExceptionProcessorProvider(() => new[] { exceptionProcessor });
var sut = _fixture.GetSut();
var evt = new SentryEvent(new Exception());
_ = sut.CaptureEvent(evt);
exceptionProcessor.Received(1).Process(evt.Exception!, evt);
}
[Fact]
public void CaptureEvent_ExceptionProcessorsOnScope_Invoked()
{
var exceptionProcessor = Substitute.For<ISentryEventExceptionProcessor>();
var scope = new Scope();
scope.AddExceptionProcessor(exceptionProcessor);
var sut = _fixture.GetSut();
var evt = new SentryEvent(new Exception());
_ = sut.CaptureEvent(evt, scope);
exceptionProcessor.Received(1).Process(evt.Exception!, evt);
}
[Fact]
public void CaptureEvent_NullEventWithScope_EmptyGuid()
{
var sut = _fixture.GetSut();
Assert.Equal(default, sut.CaptureEvent(null, new Scope(_fixture.SentryOptions)));
}
[Fact]
public void CaptureEvent_NullEvent_EmptyGuid()
{
var sut = _fixture.GetSut();
Assert.Equal(default, sut.CaptureEvent(null));
}
[Fact]
public void CaptureEvent_NullScope_QueuesEvent()
{
var expectedId = Guid.NewGuid();
var expectedEvent = new SentryEvent(eventId: expectedId);
_ = _fixture.BackgroundWorker.EnqueueEnvelope(Arg.Any<Envelope>()).Returns(true);
var sut = _fixture.GetSut();
var actualId = sut.CaptureEvent(expectedEvent);
Assert.Equal(expectedId, (Guid)actualId);
}
[Fact]
public void CaptureEvent_EventAndScope_QueuesEvent()
{
var expectedId = Guid.NewGuid();
var expectedEvent = new SentryEvent(eventId: expectedId);
_ = _fixture.BackgroundWorker.EnqueueEnvelope(Arg.Any<Envelope>()).Returns(true);
var sut = _fixture.GetSut();
var actualId = sut.CaptureEvent(expectedEvent, new Scope(_fixture.SentryOptions));
Assert.Equal(expectedId, (Guid)actualId);
}
[Fact]
public void CaptureEvent_EventAndScope_EvaluatesScope()
{
var scope = new Scope(_fixture.SentryOptions);
var sut = _fixture.GetSut();
var evaluated = false;
object actualSender = null;
scope.OnEvaluating += (sender, _) =>
{
actualSender = sender;
evaluated = true;
};
_ = sut.CaptureEvent(new SentryEvent(), scope);
Assert.True(evaluated);
Assert.Same(scope, actualSender);
}
[Fact]
public void CaptureEvent_EventAndScope_CopyScopeIntoEvent()
{
const string expectedBreadcrumb = "test";
var scope = new Scope(_fixture.SentryOptions);
scope.AddBreadcrumb(expectedBreadcrumb);
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event, scope);
Assert.Equal(scope.Breadcrumbs, @event.Breadcrumbs);
}
[Fact]
public void CaptureEvent_BeforeEvent_RejectEvent()
{
_fixture.SentryOptions.BeforeSend = _ => null;
var expectedEvent = new SentryEvent();
var sut = _fixture.GetSut();
var actualId = sut.CaptureEvent(expectedEvent, new Scope(_fixture.SentryOptions));
Assert.Equal(default, actualId);
_ = _fixture.BackgroundWorker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureEvent_BeforeEvent_ModifyEvent()
{
SentryEvent received = null;
_fixture.SentryOptions.BeforeSend = e => received = e;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);
Assert.Same(@event, received);
}
[Fact]
public void CaptureEvent_LevelOnScope_OverridesLevelOnEvent()
{
const SentryLevel expected = SentryLevel.Fatal;
var @event = new SentryEvent
{
Level = SentryLevel.Fatal
};
var scope = new Scope
{
Level = expected
};
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event, scope);
Assert.Equal(expected, @event.Level);
}
[Fact]
public void CaptureEvent_SamplingLowest_DropsEvent()
{
// Three decimal places longer than what Random returns. Should always drop
_fixture.SentryOptions.SampleRate = 0.00000000000000000001f;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
Assert.Equal(default, sut.CaptureEvent(@event));
}
[Fact]
public void CaptureEvent_SamplingHighest_SendsEvent()
{
// Three decimal places longer than what Random returns. Should always send
_fixture.SentryOptions.SampleRate = 0.99999999999999999999f;
SentryEvent received = null;
_fixture.SentryOptions.BeforeSend = e => received = e;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);
Assert.Same(@event, received);
}
[Fact]
public void CaptureEvent_SamplingNull_DropsEvent()
{
_fixture.SentryOptions.SampleRate = null;
SentryEvent received = null;
_fixture.SentryOptions.BeforeSend = e => received = e;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);
Assert.Same(@event, received);
}
[Fact]
public void CaptureEvent_Sampling50Percent_EqualDistribution()
{
// 15% deviation is ok
const double allowedRelativeDeviation = 0.15;
// Arrange
var client = new SentryClient(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
SampleRate = 0.5f,
MaxQueueItems = int.MaxValue
});
// Act
var eventIds = Enumerable
.Range(0, 1_000)
.Select(i => client.CaptureEvent(new SentryEvent { Message = $"Test[{i}]" }))
.ToArray();
var sampledInEventsCount = eventIds.Count(e => e != SentryId.Empty);
var sampledOutEventsCount = eventIds.Count(e => e == SentryId.Empty);
// Assert
sampledInEventsCount.Should().BeCloseTo(
(int)(0.5 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
sampledOutEventsCount.Should().BeCloseTo(
(int)(0.5 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
}
[Fact]
public void CaptureEvent_Sampling25Percent_AppropriateDistribution()
{
// 15% deviation is ok
const double allowedRelativeDeviation = 0.15;
// Arrange
var client = new SentryClient(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
SampleRate = 0.25f,
MaxQueueItems = int.MaxValue
});
// Act
var eventIds = Enumerable
.Range(0, 1_000)
.Select(i => client.CaptureEvent(new SentryEvent { Message = $"Test[{i}]" }))
.ToArray();
var sampledInEventsCount = eventIds.Count(e => e != SentryId.Empty);
var sampledOutEventsCount = eventIds.Count(e => e == SentryId.Empty);
// Assert
sampledInEventsCount.Should().BeCloseTo(
(int)(0.25 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
sampledOutEventsCount.Should().BeCloseTo(
(int)(0.75 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
}
[Fact]
public void CaptureEvent_Sampling75Percent_AppropriateDistribution()
{
// 15% deviation is ok
const double allowedRelativeDeviation = 0.15;
// Arrange
var client = new SentryClient(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
SampleRate = 0.75f,
MaxQueueItems = int.MaxValue
});
// Act
var eventIds = Enumerable
.Range(0, 1_000)
.Select(i => client.CaptureEvent(new SentryEvent { Message = $"Test[{i}]" }))
.ToArray();
var sampledInEventsCount = eventIds.Count(e => e != SentryId.Empty);
var sampledOutEventsCount = eventIds.Count(e => e == SentryId.Empty);
// Assert
sampledInEventsCount.Should().BeCloseTo(
(int)(0.75 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
sampledOutEventsCount.Should().BeCloseTo(
(int)(0.25 * eventIds.Length),
(uint)(allowedRelativeDeviation * eventIds.Length));
}
[Fact]
public Task CaptureEvent_BeforeEventThrows_ErrorToEventBreadcrumb()
{
var error = new Exception("Exception message!");
_fixture.SentryOptions.BeforeSend = _ => throw error;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);
return Verifier.Verify(@event.Breadcrumbs);
}
[Fact]
public void CaptureEvent_Release_SetFromOptions()
{
const string expectedRelease = "release number";
_fixture.SentryOptions.Release = expectedRelease;
var @event = new SentryEvent();
var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);
Assert.Equal(expectedRelease, @event.Release);
}
[Fact]
public void CaptureEvent_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
var @event = new SentryEvent();
sut.CaptureEvent(@event);
}
[Fact]
public void CaptureUserFeedback_EventIdEmpty_IgnoreUserFeedback()
{
//Arrange
var sut = _fixture.GetSut();
//Act
sut.CaptureUserFeedback(
new UserFeedback(SentryId.Empty, "name", "email", "comment"));
//Assert
_ = sut.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureUserFeedback_ValidUserFeedback_FeedbackRegistered()
{
//Arrange
var sut = _fixture.GetSut();
//Act
sut.CaptureUserFeedback(
new UserFeedback(SentryId.Parse("4eb98e5f861a41019f270a7a27e84f02"), "name", "email", "comment"));
//Assert
_ = sut.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureUserFeedback_EventIdEmpty_FeedbackIgnored()
{
//Arrange
var sut = _fixture.GetSut();
//Act
sut.CaptureUserFeedback(new UserFeedback(SentryId.Empty, "name", "email", "comment"));
//Assert
_ = sut.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void Dispose_should_only_flush()
{
// Arrange
var client = new SentryClient(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
});
// Act
client.Dispose();
//Assert is still usable
client.CaptureEvent(new SentryEvent { Message = "Test" });
}
[Fact]
public void CaptureUserFeedback_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
sut.CaptureUserFeedback(new UserFeedback(SentryId.Empty, "name", "email", "comment"));
}
[Fact]
public void CaptureTransaction_SampledOut_Dropped()
{
// Arrange
var client = _fixture.GetSut();
// Act
client.CaptureTransaction(new Transaction(
"test name",
"test operation"
)
{
IsSampled = false,
EndTimestamp = DateTimeOffset.Now // finished
});
// Assert
_ = client.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_ValidTransaction_Sent()
{
// Arrange
var client = _fixture.GetSut();
// Act
client.CaptureTransaction(
new Transaction(
"test name",
"test operation"
)
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
});
// Assert
_ = client.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_NoSpanId_Ignored()
{
// Arrange
var client = _fixture.GetSut();
var transaction = new Transaction(
"test name",
"test operation"
)
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
};
transaction.Contexts.Trace.SpanId = SpanId.Empty;
// Act
client.CaptureTransaction(transaction);
// Assert
_ = client.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_NoName_Ignored()
{
// Arrange
var client = _fixture.GetSut();
// Act
client.CaptureTransaction(
new Transaction(
null!,
"test operation"
)
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
});
// Assert
_ = client.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_NoOperation_Ignored()
{
// Arrange
var client = _fixture.GetSut();
// Act
client.CaptureTransaction(
new Transaction(
"test name",
null!
)
{
IsSampled = true,
EndTimestamp = DateTimeOffset.Now // finished
});
// Assert
_ = client.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_NotFinished_Sent()
{
// Arrange
var client = _fixture.GetSut();
// Act
client.CaptureTransaction(
new Transaction(
"test name",
"test operation"
)
{
IsSampled = true,
EndTimestamp = null // not finished
});
// Assert
_ = client.Worker.Received(1).EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
public void CaptureTransaction_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
sut.CaptureTransaction(
new Transaction(
"test name",
"test operation")
{
IsSampled = true,
EndTimestamp = null // not finished
});
}
[Fact]
public void Dispose_Worker_FlushCalled()
{
var client = _fixture.GetSut();
client.Dispose();
_fixture.BackgroundWorker?.Received(1).FlushAsync(_fixture.SentryOptions.ShutdownTimeout);
}
[Fact]
public void Dispose_MultipleCalls_WorkerFlushedTwice()
{
var sut = _fixture.GetSut();
sut.Dispose();
sut.Dispose();
_fixture.BackgroundWorker?.Received(2).FlushAsync(_fixture.SentryOptions.ShutdownTimeout);
}
[Fact]
public void IsEnabled_AlwaysTrue()
{
var sut = _fixture.GetSut();
Assert.True(sut.IsEnabled);
}
[Fact]
public void Ctor_NullSentryOptions_ThrowsArgumentNullException()
{
_fixture.SentryOptions = null;
var ex = Assert.Throws<ArgumentNullException>(() => _fixture.GetSut());
Assert.Equal("options", ex.ParamName);
}
[Fact]
public void Ctor_HttpOptionsCallback_InvokedConfigureClient()
{
var invoked = false;
_fixture.BackgroundWorker = null;
_fixture.SentryOptions.Dsn = DsnSamples.ValidDsnWithSecret;
_fixture.SentryOptions.ConfigureClient = _ => invoked = true;
using (_fixture.GetSut())
{
Assert.True(invoked);
}
}
[Fact]
public void Ctor_CreateHttpClientHandler_InvokedConfigureHandler()
{
var invoked = false;
_fixture.BackgroundWorker = null;
_fixture.SentryOptions.Dsn = DsnSamples.ValidDsnWithSecret;
_fixture.SentryOptions.CreateHttpClientHandler = () =>
{
invoked = true;
return Substitute.For<HttpClientHandler>();
};
using (_fixture.GetSut())
{
Assert.True(invoked);
}
}
[Fact]
public void Ctor_NullBackgroundWorker_ConcreteBackgroundWorker()
{
_fixture.SentryOptions.Dsn = DsnSamples.ValidDsnWithSecret;
using var sut = new SentryClient(_fixture.SentryOptions);
_ = Assert.IsType<BackgroundWorker>(sut.Worker);
}
}