I am trying to write testing for my chat API in go, but I am pretty new to testing in go. I can’t find proper examples on how to test more complex functions. For example I have this function where I send messages from a particular websocket to the broadcast channel, however I can’t imagine a way to test it. Could someone help me? here is the function:
func sendMessages(ws *websocket.Conn){
for {
var msg Message
// Read in a new message as JSON and map it to a Message object
err := ws.ReadJSON(&msg)
if err != nil {
log.Printf("error: %v", err)
delete(clients, ws)
break
}
// Send the newly received message to the broadcast channel
broadcast <- msg
saveMessage(msg)
}
}