@@ -161,6 +161,16 @@ impl EnginePool {
161161 Self :: assemble ( asr_tx, ocr_tx)
162162 }
163163
164+ /// 测试用:注入 ASR 通道并共享 receiver(REQ-267 排空单测专用——
165+ /// 队列中已有请求时 drain_asr_backlog 应逐条回复 Err 并清空)。
166+ #[ cfg( test) ]
167+ pub fn with_asr_channel_and_rx ( asr_tx : Sender < AsrRequest > , asr_rx : Receiver < AsrRequest > ) -> Self {
168+ let ( ocr_tx, _) = mpsc:: channel :: < OcrRequest > ( ) ;
169+ let mut p = Self :: assemble ( asr_tx, ocr_tx) ;
170+ p. asr_rx = Some ( Arc :: new ( Mutex :: new ( asr_rx) ) ) ;
171+ p
172+ }
173+
164174 /// 测试装配:计数/状态 Arc 全部归零或默认(start 与测试构造共用,避免重复)。
165175 #[ cfg( test) ]
166176 fn assemble ( asr_tx : Sender < AsrRequest > , ocr_tx : Sender < OcrRequest > ) -> Self {
@@ -479,6 +489,29 @@ mod tests {
479489 assert_eq ! ( pool. rescore_timeout_count( ) , 1 ) ;
480490 }
481491
492+ #[ test]
493+ fn drain_asr_backlog_replies_err_and_empties_queue ( ) {
494+ // Arrange:持活 receiver + 队列中 2 个待处理请求(REQ-267:worker 串行
495+ // 慢机下调用方超时后积压——排空必须逐条回复 Err 而非滞留陈旧状态)
496+ let ( asr_tx, asr_rx) = mpsc:: channel :: < AsrRequest > ( ) ;
497+ let pool = EnginePool :: with_asr_channel_and_rx ( asr_tx, asr_rx) ;
498+ let ( r1, rr1) = mpsc:: channel ( ) ;
499+ let ( r2, rr2) = mpsc:: channel ( ) ;
500+ pool. asr_tx
501+ . send ( AsrRequest :: TranscribePcm { samples : vec ! [ 0.0f32 ] , sample_rate : 16_000 , reply : r1 } )
502+ . expect ( "send1" ) ;
503+ pool. asr_tx
504+ . send ( AsrRequest :: Transcribe { path : String :: new ( ) , reply : r2 } )
505+ . expect ( "send2" ) ;
506+ // Act:排空
507+ pool. drain_asr_backlog ( ) ;
508+ // Assert:两条积压请求即刻收到可诊断 Err;队列清空后再排为空操作
509+ let e1 = rr1. recv_timeout ( std:: time:: Duration :: from_millis ( 200 ) ) . expect ( "reply1 应即时到达" ) ;
510+ let e2 = rr2. recv_timeout ( std:: time:: Duration :: from_millis ( 200 ) ) . expect ( "reply2 应即时到达" ) ;
511+ assert ! ( e1. is_err( ) && e2. is_err( ) , "积压请求应收到废弃 Err" ) ;
512+ pool. drain_asr_backlog ( ) ; // 不 panic(空队列 no-op)
513+ }
514+
482515 #[ test]
483516 fn recognize_image_timeout_returns_err_on_bounded_wait ( ) {
484517 // Arrange:持活 receiver 的 OCR 通道——send 成功、recv 必超时(无 worker 应答)
0 commit comments