Fix the random failure by increasing the timeout and introducing few minor refactor/bug fixes Fixes #3970 Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>