This commit is contained in:
Xing Xing 2015-01-06 11:45:18 +08:00
parent a003eac543
commit ad9b3cb988
5 changed files with 57 additions and 58 deletions

View File

@ -3,8 +3,8 @@ package main
import ( import (
"github.com/mikespook/gearman-go/client" "github.com/mikespook/gearman-go/client"
"log" "log"
"sync"
"os" "os"
"sync"
) )
func main() { func main() {

View File

@ -4,10 +4,10 @@ import (
"github.com/mikespook/gearman-go/worker" "github.com/mikespook/gearman-go/worker"
"github.com/mikespook/golib/signal" "github.com/mikespook/golib/signal"
"log" "log"
"net"
"os" "os"
"strings" "strings"
"time" "time"
"net"
) )
func ToUpper(job worker.Job) ([]byte, error) { func ToUpper(job worker.Job) ([]byte, error) {

View File

@ -4,9 +4,9 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"io"
"net" "net"
"sync" "sync"
"io"
) )
// The agent of job server. // The agent of job server.
@ -66,7 +66,7 @@ func (a *agent) work() {
break break
} }
} else if( err == io.EOF ){ } else if err == io.EOF {
a.disconnect_error(err) a.disconnect_error(err)
break break
} }
@ -105,7 +105,7 @@ func (a *agent) work() {
} }
func (a *agent) disconnect_error(err error) { func (a *agent) disconnect_error(err error) {
if( a.conn != nil ){ if a.conn != nil {
err = &WorkerDisconnectError{ err = &WorkerDisconnectError{
err: err, err: err,
agent: a, agent: a,
@ -143,12 +143,12 @@ func (a *agent) PreSleep() {
a.write(outpack) a.write(outpack)
} }
func (a *agent) reconnect() (error){ func (a *agent) reconnect() error {
a.Lock() a.Lock()
defer a.Unlock() defer a.Unlock()
conn, err := net.Dial(a.net, a.addr) conn, err := net.Dial(a.net, a.addr)
if err != nil { if err != nil {
return err; return err
} }
a.conn = conn a.conn = conn
a.rw = bufio.NewReadWriter(bufio.NewReader(a.conn), a.rw = bufio.NewReadWriter(bufio.NewReader(a.conn),

View File

@ -176,7 +176,7 @@ func TestDcRc(t *testing.T) {
worker.ErrorHandler = func(e error) { worker.ErrorHandler = func(e error) {
wdc, wdcok := e.(*WorkerDisconnectError) wdc, wdcok := e.(*WorkerDisconnectError)
if( wdcok){ if wdcok {
log.Println("Reconnecting!") log.Println("Reconnecting!")
reconnected := false reconnected := false
for tries := 20; !reconnected && tries > 0; tries-- { for tries := 20; !reconnected && tries > 0; tries-- {
@ -184,11 +184,10 @@ func TestDcRc(t *testing.T) {
if rcerr != nil { if rcerr != nil {
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)
} else { } else {
reconnected = true; reconnected = true
} }
} }
} else { } else {
panic("Some other kind of error " + e.Error()) panic("Some other kind of error " + e.Error())
} }

View File

@ -153,8 +153,8 @@ func TestWorkWithoutReady(t * testing.T){
done := make(chan bool, 1) done := make(chan bool, 1)
other_worker.JobHandler = func(j Job) error { other_worker.JobHandler = func(j Job) error {
if( ! other_worker.ready ){ if !other_worker.ready {
t.Error("Worker not ready as expected"); t.Error("Worker not ready as expected")
} }
done <- true done <- true
return nil return nil
@ -165,14 +165,14 @@ func TestWorkWithoutReady(t * testing.T){
}() }()
go func() { go func() {
other_worker.Work(); other_worker.Work()
}() }()
// With the all-in-one Work() we don't know if the // With the all-in-one Work() we don't know if the
// worker is ready at this stage so we may have to wait a sec: // worker is ready at this stage so we may have to wait a sec:
go func() { go func() {
tries := 5 tries := 5
for( tries > 0 ){ for tries > 0 {
if other_worker.ready { if other_worker.ready {
other_worker.Echo([]byte("Hello")) other_worker.Echo([]byte("Hello"))
break break
@ -209,7 +209,7 @@ func TestWorkWithoutReadyWithPanic(t * testing.T){
t.Error("Work should raise a panic.") t.Error("Work should raise a panic.")
done <- true done <- true
}() }()
other_worker.Work(); other_worker.Work()
}() }()
go func() { go func() {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)