client: test indication and cleanup

This commit is contained in:
Aleksandr Razumov
2017-12-15 20:13:29 +03:00
parent 844549d50d
commit c563d0b3ba

View File

@@ -1,8 +1,8 @@
package stun package stun
import ( import (
"errors"
"io" "io"
"sync"
"testing" "testing"
"time" "time"
) )
@@ -78,23 +78,22 @@ func BenchmarkClient_Do(b *testing.B) {
type testConnection struct { type testConnection struct {
write func([]byte) (int, error) write func([]byte) (int, error)
b []byte b []byte
l sync.Mutex
stopped bool stopped bool
} }
func (t *testConnection) Write(b []byte) (int, error) { func (t *testConnection) Write(b []byte) (int, error) {
t.l.Unlock()
return t.write(b) return t.write(b)
} }
func (t *testConnection) Close() error { func (t *testConnection) Close() error {
if t.stopped {
return errors.New("already stopped")
}
t.stopped = true t.stopped = true
t.l.Unlock()
return nil return nil
} }
func (t *testConnection) Read(b []byte) (int, error) { func (t *testConnection) Read(b []byte) (int, error) {
t.l.Lock()
if t.stopped { if t.stopped {
return 0, io.EOF return 0, io.EOF
} }
@@ -124,7 +123,6 @@ func TestClient_Do(t *testing.T) {
return len(bytes), nil return len(bytes), nil
}, },
} }
conn.l.Lock()
c := NewClient(ClientOptions{ c := NewClient(ClientOptions{
Connection: conn, Connection: conn,
}) })
@@ -136,9 +134,9 @@ func TestClient_Do(t *testing.T) {
t.Error("second close should fail") t.Error("second close should fail")
} }
}() }()
m := new(Message) m := MustBuild(
m.TransactionID = response.TransactionID NewTransactionIDSetter(response.TransactionID),
m.Encode() )
d := time.Now().Add(time.Second) d := time.Now().Add(time.Second)
if err := c.Do(m, d, func(event Event) { if err := c.Do(m, d, func(event Event) {
if event.Error != nil { if event.Error != nil {
@@ -147,6 +145,10 @@ func TestClient_Do(t *testing.T) {
}); err != nil { }); err != nil {
t.Error(err) t.Error(err)
} }
m = MustBuild(TransactionID)
if err := c.Do(m, d, nil); err != nil {
t.Error(err)
}
} }
func TestCloseErr_Error(t *testing.T) { func TestCloseErr_Error(t *testing.T) {