Backport #33172 by wxiaoguang Fix #33164 and add more tests Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>