This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
107 lines (86 loc) · 2.43 KB
/
fake.go
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
package cloudtasks
import (
"context"
"fmt"
"log"
"net"
"strings"
"testing"
"google.golang.org/protobuf/proto"
"google.golang.org/api/option"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type Faker struct {
mock *mockCloudTasksServer
ClientOpt option.ClientOption
}
func NewFaker(t *testing.T) *Faker {
t.Helper()
mockCloudTasks := mockCloudTasksServer{}
serv := grpc.NewServer()
taskspb.RegisterCloudTasksServer(serv, &mockCloudTasks)
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
log.Fatal(err)
}
go serv.Serve(lis)
conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
return &Faker{
mock: &mockCloudTasks,
ClientOpt: option.WithGRPCConn(conn),
}
}
// AddMockResponse is Set Response when CreateTask is called
//
// Setting err returns that err.
// When returning a normal Response, err specifies nil.
func (f *Faker) AddMockResponse(err error, resp ...proto.Message) {
f.mock.tasks = append(f.mock.tasks, &mockTaskContainer{
err: err,
resp: resp,
})
}
// GetCreateTaskCallCount is Returns the number of times CreateTask was called
func (f *Faker) GetCreateTaskCallCount() int {
return f.mock.createTaskCallCount
}
// GetCreateTaskRequest is Returns the request passed to CreateTask
func (f *Faker) GetCreateTaskRequest(i int) ([]proto.Message, error) {
if i > len(f.mock.tasks)-1 {
return nil, fmt.Errorf("GetCreateTaskRequest out of range. len=%d", len(f.mock.tasks))
}
return f.mock.tasks[i].reqs, nil
}
type mockTaskContainer struct {
reqs []proto.Message
err error
resp []proto.Message
}
type mockCloudTasksServer struct {
// Embed for forward compatibility.
// Tests will keep working if more methods are added
// in the future.
taskspb.CloudTasksServer
tasks []*mockTaskContainer
tasksIndex int
createTaskCallCount int
}
func (s *mockCloudTasksServer) CreateTask(ctx context.Context, req *taskspb.CreateTaskRequest) (*taskspb.Task, error) {
md, _ := metadata.FromIncomingContext(ctx)
if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
}
task := s.tasks[s.tasksIndex]
s.tasksIndex++
s.createTaskCallCount++
task.reqs = append(task.reqs, req)
if task.err != nil {
return nil, task.err
}
return task.resp[0].(*taskspb.Task), nil
}