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) {
@ -41,7 +41,7 @@ func main() {
w.ErrorHandler = func(e error) { w.ErrorHandler = func(e error) {
log.Println(e) log.Println(e)
if opErr, ok := e.(*net.OpError); ok { if opErr, ok := e.(*net.OpError); ok {
if ! opErr.Temporary() { if !opErr.Temporary() {
proc, err := os.FindProcess(os.Getpid()) proc, err := os.FindProcess(os.Getpid())
if err != nil { if err != nil {
log.Println(err) log.Println(err)

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.
@ -59,14 +59,14 @@ func (a *agent) work() {
if opErr, ok := err.(*net.OpError); ok { if opErr, ok := err.(*net.OpError); ok {
if opErr.Temporary() { if opErr.Temporary() {
continue continue
}else{ } else {
a.disconnect_error(err) a.disconnect_error(err)
// else - we're probably dc'ing due to a Close() // else - we're probably dc'ing due to a Close()
break break
} }
} else if( err == io.EOF ){ } else if err == io.EOF {
a.disconnect_error(err) a.disconnect_error(err)
break break
} }
@ -104,11 +104,11 @@ 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,
} }
a.worker.err(err) a.worker.err(err)
} }
@ -129,7 +129,7 @@ func (a *agent) Grab() {
a.grab() a.grab()
} }
func (a *agent) grab(){ func (a *agent) grab() {
outpack := getOutPack() outpack := getOutPack()
outpack.dataType = dtGrabJobUniq outpack.dataType = dtGrabJobUniq
a.write(outpack) a.write(outpack)
@ -143,16 +143,16 @@ 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),
bufio.NewWriter(a.conn)) bufio.NewWriter(a.conn))
a.grab() a.grab()
a.worker.reRegisterFuncsForAgent(a) a.worker.reRegisterFuncsForAgent(a)

View File

@ -50,8 +50,8 @@ func run_gearman() {
panic(`Unable to start gearman aborting test`) panic(`Unable to start gearman aborting test`)
} }
gearman_ready <- true gearman_ready <- true
<- kill_gearman <-kill_gearman
} }
func check_gearman_present() bool { func check_gearman_present() bool {
@ -134,14 +134,14 @@ func TestBasicDisconnect(t *testing.T) {
send_client_request() send_client_request()
select { select {
case <- done: case <-done:
t.Error("Client request handled (somehow), did we magically reconnect?") t.Error("Client request handled (somehow), did we magically reconnect?")
case <-timeout: case <-timeout:
t.Error("Test timed out waiting for the error handler") t.Error("Test timed out waiting for the error handler")
case <-c_error: case <-c_error:
// error was handled! // error was handled!
if ! was_dc_err { if !was_dc_err {
t.Error( "Disconnect didn't manifest as a net.OpError?") t.Error("Disconnect didn't manifest as a net.OpError?")
} }
} }
worker.Close() worker.Close()
@ -175,24 +175,23 @@ 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-- {
rcerr := wdc.Reconnect() rcerr := wdc.Reconnect()
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())
} }
} }
go func() { go func() {
@ -219,9 +218,9 @@ func TestDcRc(t *testing.T) {
} }
send_client_request() send_client_request()
select { select {
case <- done: case <-done:
case <-timeout: case <-timeout:
t.Error("Test timed out") t.Error("Test timed out")
} }

View File

@ -139,7 +139,7 @@ func TestWorkerClose(t *testing.T) {
worker.Close() worker.Close()
} }
func TestWorkWithoutReady(t * testing.T){ func TestWorkWithoutReady(t *testing.T) {
other_worker := New(Unlimited) other_worker := New(Unlimited)
if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil { if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
@ -148,15 +148,15 @@ func TestWorkWithoutReady(t * testing.T){
if err := other_worker.AddFunc("gearman-go-workertest", foobar, 0); err != nil { if err := other_worker.AddFunc("gearman-go-workertest", foobar, 0); err != nil {
t.Error(err) t.Error(err)
} }
timeout := make(chan bool, 1)
done := make( chan bool, 1)
other_worker.JobHandler = func( j Job ) error { timeout := make(chan bool, 1)
if( ! other_worker.ready ){ done := make(chan bool, 1)
t.Error("Worker not ready as expected");
other_worker.JobHandler = func(j Job) error {
if !other_worker.ready {
t.Error("Worker not ready as expected")
} }
done <-true done <- true
return nil return nil
} }
go func() { go func() {
@ -164,15 +164,15 @@ func TestWorkWithoutReady(t * testing.T){
timeout <- true timeout <- true
}() }()
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
@ -183,24 +183,24 @@ func TestWorkWithoutReady(t * testing.T){
tries-- tries--
} }
}() }()
// determine if we've finished or timed out: // determine if we've finished or timed out:
select{ select {
case <- timeout: case <-timeout:
t.Error("Test timed out waiting for the worker") t.Error("Test timed out waiting for the worker")
case <- done: case <-done:
} }
} }
func TestWorkWithoutReadyWithPanic(t * testing.T){ func TestWorkWithoutReadyWithPanic(t *testing.T) {
other_worker := New(Unlimited) other_worker := New(Unlimited)
timeout := make(chan bool, 1) timeout := make(chan bool, 1)
done := make( chan bool, 1) done := make(chan bool, 1)
// Going to work with no worker setup. // Going to work with no worker setup.
// when Work (hopefully) calls Ready it will get an error which should cause it to panic() // when Work (hopefully) calls Ready it will get an error which should cause it to panic()
go func(){ go func() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
done <- true done <- true
@ -209,17 +209,17 @@ 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)
timeout <- true timeout <- true
}() }()
select{ select {
case <- timeout: case <-timeout:
t.Error("Test timed out waiting for the worker") t.Error("Test timed out waiting for the worker")
case <- done: case <-done:
} }
} }