Skip to content

Commit 7f1ca53

Browse files
authored
Fix client tests (#8)
* Fix client tests * Reverted to use of NewConnected * Fixed arguments on some New Client calls * Optimize conn checking in readLoop * Add error handling for gearmanClient.Do() call
1 parent afa6a31 commit 7f1ca53

File tree

4 files changed

+22
-33
lines changed

4 files changed

+22
-33
lines changed

Diff for: client/client.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,12 @@ func (client *Client) writeLoop() {
204204
ibuf := make([]byte, 4)
205205
length := uint32(0)
206206
var i int
207+
chans := client.loadChans()
207208

208209
// Pipeline requests; but only write them one at a time. To allow multiple
209210
// goroutines to all write as quickly as possible, uses a channel and the
210211
// writeLoop lives in a separate goroutine.
211-
for req := range client.loadChans().outbound {
212+
for req := range chans.outbound {
212213

213214
conn := client.loadConn()
214215
if conn == nil {
@@ -348,8 +349,12 @@ func (client *Client) readLoop() {
348349
var err error
349350
var resp *Response
350351
startConn := client.loadConn()
352+
newConn := startConn
353+
if startConn == nil {
354+
return
355+
}
351356

352-
for startConn == client.loadConn() {
357+
for ; newConn == startConn; newConn = client.loadConn() {
353358

354359
if _, exit := client.readReconnect(startConn, header); exit {
355360
return

Diff for: client/client_test.go

+13-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"errors"
88
"fmt"
9-
"github.com/appscode/go/log"
109
rt "github.com/quantcast/g2/pkg/runtime"
1110
"io"
1211
"net"
@@ -188,17 +187,9 @@ func drain(observed io.Reader) {
188187

189188
func TestClose(test *testing.T) {
190189

191-
handlerConnOpen := func() (conn net.Conn, err error) {
192-
log.Infoln("Creating net.Pipe connection...")
193-
conn, _ = net.Pipe()
194-
return
195-
}
196-
197-
handlerConnClose := func(conn net.Conn) (err error) {
198-
return conn.Close()
199-
}
190+
client, _ := net.Pipe()
200191

201-
gearmanc := NewClient(handlerConnClose, handlerConnOpen)
192+
gearmanc := NewConnected(client)
202193

203194
if gearmanc.Close() != nil {
204195
test.Fatalf("expected no error in closing connected client")
@@ -218,17 +209,9 @@ func TestSnapshot(test *testing.T) {
218209
test.Fatalf("error loading snapshot: %s\n", err)
219210
}
220211

221-
handlerConnOpen := func() (conn net.Conn, err error) {
222-
return client, nil // return pre-created pipe client
223-
}
224-
225-
handlerConnClose := func(conn net.Conn) (err error) {
226-
return conn.Close()
227-
}
228-
229212
// This has to be done in another go-routine since all of the reads/writes
230213
// are synchronous
231-
gearmanClient := NewClient(handlerConnClose, handlerConnOpen)
214+
gearmanClient := NewConnected(client)
232215

233216
if err = snapshot.replay(server, "server", "client"); err != nil {
234217
test.Fatalf("error loading snapshot: %s", err)
@@ -240,14 +223,17 @@ func TestSnapshot(test *testing.T) {
240223

241224
errors := make(chan error)
242225

243-
go gearmanClient.Do("test", payload, rt.JobNormal, func(r *Response) {
244-
if !reflect.DeepEqual(payload, r.Data) {
245-
errors <- fmt.Errorf("\nexpected:\n%s\nobserved:\n%s\n",
246-
hex.Dump(payload), hex.Dump(r.Data))
226+
go func() {
227+
if _, err := gearmanClient.Do("test", payload, rt.JobNormal, func(r *Response) {
228+
if !reflect.DeepEqual(payload, r.Data) {
229+
errors <- fmt.Errorf("\nexpected:\n%s\nobserved:\n%s\n",
230+
hex.Dump(payload), hex.Dump(r.Data))
231+
}
232+
close(errors)
233+
}); err != nil {
234+
errors <- fmt.Errorf("\nError:%v", err.Error())
247235
}
248-
249-
close(errors)
250-
})
236+
}()
251237

252238
for err := range errors {
253239
test.Fatalf("error: %s", err)

Diff for: example/client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515
// by implementing IdGenerator interface.
1616
// client.IdGen = client.NewAutoIncId()
1717

18-
c, err := client.NewNetClient(rt.Network, "127.0.0.1:4730")
18+
c, err := client.New(rt.Network, "127.0.0.1:4730", nil)
1919
if err != nil {
2020
log.Fatalln(err)
2121
}

Diff for: example/client/persistent_client.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ func main() {
2727
// by implementing IdGenerator interface.
2828
// client.IdGen = client.NewAutoIncId()
2929

30-
logs.InitLogs()
31-
logs.FlushLogs()
32-
c, err := client.NewNetClient(rt.Network, "127.0.0.1:4730")
30+
c, err := client.New(rt.Network, "127.0.0.1:4730", logHandler)
3331
if err != nil {
3432
log.Fatalln(err)
3533
}

0 commit comments

Comments
 (0)