client_internal_test.go 3.4 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.

H
Helin Wang 已提交
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
package master

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

	log "github.com/sirupsen/logrus"

	"github.com/PaddlePaddle/Paddle/go/connection"
	"github.com/PaddlePaddle/recordio"
)

const (
	totalTask    = 20
	chunkPerTask = 10
)

func init() {
	log.SetLevel(log.ErrorLevel)
}

func TestGetFinishTask(t *testing.T) {
	const path = "/tmp/master_client_test_0"

	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) {
57 58 59
		s, sErr := NewService(&InMemStore{}, chunkPerTask, time.Second, 1)
		if sErr != nil {
			panic(sErr)
60 61
		}

H
Helin Wang 已提交
62
		server := rpc.NewServer()
63 64 65
		sErr = server.Register(s)
		if sErr != nil {
			panic(sErr)
H
Helin Wang 已提交
66 67 68 69
		}

		mux := http.NewServeMux()
		mux.Handle(rpc.DefaultRPCPath, server)
70 71 72
		sErr = http.Serve(l, mux)
		if sErr != nil {
			panic(sErr)
H
Helin Wang 已提交
73 74 75 76 77 78 79 80 81 82
		}
	}(l)

	f, err := os.Create(path)
	if err != nil {
		panic(err)
	}

	for i := 0; i < totalTask*chunkPerTask; i++ {
		w := recordio.NewWriter(f, -1, -1)
H
Helin Wang 已提交
83 84 85 86 87
		_, err = w.Write(nil)
		if err != nil {
			panic(err)
		}

H
Helin Wang 已提交
88
		// call Close to force RecordIO writing a chunk.
H
Helin Wang 已提交
89 90 91 92 93 94 95 96
		err = w.Close()
		if err != nil {
			panic(err)
		}
	}
	err = f.Close()
	if err != nil {
		panic(err)
H
Helin Wang 已提交
97 98
	}

H
Helin Wang 已提交
99
	// Manually intialize client to avoid calling c.getRecords()
H
Helin Wang 已提交
100 101
	c := &Client{}
	c.conn = connection.New()
102 103 104 105
	addr := fmt.Sprintf(":%d", p)
	ch := make(chan string, 1)
	ch <- addr
	go c.monitorMaster(ch)
106

H
Helin Wang 已提交
107 108 109 110 111
	err = c.SetDataset([]string{path})
	if err != nil {
		panic(err)
	}

H
Helin Wang 已提交
112 113 114
	checkOnePass := func(i int) {
		var tasks []Task
		for idx := 0; idx < totalTask; idx++ {
115 116 117
			task, cErr := c.getTask(i)
			if cErr != nil && cErr.Error() != ErrNoMoreAvailable.Error() && cErr.Error() != ErrPassAfter.Error() {
				t.Fatalf("error: %v, pass: %d\n", cErr, i)
H
Helin Wang 已提交
118 119 120 121
			}
			tasks = append(tasks, task)
		}

122 123 124
		// getting task before task finishes should return error
		_, cErr := c.getTask(i)
		if cErr == nil {
H
Helin Wang 已提交
125
			t.Fatalf("Should get error, pass: %d\n", i)
H
Helin Wang 已提交
126 127
		}

128 129 130
		cErr = c.taskFinished(tasks[0].Meta.ID)
		if cErr != nil {
			t.Fatalf("Error: %v, pass: %d\n", cErr, i)
H
Helin Wang 已提交
131
		}
132 133 134 135 136
		// call taskFailed once won't put the task to failed queue, just ensure
		// the call
		cErr = c.taskFailed(tasks[0].Meta)
		if cErr != nil {
			t.Fatalf("Error: %v, pass: %d\n", cErr, i)
G
gongweibao 已提交
137 138
		}

H
Helin Wang 已提交
139
		tasks = tasks[1:]
140 141 142
		_, cErr = c.getTask(i)
		if cErr != nil && cErr.Error() != ErrNoMoreAvailable.Error() && cErr.Error() != ErrPassAfter.Error() {
			t.Fatalf("Should be ErrNoMoreAvailable or ErrPassAfter: %s", cErr)
H
Helin Wang 已提交
143 144 145
		}

		for _, task := range tasks {
146 147 148
			cErr = c.taskFinished(task.Meta.ID)
			if cErr != nil {
				t.Fatal(cErr)
H
Helin Wang 已提交
149 150 151 152 153
			}
		}
	}

	for i := 0; i < 10; i++ {
154 155
		// init pass data
		c.StartGetRecords(i)
H
Helin Wang 已提交
156 157 158
		checkOnePass(i)
	}
}