-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathQueueIntegrationTest.php
185 lines (141 loc) · 5.28 KB
/
QueueIntegrationTest.php
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
<?php
namespace Sentry\Laravel\Tests\EventHandler;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sentry\Breadcrumb;
use Sentry\EventType;
use Sentry\Laravel\Tests\TestCase;
use function Sentry\addBreadcrumb;
use function Sentry\captureException;
class QueueIntegrationTest extends TestCase
{
public function testQueueJobPushesAndPopsScopeWithBreadcrumbs(): void
{
dispatch(new QueueEventsTestJobWithBreadcrumb);
$this->assertCount(0, $this->getCurrentSentryBreadcrumbs());
}
public function testQueueJobThatReportsPushesAndPopsScopeWithBreadcrumbs(): void
{
dispatch(new QueueEventsTestJobThatReportsAnExceptionWithBreadcrumb);
$this->assertCount(0, $this->getCurrentSentryBreadcrumbs());
$this->assertNotNull($this->getLastSentryEvent());
$event = $this->getLastSentryEvent();
$this->assertCount(2, $event->getBreadcrumbs());
}
public function testQueueJobThatThrowsLeavesPushedScopeWithBreadcrumbs(): void
{
try {
dispatch(new QueueEventsTestJobThatThrowsAnUnhandledExceptionWithBreadcrumb);
} catch (Exception $e) {
// No action required, expected to throw
}
// We still expect to find the breadcrumbs from the job here so they are attached to reported exceptions
$this->assertCount(2, $this->getCurrentSentryBreadcrumbs());
$firstBreadcrumb = $this->getCurrentSentryBreadcrumbs()[0];
$this->assertEquals('queue.job', $firstBreadcrumb->getCategory());
$secondBreadcrumb = $this->getCurrentSentryBreadcrumbs()[1];
$this->assertEquals('test', $secondBreadcrumb->getCategory());
}
public function testQueueJobsThatThrowPopsAndPushesScopeWithBreadcrumbsBeforeNewJob(): void
{
try {
dispatch(new QueueEventsTestJobThatThrowsAnUnhandledExceptionWithBreadcrumb('test #1'));
} catch (Exception $e) {
// No action required, expected to throw
}
try {
dispatch(new QueueEventsTestJobThatThrowsAnUnhandledExceptionWithBreadcrumb('test #2'));
} catch (Exception $e) {
// No action required, expected to throw
}
// We only expect to find the breadcrumbs from the second job here
$this->assertCount(2, $this->getCurrentSentryBreadcrumbs());
$firstBreadcrumb = $this->getCurrentSentryBreadcrumbs()[0];
$this->assertEquals('queue.job', $firstBreadcrumb->getCategory());
$secondBreadcrumb = $this->getCurrentSentryBreadcrumbs()[1];
$this->assertEquals('test #2', $secondBreadcrumb->getMessage());
}
public function testQueueJobsWithBreadcrumbSetInBetweenKeepsNonJobBreadcrumbsOnCurrentScope(): void
{
dispatch(new QueueEventsTestJobWithBreadcrumb);
addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_INFO, Breadcrumb::LEVEL_DEBUG, 'test2', 'test2'));
dispatch(new QueueEventsTestJobWithBreadcrumb);
$this->assertCount(1, $this->getCurrentSentryBreadcrumbs());
}
protected function withTracingEnabled($app): void
{
$app['config']->set('sentry.traces_sample_rate', 1.0);
}
/**
* @define-env withTracingEnabled
*/
public function testQueueJobCreatesTransactionByDefault(): void
{
dispatch(new QueueEventsTestJob);
$transaction = $this->getLastSentryEvent();
$this->assertNotNull($transaction);
$this->assertEquals(EventType::transaction(), $transaction->getType());
$this->assertEquals(QueueEventsTestJob::class, $transaction->getTransaction());
$traceContext = $transaction->getContexts()['trace'];
$this->assertEquals('queue.process', $traceContext['op']);
}
protected function withQueueJobTracingDisabled($app): void
{
$app['config']->set('sentry.traces_sample_rate', 1.0);
$app['config']->set('sentry.tracing.queue_job_transactions', false);
}
/**
* @define-env withQueueTracingDisabled
*/
public function testQueueJobDoesntCreateTransaction(): void
{
dispatch(new QueueEventsTestJob);
$transaction = $this->getLastSentryEvent();
$this->assertNull($transaction);
}
}
class QueueEventsTestJob implements ShouldQueue
{
public function handle(): void
{
}
}
function queueEventsTestAddTestBreadcrumb($message = null): void
{
addBreadcrumb(
new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::LEVEL_DEBUG,
'test',
$message ?? 'test'
)
);
}
class QueueEventsTestJobWithBreadcrumb implements ShouldQueue
{
public function handle(): void
{
queueEventsTestAddTestBreadcrumb();
}
}
class QueueEventsTestJobThatReportsAnExceptionWithBreadcrumb implements ShouldQueue
{
public function handle(): void
{
queueEventsTestAddTestBreadcrumb();
captureException(new Exception('This is a test exception'));
}
}
class QueueEventsTestJobThatThrowsAnUnhandledExceptionWithBreadcrumb implements ShouldQueue
{
private $message;
public function __construct($message = null)
{
$this->message = $message;
}
public function handle(): void
{
queueEventsTestAddTestBreadcrumb($this->message);
throw new Exception('This is a test exception');
}
}