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")
var err error
if client, err = New("tcp4", "127.0.0.1:4730"); err != nil {
t.Error(err)
return
t.Fatal(err)
}
client.ErrorHandler = func(e error) {
t.Log(e)

View File

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

View File

@ -10,14 +10,16 @@ var (
func TestPoolAdd(t *testing.T) {
t.Log("Add servers")
c := 2
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 {
t.Error(err)
t.Log(err)
c -= 1
}
if len(pool.clients) != 2 {
t.Errorf("2 servers expected, %d got.", len(pool.clients))
if len(pool.clients) != c {
t.Errorf("%d servers expected, %d got.", c, len(pool.clients))
}
}