client_test.go 2.3 KB
Newer Older
D
dongzhihong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17 18 19 20 21
package master_test

import (
	"fmt"
	"net"
	"net/http"
	"net/rpc"
22
	"os"
23 24 25 26 27 28
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/PaddlePaddle/Paddle/go/master"
29
	"github.com/PaddlePaddle/recordio"
30 31
)

H
Helin Wang 已提交
32 33 34 35 36
func TestNextRecord(t *testing.T) {
	const (
		path  = "/tmp/master_client_TestFull"
		total = 50
	)
37 38 39 40 41 42 43 44 45 46 47
	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 已提交
48
		s, err := master.NewService(&master.InMemStore{}, 10, time.Second, 1)
49 50 51 52
		if err != nil {
			panic(err)
		}

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

H
Helin Wang 已提交
72 73
	w := recordio.NewWriter(f, -1, -1)
	for i := 0; i < total; i++ {
H
Helin Wang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87
		_, err = w.Write([]byte{byte(i)})
		if err != nil {
			panic(err)
		}
	}

	err = w.Close()
	if err != nil {
		panic(err)
	}

	err = f.Close()
	if err != nil {
		panic(err)
88
	}
H
Helin Wang 已提交
89

90 91 92 93 94
	c, err := master.NewClient(master.WithAddr(fmt.Sprintf(":%d", p)), master.WithBuffer(10))
	if err != nil {
		panic(err)
	}

H
Helin Wang 已提交
95 96 97 98 99
	err = c.SetDataset([]string{path})
	if err != nil {
		panic(err)
	}

H
Helin Wang 已提交
100 101
	for pass := 0; pass < 50; pass++ {
		received := make(map[byte]bool)
G
gongweibao 已提交
102
		for i := 0; i < total; i++ {
G
gongweibao 已提交
103 104 105 106 107
			r, err := c.NextRecord()
			if err != nil {
				t.Fatal(pass, i, "Read error:", err)
			}

H
Helin Wang 已提交
108
			if len(r) != 1 {
G
gongweibao 已提交
109
				t.Fatal(pass, i, "Length should be 1.", r)
110
			}
G
gongweibao 已提交
111

H
Helin Wang 已提交
112
			if received[r[0]] {
G
gongweibao 已提交
113
				t.Fatal(pass, i, "Received duplicate.", received, r)
114
			}
H
Helin Wang 已提交
115
			received[r[0]] = true
116 117 118
		}
	}
}