@@ -110,5 +110,42 @@ public function testInvoke(): void
110110 self ::assertSame ([['appname ' => 'myapp ' , 'license ' => null ]], TestNewRelicStartTransaction ::all ());
111111 self ::assertSame ([['ignore ' => false ]], TestNewRelicEndTransaction ::all ());
112112 }
113+
114+ public function testInvokeEndsTransactionWhenRequestHandlingFails (): void
115+ {
116+ TestNewRelicStartTransaction ::reset ();
117+ TestNewRelicEndTransaction ::reset ();
118+
119+ $ builder = new MockObjectBuilder ();
120+
121+ /** @var WorkermanTcpConnection $workermanTcpConnection */
122+ $ workermanTcpConnection = $ builder ->create (WorkermanTcpConnection::class, []);
123+
124+ /** @var WorkermanRequest $workermanRequest */
125+ $ workermanRequest = $ builder ->create (WorkermanRequest::class, []);
126+
127+ $ exception = new \RuntimeException ('Request handling failed ' );
128+
129+ $ onMessage = new class ($ exception ) implements OnMessageInterface {
130+ public function __construct (private readonly \RuntimeException $ exception ) {}
131+
132+ public function __invoke (WorkermanTcpConnection $ workermanTcpConnection , WorkermanRequest $ workermanRequest ): void
133+ {
134+ throw $ this ->exception ;
135+ }
136+ };
137+
138+ $ adapter = new NewRelicOnMessageAdapter ($ onMessage , 'myapp ' );
139+
140+ try {
141+ $ adapter ($ workermanTcpConnection , $ workermanRequest );
142+ self ::fail ('Expected the request handler exception to be rethrown. ' );
143+ } catch (\RuntimeException $ caughtException ) {
144+ self ::assertSame ($ exception , $ caughtException );
145+ }
146+
147+ self ::assertSame ([['appname ' => 'myapp ' , 'license ' => null ]], TestNewRelicStartTransaction ::all ());
148+ self ::assertSame ([['ignore ' => false ]], TestNewRelicEndTransaction ::all ());
149+ }
113150 }
114151}
0 commit comments