client_test.go 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package master_test

import (
	"fmt"
	"net"
	"net/http"
	"net/rpc"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/PaddlePaddle/Paddle/go/master"
)

const (
	totalTask    = 20
	chunkPerTask = 10
)

var port int

func init() {
	l, err := net.Listen("tcp", ":0")
	if err != nil {
		panic(err)
	}

	ss := strings.Split(l.Addr().String(), ":")
	p, err := strconv.Atoi(ss[len(ss)-1])
	if err != nil {
		panic(err)
	}
	port = p

	go func(l net.Listener) {
		chunks := make([]master.Chunk, totalTask)
		s := master.NewService(chunks, chunkPerTask, time.Second, 1)
		server := rpc.NewServer()
		err := server.Register(s)
		if err != nil {
			panic(err)
		}

		mux := http.NewServeMux()
		mux.Handle(rpc.DefaultRPCPath, server)
		err = http.Serve(l, mux)
		if err != nil {
			panic(err)
		}
	}(l)
}

type addresser string

func (a addresser) Address() string {
	return string(a)
}

func TestClientFull(t *testing.T) {
	c := master.NewClient(addresser(fmt.Sprintf(":%d", port)))

	for i := 0; i < 5*totalTask/chunkPerTask; i++ {
		task, err := c.GetTask()
		if err != nil {
			panic(err)
		}

		if len(task.Chunks) != chunkPerTask {
			t.Fatal("wrong number of chunk per task", len(task.Chunks))
		}

		err = c.TaskFinished(task.ID)
		if err != nil {
			panic(err)
		}
	}
}