-
Notifications
You must be signed in to change notification settings - Fork 0
/
chanliteral_test.go
310 lines (289 loc) · 8.17 KB
/
chanliteral_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package rel
import (
"fmt"
"reflect"
"testing"
)
// tests & benchmarks for the rel.chanLiteral type
func toChanLiteral(r Relation, isDistinct bool) Relation {
r2, _ := toChanLiteralCancel(r, isDistinct)
return r2
}
func toChanLiteralCancel(r Relation, isDistinct bool) (r2 Relation, c chan<- struct{}) {
cancel := make(chan struct{})
// construct a channel using reflection
z := r.Zero()
e := reflect.TypeOf(z)
ch := reflect.MakeChan(reflect.ChanOf(reflect.SendDir, e), 0)
body := reflect.MakeChan(reflect.ChanOf(reflect.RecvDir, e), 0)
r.TupleChan(body.Interface())
go func(b reflect.Value) {
resSel := reflect.SelectCase{Dir: reflect.SelectSend, Chan: ch}
canSel := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(cancel)}
for {
tup, ok := b.Recv()
if !ok {
break
}
resSel.Send = tup
chosen, _, _ := reflect.Select([]reflect.SelectCase{canSel, resSel})
if chosen == 0 {
return
}
}
ch.Close()
}(body)
c = cancel
r2 = &chanLiteral{ch, r.CKeys(), z, isDistinct, nil}
return
}
func TestChanLiteral(t *testing.T) {
type distinctTup struct {
PNO int
SNO int
}
type nonDistinctTup struct {
PNO int
Qty int
}
type titleCaseTup struct {
Pno int
Sno int
Qty int
}
type joinTup struct {
PNO int
SNO int
Qty int
SName string
Status int
City string
}
type groupByTup struct {
PNO int
Qty int
}
type valTup struct {
Qty int
}
groupFcn := func(val <-chan valTup) valTup {
res := valTup{}
for vi := range val {
res.Qty += vi.Qty
}
return res
}
type mapRes struct {
PNO int
SNO int
Qty1 int
Qty2 int
}
mapFcn := func(tup1 orderTup) mapRes {
return mapRes{tup1.PNO, tup1.SNO, tup1.Qty, tup1.Qty * 2}
}
mapKeys := [][]string{
[]string{"PNO", "SNO"},
}
var relTest = []struct {
rel Relation
expectString string
expectDeg int
expectCard int
}{
{toChanLiteral(orders(), true), "Relation(PNO, SNO, Qty)", 3, 12},
{toChanLiteral(orders(), true).Restrict(Not(Attribute("PNO").EQ(1))), "σ{!(PNO == 1)}(Relation(PNO, SNO, Qty))", 3, 6},
{toChanLiteral(orders(), true).Project(distinctTup{}), "π{PNO, SNO}(Relation(PNO, SNO, Qty))", 2, 12},
{toChanLiteral(orders(), true).Project(nonDistinctTup{}), "π{PNO, Qty}(Relation(PNO, SNO, Qty))", 2, 10},
{toChanLiteral(orders(), true).Rename(titleCaseTup{}), "ρ{Pno, Sno, Qty}/{PNO, SNO, Qty}(Relation(PNO, SNO, Qty))", 3, 12},
{toChanLiteral(orders(), true).Diff(toChanLiteral(orders(), false)), "Relation(PNO, SNO, Qty) − Relation(PNO, SNO, Qty)", 3, 0},
{toChanLiteral(orders(), true).Union(toChanLiteral(orders(), false)), "Relation(PNO, SNO, Qty) ∪ Relation(PNO, SNO, Qty)", 3, 12},
{toChanLiteral(orders(), true).Join(toChanLiteral(suppliers(), false), joinTup{}), "Relation(PNO, SNO, Qty) ⋈ Relation(SNO, SName, Status, City)", 6, 11},
{toChanLiteral(orders(), true).GroupBy(groupByTup{}, groupFcn), "Relation(PNO, SNO, Qty).GroupBy({PNO, Qty}->{Qty})", 2, 4},
{toChanLiteral(orders(), true).Map(mapFcn, mapKeys), "Relation(PNO, SNO, Qty).Map({PNO, SNO, Qty}->{PNO, SNO, Qty1, Qty2})", 4, 12},
{toChanLiteral(orders(), true).Map(mapFcn, [][]string{}), "Relation(PNO, SNO, Qty).Map({PNO, SNO, Qty}->{PNO, SNO, Qty1, Qty2})", 4, 12},
}
for i, tt := range relTest {
if err := tt.rel.Err(); err != nil {
t.Errorf("%d has Err() => %s", i, err.Error())
continue
}
if str := tt.rel.String(); str != tt.expectString {
t.Errorf("%d has String() => %v, want %v", i, str, tt.expectString)
}
if deg := Deg(tt.rel); deg != tt.expectDeg {
t.Errorf("%d %s has Deg() => %v, want %v", i, tt.expectString, deg, tt.expectDeg)
}
if card := Card(tt.rel); card != tt.expectCard {
t.Errorf("%d %s has Card() => %v, want %v", i, tt.expectString, card, tt.expectCard)
}
}
// test cancellation
r, cancelSource := toChanLiteralCancel(orders(), true)
res := make(chan orderTup)
cancel := r.TupleChan(res)
close(cancel)
select {
case <-res:
t.Errorf("cancel did not end tuple generation")
default:
// passed test
}
close(cancelSource)
// test non distinct & cancellation
r, cancelSource = toChanLiteralCancel(orders(), false)
res = make(chan orderTup)
cancel = r.TupleChan(res)
close(cancel)
select {
case <-res:
t.Errorf("cancel did not end tuple generation")
default:
// passed test
}
close(cancelSource)
// test errors
err := fmt.Errorf("testing error")
r1 := new(chanLiteral)
r1 = toChanLiteral(orders(), true).(*chanLiteral)
r1.err = err
r2 := new(chanLiteral)
r2 = toChanLiteral(orders(), true).(*chanLiteral)
r2.err = err
res = make(chan orderTup)
_ = r1.TupleChan(res)
if _, ok := <-res; ok {
t.Errorf("chanliteral did not short circuit TupleChan")
}
errTest := []Relation{
r1.Project(distinctTup{}),
r1.Restrict(Not(Attribute("PNO").EQ(1))),
r1.Rename(titleCaseTup{}),
r1.Union(r2),
r.Union(r2),
r1.Diff(r2),
r.Diff(r2),
r1.Join(r2, orderTup{}),
r.Join(r2, orderTup{}),
r1.GroupBy(groupByTup{}, groupFcn),
r1.Map(mapFcn, mapKeys),
}
for i, errRel := range errTest {
if errRel.Err() != err {
t.Errorf("%d did not short circuit error", i)
}
}
}
// unlike the rel.Map and rel.Slice type, this has to drain the resulting
// relation, otherwise there will be hanging go-routines. It would be better
// if we could cancel, but that might require a different type of relation.
// this allows us to drain a channel so that the source goroutines finish
func drain(t chan exTup2) {
for _ = range t {
}
return
}
func BenchmarkChanLiteralNewTinySimple(b *testing.B) {
// test the time it takes to make a new relation with a given size
exRel := exampleRelChan2(10)
ck := [][]string{[]string{"foo"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewTinyNonDistinct(b *testing.B) {
// test the time it takes to make a new relation with a given size,
// but without any candidate keys. The New function will run
// a distinct on the input data.
exRel := exampleRelChan2(10)
ck := [][]string{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewSmallSimple(b *testing.B) {
// test the time it takes to make a new relation with a given size
exRel := exampleRelChan2(1000)
ck := [][]string{[]string{"foo"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewSmallNonDistinct(b *testing.B) {
// test the time it takes to make a new relation with a given size,
// but without any candidate keys. The New function will run
// a distinct on the input data.
exRel := exampleRelChan2(1000)
ck := [][]string{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewMediumSimple(b *testing.B) {
// test the time it takes to make a new relation with a given size
exRel := exampleRelChan2(100000)
ck := [][]string{[]string{"foo"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewMediumNonDistinct(b *testing.B) {
// test the time it takes to make a new relation with a given size,
// but without any candidate keys. The New function will run
// a distinct on the input data.
exRel := exampleRelChan2(100000)
ck := [][]string{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewLargeSimple(b *testing.B) {
// test the time it takes to make a new relation with a given size
exRel := exampleRelChan2(10000000)
ck := [][]string{[]string{"foo"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}
func BenchmarkChanLiteralNewLargeNonDistinct(b *testing.B) {
// test the time it takes to make a new relation with a given size,
// but without any candidate keys. The New function will run
// a distinct on the input data.
exRel := exampleRelChan2(10000000)
ck := [][]string{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
New(exRel, ck)
}
// channel cleanup
b.StopTimer()
drain(exRel)
}