Ejemplo n.º 1
0
func main() {
	var wg sync.WaitGroup
	// Set the autoinc id generator
	// You can write your own id generator
	// by implementing IdGenerator interface.
	// client.IdGen = client.NewAutoIncId()

	c, err := client.New("127.0.0.1:4730")
	if err != nil {
		log.Fatalln(err)
	}
	defer c.Close()
	c.ErrHandler = func(e error) {
		log.Println(e)
	}
	echo := []byte("Hello\x00 world")
	wg.Add(1)
	echomsg, err := c.Echo(echo, time.Second)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(echomsg))
	wg.Done()
	jobHandler := func(job *client.Job) {
		log.Printf("%s", job.Data)
		wg.Done()
	}
	handle := c.Do("ToUpper", echo, client.JOB_NORMAL, jobHandler)
	wg.Add(1)
	status, err := c.Status(handle, time.Second)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%t", status)

	wg.Wait()
}
Ejemplo n.º 2
0
func TestJobs(t *testing.T) {
	w := worker.New(worker.Unlimited)
	if err := w.AddServer(GEARMAND); err != nil {
		t.Error(err)
		return
	}
	defer w.Close()
	if err := w.AddFunc("ToUpper", ToUpper, 0); err != nil {
		t.Error(err)
		return
	}
	if err := w.AddFunc("Sleep", Sleep, 0); err != nil {
		t.Error(err)
		return
	}

	w.ErrHandler = func(e error) {
		t.Error(e)
	}
	go w.Work()

	c, err := client.New(GEARMAND)
	if err != nil {
		t.Error(err)
		return
	}
	defer c.Close()

	c.ErrHandler = func(e error) {
		//        t.Error(e)
		t.Log(e)
	}

	{
		var w sync.WaitGroup
		jobHandler := func(job *client.Job) {
			upper := strings.ToUpper(STR)
			if string(job.Data) != upper {
				t.Error("%s expected, got %s", []byte(upper), job.Data)
			}
			w.Done()
		}

		w.Add(1)
		handle := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
		w.Wait()
		status, err := c.Status(handle, time.Second)
		if err != nil {
			t.Error(err)
			return
		}
		if status.Known {
			t.Errorf("%s shouldn't be known", status.Handle)
			return
		}

		if status.Running {
			t.Errorf("%s shouldn't be running", status.Handle)
		}
	}
	{
		handle := c.DoBg("Sleep", nil, client.JOB_NORMAL)
		time.Sleep(time.Second)
		status, err := c.Status(handle, time.Second)
		if err != nil {
			t.Error(err)
			return
		}

		if !status.Known {
			t.Errorf("%s should be known", status.Handle)
			return
		}

		if !status.Running {
			t.Errorf("%s should be running", status.Handle)
		}
	}
	{
		status, err := c.Status("not exists handle", time.Second)
		if err != nil {
			t.Error(err)
			return
		}

		if status.Known {
			t.Errorf("%s shouldn't be known", status.Handle)
			return
		}

		if status.Running {
			t.Errorf("%s shouldn't be running", status.Handle)
		}
	}
}