Using precisely sized buffers for tcp tests

160-regression
rcorniere 4 years ago
parent 1f5591f33a
commit fd48f52f3d

@ -20,6 +20,7 @@ type ConnState = uint8
// This is a the list of events happening on the connection that the // This is a the list of events happening on the connection that the
// client can be notified about. // client can be notified about.
const ( const (
InitialPresence = "<presence/>"
StateDisconnected ConnState = iota StateDisconnected ConnState = iota
StateConnected StateConnected
StateSessionEstablished StateSessionEstablished
@ -199,7 +200,7 @@ func (c *Client) Resume(state SMState) error {
//fmt.Fprintf(client.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", "chat", "Online") //fmt.Fprintf(client.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", "chat", "Online")
// TODO: Do we always want to send initial presence automatically ? // TODO: Do we always want to send initial presence automatically ?
// Do we need an option to avoid that or do we rely on client to send the presence itself ? // Do we need an option to avoid that or do we rely on client to send the presence itself ?
err = c.sendWithWriter(c.transport, []byte("<presence/>")) err = c.sendWithWriter(c.transport, []byte(InitialPresence))
return err return err
} }

@ -184,15 +184,15 @@ func TestClient_SendIQ(t *testing.T) {
select { select {
case <-res: // If the server responds with an IQ, we pass the test case <-res: // If the server responds with an IQ, we pass the test
case err := <-errChan: // If the server sends an error, or there is a connection error case err := <-errChan: // If the server sends an error, or there is a connection error
t.Errorf(err.Error()) t.Fatal(err.Error())
case <-time.After(defaultChannelTimeout): // If we timeout case <-time.After(defaultChannelTimeout): // If we timeout
t.Errorf("Failed to receive response, to sent IQ, from mock server") t.Fatal("Failed to receive response, to sent IQ, from mock server")
} }
select { select {
case <-done: case <-done:
mock.Stop() mock.Stop()
case <-time.After(defaultChannelTimeout): case <-time.After(defaultChannelTimeout):
t.Errorf("The mock server failed to finish its job !") t.Fatal("The mock server failed to finish its job !")
} }
} }

@ -120,6 +120,7 @@ func respondToIQ(t *testing.T, c net.Conn) {
recvBuf := make([]byte, 1024) recvBuf := make([]byte, 1024)
var iqR stanza.IQ var iqR stanza.IQ
_, err := c.Read(recvBuf[:]) // recv data _, err := c.Read(recvBuf[:]) // recv data
if err != nil { if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() { if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
t.Errorf("read timeout: %s", err) t.Errorf("read timeout: %s", err)
@ -155,11 +156,22 @@ func respondToIQ(t *testing.T, c net.Conn) {
// When a presence stanza is automatically sent (right now it's the case in the client), we may want to discard it // When a presence stanza is automatically sent (right now it's the case in the client), we may want to discard it
// and test further stanzas. // and test further stanzas.
func discardPresence(t *testing.T, c net.Conn) { func discardPresence(t *testing.T, c net.Conn) {
decoder := xml.NewDecoder(c)
c.SetDeadline(time.Now().Add(defaultTimeout)) c.SetDeadline(time.Now().Add(defaultTimeout))
defer c.SetDeadline(time.Time{}) defer c.SetDeadline(time.Time{})
var presenceStz stanza.Presence var presenceStz stanza.Presence
err := decoder.Decode(&presenceStz)
recvBuf := make([]byte, len(InitialPresence))
_, err := c.Read(recvBuf[:]) // recv data
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
t.Errorf("read timeout: %s", err)
} else {
t.Errorf("read error: %s", err)
}
}
xml.Unmarshal(recvBuf, &presenceStz)
if err != nil { if err != nil {
t.Errorf("Expected presence but this happened : %s", err.Error()) t.Errorf("Expected presence but this happened : %s", err.Error())
} }

Loading…
Cancel
Save