client_test.go 1.5 KB
Newer Older
1 2 3 4
package master_test

import (
	"fmt"
G
gongweibao 已提交
5
	"io"
6 7 8
	"net"
	"net/http"
	"net/rpc"
9
	"os"
10 11 12 13 14 15
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/PaddlePaddle/Paddle/go/master"
16
	"github.com/PaddlePaddle/recordio"
17 18
)

H
Helin Wang 已提交
19 20 21 22 23
func TestNextRecord(t *testing.T) {
	const (
		path  = "/tmp/master_client_TestFull"
		total = 50
	)
H
Helin Wang 已提交
24

25 26 27 28 29 30 31 32 33 34 35 36
	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)
	}

	go func(l net.Listener) {
H
Helin Wang 已提交
37
		s, err := master.NewService(&master.InMemStore{}, 10, time.Second, 1)
38 39 40 41
		if err != nil {
			panic(err)
		}

42
		server := rpc.NewServer()
43
		err = server.Register(s)
44 45 46 47 48 49 50 51 52 53 54 55
		if err != nil {
			panic(err)
		}

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

H
Helin Wang 已提交
56
	f, err := os.Create(path)
57 58 59 60
	if err != nil {
		panic(err)
	}

H
Helin Wang 已提交
61 62 63
	w := recordio.NewWriter(f, -1, -1)
	for i := 0; i < total; i++ {
		w.Write([]byte{byte(i)})
64
	}
H
Helin Wang 已提交
65
	w.Close()
66 67
	f.Close()

68
	c := master.NewClient(master.TestAddresser(fmt.Sprintf(":%d", p)), 10)
H
Helin Wang 已提交
69
	c.SetDataset([]string{path})
70

H
Helin Wang 已提交
71 72
	for pass := 0; pass < 50; pass++ {
		received := make(map[byte]bool)
G
gongweibao 已提交
73 74 75 76 77 78 79 80 81 82
		for i := 0; i <= total; i++ {
			r, err := c.NextRecord()
			if err == io.EOF {
				break
			}

			if err != nil {
				t.Fatal(pass, i, "Read error:", err)
			}

H
Helin Wang 已提交
83
			if len(r) != 1 {
G
gongweibao 已提交
84
				t.Fatal(pass, i, "Length should be 1.", r)
85
			}
G
gongweibao 已提交
86

H
Helin Wang 已提交
87
			if received[r[0]] {
G
gongweibao 已提交
88
				t.Fatal(pass, i, "Received duplicate.", received, r)
89
			}
H
Helin Wang 已提交
90
			received[r[0]] = true
91 92 93
		}
	}
}