-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtraining_test.go
More file actions
141 lines (115 loc) · 2.83 KB
/
Copy pathtraining_test.go
File metadata and controls
141 lines (115 loc) · 2.83 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package lukai
import (
"io/ioutil"
"net"
"os"
"strings"
"sync"
"testing"
"go.uber.org/goleak"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/pkg/errors"
tensorflow "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/luk-ai/lukai/protobuf/aggregatorpb"
"github.com/luk-ai/lukai/testutil"
)
type testEdgeServer struct {
aggregatorpb.EdgeServer
aggregatorpb.AggregatorServer
grpc *grpc.Server
}
func newTestEdgeServer(t *testing.T) *testEdgeServer {
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
EdgeAddress = lis.Addr().String()
grpcServer := grpc.NewServer()
s := &testEdgeServer{
grpc: grpcServer,
}
aggregatorpb.RegisterEdgeServer(grpcServer, s)
aggregatorpb.RegisterAggregatorServer(grpcServer, s)
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
if err := grpcServer.Serve(lis); err != nil {
t.Log(err)
}
}()
wg.Wait()
return s
}
func (s *testEdgeServer) stop() {
s.grpc.GracefulStop()
}
func (s *testEdgeServer) ProdModel(ctx context.Context, in *aggregatorpb.ProdModelRequest) (*aggregatorpb.ProdModelResponse, error) {
return nil, ErrNotImplemented
}
func (s *testEdgeServer) GetWork(
req *aggregatorpb.GetWorkRequest,
stream aggregatorpb.Aggregator_GetWorkServer,
) error {
return ErrNotImplemented
}
func (s *testEdgeServer) ReportWork(stream aggregatorpb.Aggregator_ReportWorkServer) error {
return ErrNotImplemented
}
func newTestModelType(t *testing.T) (*ModelType, func()) {
dir, err := ioutil.TempDir("", "pok-server-TestLog")
if err != nil {
t.Fatalf("%+v", err)
}
mt, err := MakeModelType("test", "test", dir)
if err != nil {
t.Fatal(err)
}
return mt, func() {
if err := mt.Close(); err != nil {
t.Error(err)
}
os.RemoveAll(dir)
}
}
func TestModelTraining(t *testing.T) {
defer goleak.VerifyNoLeaks(t)
s := newTestEdgeServer(t)
defer s.stop()
mt, stop := newTestModelType(t)
defer stop()
ctx := context.Background()
mt.StartTraining(ctx)
mt.training.Lock()
if mt.training.running {
t.Fatalf("ModelType shouldn't be training due to no examples")
}
mt.training.Unlock()
if err := mt.Log(map[string]*tensorflow.Tensor{
"Placeholder_1": newTensor([]float32{1, 2, 3}),
}, []string{"train"}); err != nil {
t.Fatal(err)
}
mt.StartTraining(ctx)
mt.training.Lock()
if !mt.training.running {
t.Fatalf("ModelType should be training")
}
mt.training.Unlock()
mt.StopTraining()
testutil.SucceedsSoon(t, func() error {
mt.training.Lock()
defer mt.training.Unlock()
if mt.training.running {
return errors.New("ModelType should stop training")
}
return nil
})
if err := mt.ExamplesError(); err != nil && !strings.Contains(err.Error(), "context canceled") {
t.Error(err)
}
if err := mt.TrainingError(); err != nil && !strings.Contains(err.Error(), "context canceled") {
t.Error(err)
}
}