看板 Programming 關於我們 聯絡資訊
小弟這週接觸 golang,是一位新手 目前遭遇到一個問題, 測試一個會 return chan type 的 func 測試程式呼叫到此 func 時,會 block 就此沒有回應 想問版上大大們,該如何正確測試這類型的 function ? 有關詳細程式碼描述放在 stack overflow 連結如下, https://stackoverflow.com/questions/75381842/golang-how-to-test-function-that-return-channel-type -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.149.125 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1675838914.A.122.html
lycantrope: Done似乎要回傳一個chan內含nil 133.51.216.27 02/08 15:15
rwweeeh: 試過了 在DoAndReturn回傳 還是不work118.163.149.125 02/08 15:18
lycantrope: 回傳chan error放nil一樣錯誤喔? 133.51.216.27 02/08 15:29
terter: 你直接return make(chan err), 然後丟nil 27.105.100.6 02/08 15:39
terter: 進去吧。直接給nil,你等半天也不會結束 27.105.100.6 02/08 15:40
terter: 補充,總之不能直接return nil, 它會卡 27.105.100.6 02/08 15:44
terter: 你要產一個make(chan error), 然後那個 27.105.100.6 02/08 15:45
terter: func() chan error那邊return那個chan 27.105.100.6 02/08 15:45
terter: 最後用go func丟個nil進去那個chan吧 27.105.100.6 02/08 15:45
rwweeeh: 大大 嘗試過你們的方式118.163.149.125 02/08 16:08
rwweeeh: 改法補充在stackoverflow最後面 不work118.163.149.125 02/08 16:09
terter: 你不能把ch<-nil放在那,會卡在那邊,請用go 122.116.86.61 02/08 21:47
terter: func(){ch <- nil } 來丟 122.116.86.61 02/08 21:47
terter: 或是你make chan要加大buffer不然會堵塞 122.116.86.61 02/08 21:51
lycantrope: CreateVService有mock嗎 138.199.39.153 02/08 23:08
rwweeeh: 謝各位熱心回答,正確寫法補充在文章內118.163.149.125 02/09 16:45