-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstate_test.go
158 lines (141 loc) · 3.77 KB
/
state_test.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package serve
import (
"context"
"sync"
"testing"
pb "github.com/cloudquery/plugin-pb-go/pb/plugin/v3"
"github.com/cloudquery/plugin-sdk/v4/internal/clients/state/v3"
"github.com/cloudquery/plugin-sdk/v4/internal/memdb"
"github.com/cloudquery/plugin-sdk/v4/plugin"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestState(t *testing.T) {
p := plugin.NewPlugin(
"testPluginV3",
"v1.0.0",
memdb.NewMemDBClient)
srv := Plugin(p, WithArgs("serve"), WithTestListener())
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
wg.Add(1)
var serverErr error
go func() {
defer wg.Done()
serverErr = srv.Serve(ctx)
}()
defer func() {
cancel()
wg.Wait()
}()
// https://stackoverflow.com/questions/42102496/testing-a-grpc-service
// TODO: Remove once there's a documented migration path per https://github.com/grpc/grpc-go/issues/7244
// nolint:staticcheck
conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(srv.bufPluginDialer), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
t.Fatalf("Failed to dial bufnet: %v", err)
}
c := pb.NewPluginClient(conn)
if _, err := c.Init(ctx, &pb.Init_Request{}); err != nil {
t.Fatal(err)
}
stateClient, err := state.NewClient(ctx, conn, "test")
if err != nil {
t.Fatal(err)
}
if err := stateClient.SetKey(ctx, "key", "value"); err != nil {
t.Fatal(err)
}
val, err := stateClient.GetKey(ctx, "key")
if err != nil {
t.Fatal(err)
}
if val != "value" {
t.Fatalf("expected value to be value but got %s", val)
}
if err := stateClient.Flush(ctx); err != nil {
t.Fatal(err)
}
stateClient, err = state.NewClient(ctx, conn, "test")
if err != nil {
t.Fatal(err)
}
val, err = stateClient.GetKey(ctx, "key")
if err != nil {
t.Fatal(err)
}
if val != "value" {
t.Fatalf("expected value to be value but got %s", val)
}
cancel()
wg.Wait()
if serverErr != nil {
t.Fatal(serverErr)
}
}
func TestStateOverwriteGetLatest(t *testing.T) {
p := plugin.NewPlugin(
"testPluginV3",
"v1.0.0",
memdb.NewMemDBClient)
srv := Plugin(p, WithArgs("serve"), WithTestListener())
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
wg.Add(1)
var serverErr error
go func() {
defer wg.Done()
serverErr = srv.Serve(ctx)
}()
defer func() {
cancel()
wg.Wait()
}()
// https://stackoverflow.com/questions/42102496/testing-a-grpc-service
// TODO: Remove once there's a documented migration path per https://github.com/grpc/grpc-go/issues/7244
// nolint:staticcheck
conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(srv.bufPluginDialer), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
t.Fatalf("Failed to dial bufnet: %v", err)
}
c := pb.NewPluginClient(conn)
if _, err := c.Init(ctx, &pb.Init_Request{}); err != nil {
t.Fatal(err)
}
table := state.Table("test_no_pk")
// Remove PKs
for i := range table.Columns {
table.Columns[i].PrimaryKey = false
}
stateClient, err := state.NewClient(ctx, conn, table.Name)
if err != nil {
t.Fatal(err)
}
for _, v := range []string{"valua", "value1", "value3", "value2"} {
if err := stateClient.SetKey(ctx, "key", v); err != nil {
t.Fatal(err)
}
// Without Flush(), value will only be updated in memory, and we won't get duplicate entries in memdb
if err := stateClient.Flush(ctx); err != nil {
t.Fatal(err)
}
}
stateClient, err = state.NewClient(ctx, conn, table.Name)
if err != nil {
t.Fatal(err)
}
val, err := stateClient.GetKey(ctx, "key")
if err != nil {
t.Fatal(err)
}
if val != `value2` {
t.Fatalf("expected value to be %q but got %q", `value2`, val)
}
cancel()
wg.Wait()
if serverErr != nil {
t.Fatal(serverErr)
}
}