mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2024-11-21 01:52:01 -08:00
Using precisely sized buffers for tcp tests
This commit is contained in:
parent
1f5591f33a
commit
fd48f52f3d
@ -20,6 +20,7 @@ type ConnState = uint8
|
||||
// This is a the list of events happening on the connection that the
|
||||
// client can be notified about.
|
||||
const (
|
||||
InitialPresence = "<presence/>"
|
||||
StateDisconnected ConnState = iota
|
||||
StateConnected
|
||||
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")
|
||||
// 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 ?
|
||||
err = c.sendWithWriter(c.transport, []byte("<presence/>"))
|
||||
err = c.sendWithWriter(c.transport, []byte(InitialPresence))
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -184,15 +184,15 @@ func TestClient_SendIQ(t *testing.T) {
|
||||
select {
|
||||
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
|
||||
t.Errorf(err.Error())
|
||||
t.Fatal(err.Error())
|
||||
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 {
|
||||
case <-done:
|
||||
mock.Stop()
|
||||
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)
|
||||
var iqR stanza.IQ
|
||||
_, err := c.Read(recvBuf[:]) // recv data
|
||||
|
||||
if err != nil {
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||
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
|
||||
// and test further stanzas.
|
||||
func discardPresence(t *testing.T, c net.Conn) {
|
||||
decoder := xml.NewDecoder(c)
|
||||
c.SetDeadline(time.Now().Add(defaultTimeout))
|
||||
defer c.SetDeadline(time.Time{})
|
||||
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 {
|
||||
t.Errorf("Expected presence but this happened : %s", err.Error())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user