@@ -70,4 +70,58 @@ public function runJob($items)
7070 }
7171 return true ;
7272 }
73+
74+ public function testIsMsgTooBig ()
75+ {
76+ $ job = new BatchJob ('testing ' , array ($ this , 'runJob ' ), 1 );
77+ $ this ->assertTrue ($ job ->isMsgTooBig (7 ));
78+ if (defined ('MSG_E2BIG ' )) {
79+ $ this ->assertTrue ($ job ->isMsgTooBig (constant ('MSG_E2BIG ' )));
80+ }
81+ if (defined ('PCNTL_E2BIG ' )) {
82+ $ this ->assertTrue ($ job ->isMsgTooBig (PCNTL_E2BIG ));
83+ }
84+ if (defined ('SOCKET_E2BIG ' )) {
85+ $ this ->assertTrue ($ job ->isMsgTooBig (SOCKET_E2BIG ));
86+ }
87+ $ this ->assertFalse ($ job ->isMsgTooBig (0 ));
88+ $ this ->assertFalse ($ job ->isMsgTooBig (4 ));
89+ $ this ->assertFalse ($ job ->isMsgTooBig (35 ));
90+ }
91+
92+ public function testDrainOversizedMessage ()
93+ {
94+ if (!extension_loaded ('sysvmsg ' )) {
95+ $ this ->markTestSkipped ('sysvmsg extension required ' );
96+ }
97+ $ key = ftok (__FILE__ , 'B ' );
98+ $ q = msg_get_queue ($ key );
99+ while (@msg_receive ($ q , 0 , $ t , 8192 , $ m , false , MSG_IPC_NOWAIT | MSG_NOERROR , $ e )) {
100+ }
101+ $ job = new BatchJob ('testing ' , array ($ this , 'runJob ' ), 1 );
102+
103+ // Verify drain on an empty queue returns false.
104+ $ this ->assertFalse ($ job ->drainOversizedMessage ($ q ));
105+
106+ // Queue an oversized item and a subsequent valid item.
107+ $ oversizedMessage = str_repeat ('A ' , 100 );
108+ $ nextMessage = 'valid message ' ;
109+ $ this ->assertTrue (msg_send ($ q , 1 , $ oversizedMessage , true , false ));
110+ $ this ->assertTrue (msg_send ($ q , 1 , $ nextMessage , true , false ));
111+
112+ // Attempting to read with a buffer smaller than the message triggers E2BIG.
113+ $ received = @msg_receive ($ q , 0 , $ type , 10 , $ message , true , MSG_IPC_NOWAIT , $ errorcode );
114+ $ this ->assertFalse ($ received );
115+ $ this ->assertTrue ($ job ->isMsgTooBig ($ errorcode ));
116+
117+ // Draining with MSG_NOERROR purges the oversized message and unblocks the queue.
118+ $ this ->assertTrue ($ job ->drainOversizedMessage ($ q ));
119+
120+ // The subsequent message is now at the head of the queue and can be received.
121+ $ receivedNext = @msg_receive ($ q , 0 , $ type , 8192 , $ message , true , MSG_IPC_NOWAIT , $ errorcode );
122+ $ this ->assertTrue ($ receivedNext );
123+ $ this ->assertSame ($ nextMessage , $ message );
124+
125+ msg_remove_queue ($ q );
126+ }
73127}
0 commit comments