fixed nil pointer

This commit is contained in:
Xing Xing 2013-09-22 22:02:05 +08:00
parent e5179e3b5b
commit e3f789bc86
3 changed files with 11 additions and 8 deletions

View File

@ -14,8 +14,7 @@ func TestClientAddServer(t *testing.T) {
t.Log("Add local server 127.0.0.1:4730") t.Log("Add local server 127.0.0.1:4730")
var err error var err error
if client, err = New("tcp4", "127.0.0.1:4730"); err != nil { if client, err = New("tcp4", "127.0.0.1:4730"); err != nil {
t.Error(err) t.Fatal(err)
return
} }
client.ErrorHandler = func(e error) { client.ErrorHandler = func(e error) {
t.Log(e) t.Log(e)

View File

@ -80,9 +80,11 @@ func (pool *Pool) Add(net, addr string, rate int) (err error) {
} else { } else {
var client *Client var client *Client
client, err = New(net, addr) client, err = New(net, addr)
if err == nil {
item = &poolClient{Client: client, Rate: rate} item = &poolClient{Client: client, Rate: rate}
pool.clients[addr] = item pool.clients[addr] = item
} }
}
return return
} }

View File

@ -10,14 +10,16 @@ var (
func TestPoolAdd(t *testing.T) { func TestPoolAdd(t *testing.T) {
t.Log("Add servers") t.Log("Add servers")
c := 2
if err := pool.Add("tcp4", "127.0.0.1:4730", 1); err != nil { if err := pool.Add("tcp4", "127.0.0.1:4730", 1); err != nil {
t.Error(err) t.Fatal(err)
} }
if err := pool.Add("tcp4", "127.0.1.1:4730", 1); err != nil { if err := pool.Add("tcp4", "127.0.1.1:4730", 1); err != nil {
t.Error(err) t.Log(err)
c -= 1
} }
if len(pool.clients) != 2 { if len(pool.clients) != c {
t.Errorf("2 servers expected, %d got.", len(pool.clients)) t.Errorf("%d servers expected, %d got.", c, len(pool.clients))
} }
} }