You need to sign in or sign up before continuing.
client_test.go 1.6 KB
Newer Older
1 2 3 4 5 6 7
package master_test

import (
	"fmt"
	"net"
	"net/http"
	"net/rpc"
8
	"os"
9 10 11 12 13 14
	"strconv"
	"strings"
	"testing"
	"time"

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

18 19 20 21 22 23 24 25 26 27 28 29 30
type myStore struct {
	buf []byte
}

func (m *myStore) Save(b []byte) error {
	m.buf = b
	return nil
}

func (m *myStore) Load() ([]byte, error) {
	return m.buf, nil
}

H
Helin Wang 已提交
31 32 33 34 35
func TestNextRecord(t *testing.T) {
	const (
		path  = "/tmp/master_client_TestFull"
		total = 50
	)
H
Helin Wang 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48
	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) {
49 50 51 52 53
		s, err := master.NewService(&myStore{}, 10, time.Second, 1)
		if err != nil {
			panic(err)
		}

54
		server := rpc.NewServer()
55
		err = server.Register(s)
56 57 58 59 60 61 62 63 64 65 66 67
		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 已提交
68
	f, err := os.Create(path)
69 70 71 72
	if err != nil {
		panic(err)
	}

H
Helin Wang 已提交
73 74 75
	w := recordio.NewWriter(f, -1, -1)
	for i := 0; i < total; i++ {
		w.Write([]byte{byte(i)})
76
	}
H
Helin Wang 已提交
77
	w.Close()
78 79
	f.Close()

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

H
Helin Wang 已提交
83 84 85 86 87 88
	for pass := 0; pass < 50; pass++ {
		received := make(map[byte]bool)
		for i := 0; i < total; i++ {
			r := c.NextRecord()
			if len(r) != 1 {
				t.Fatal("Length should be 1.", r)
89
			}
H
Helin Wang 已提交
90 91
			if received[r[0]] {
				t.Fatal("Received duplicate.", received, r)
92
			}
H
Helin Wang 已提交
93
			received[r[0]] = true
94 95 96
		}
	}
}