-
Notifications
You must be signed in to change notification settings - Fork 37
/
goon_test.go
3405 lines (3066 loc) · 107 KB
/
goon_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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012 The Goon Authors
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package goon
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/golang/protobuf/proto"
"google.golang.org/appengine/v2"
"google.golang.org/appengine/v2/aetest"
"google.golang.org/appengine/v2/datastore"
"google.golang.org/appengine/v2/memcache"
)
func init() {
// The SDK emulators are extremely slow, so we can't use production timeouts
MemcachePutTimeoutSmall = 10 * time.Second
MemcacheGetTimeout = 10 * time.Second
// Make sure to propagate all errors for better testing
propagateMemcachePutError = true
}
// *[]S, *[]*S, *[]I, []S, []*S, []I,
// *[]PLS, *[]*PLS, *[]IPLS, []PLS, []*PLS, []IPLS
const (
ivTypePtrToSliceOfStructs = iota
ivTypePtrToSliceOfPtrsToStruct
ivTypePtrToSliceOfInterfaces
ivTypeSliceOfStructs
ivTypeSliceOfPtrsToStruct
ivTypeSliceOfInterfaces
ivTypePtrToSliceOfPLS
ivTypePtrToSliceOfPtrsToPLS
ivTypePtrToSliceOfInterfacesPLS
ivTypeSliceOfPLS
ivTypeSliceOfPtrsToPLS
ivTypeSliceOfInterfacesPLS
ivTypeTotal
)
const (
ivModeDatastore = iota
ivModeMemcache
ivModeMemcacheAndDatastore
ivModeLocalcache
ivModeLocalcacheAndMemcache
ivModeLocalcacheAndDatastore
ivModeLocalcacheAndMemcacheAndDatastore
ivModeTotal
)
func cloneKey(key *datastore.Key) *datastore.Key {
if key == nil {
return nil
}
dupe, err := datastore.DecodeKey(key.Encode())
if err != nil {
panic(fmt.Sprintf("Failed to clone key: %v", err))
}
return dupe
}
func cloneKeys(keys []*datastore.Key) []*datastore.Key {
if keys == nil {
return nil
}
dupe := make([]*datastore.Key, 0, len(keys))
for _, key := range keys {
if key == nil {
dupe = append(dupe, nil)
} else {
dupe = append(dupe, cloneKey(key))
}
}
return dupe
}
func TestCloneIVItem(t *testing.T) {
c, done, err := aetest.NewContext()
if err != nil {
t.Fatalf("Could not start aetest - %v", err)
}
defer done()
initializeIvItems(c)
for i := range ivItems {
clone := *ivItems[i].clone()
if !reflect.DeepEqual(ivItems[i], clone) {
t.Fatalf("ivItem clone failed!\n%v", getDiff(ivItems[i], clone, fmt.Sprintf("ivItems[%d]", i), "clone"))
}
}
}
// Have a bunch of different supported types to detect any wild errors
// https://cloud.google.com/appengine/docs/standard/go/datastore/reference
//
// - signed integers (int, int8, int16, int32 and int64),
// - bool,
// - string,
// - float32 and float64,
// - []byte (up to 1 megabyte in length),
// - any type whose underlying type is one of the above predeclared types,
// - ByteString,
// - *Key,
// - time.Time (stored with microsecond precision),
// - appengine.BlobKey,
// - appengine.GeoPoint,
// - structs whose fields are all valid value types,
// - slices of any of the above.
//
// In addition, although undocumented, there's also support for any type,
// whose underlying type is a legal slice.
type ivItem struct {
Id int64 `datastore:"-" goon:"id"`
Kind string `datastore:"-" goon:"kind,ivItem"`
Int int `datastore:"int,noindex"`
Int8 int8 `datastore:"int8,noindex"`
Int16 int16 `datastore:"int16,noindex"`
Int32 int32 `datastore:"int32,noindex"`
Int64 int64 `datastore:"int64,noindex"`
Bool bool `datastore:"bool,noindex"`
String string `datastore:"string,noindex"`
Float32 float32 `datastore:"float32,noindex"`
Float64 float64 `datastore:"float64,noindex"`
ByteSlice []byte `datastore:"byte_slice,noindex"`
CustomTypes ivItemCustom `datastore:"custom,noindex"`
BString datastore.ByteString `datastore:"bstr,noindex"`
Key *datastore.Key `datastore:"key,noindex"`
Time time.Time `datastore:"time,noindex"`
BlobKey appengine.BlobKey `datastore:"bk,noindex"`
GeoPoint appengine.GeoPoint `datastore:"gp,noindex"`
Sub ivItemSub `datastore:"sub,noindex"`
SliceTypes ivItemSlice `datastore:"slice,noindex"`
CustomSlices ivItemSliceCustom `datastore:"custom_slice,noindex"`
NoIndex int `datastore:",noindex"`
Casual string
Ζεύς string
ChildKey *datastore.Key
ZeroKey *datastore.Key
KeySliceNil []*datastore.Key
SaveCount int
LoadCount int
}
func (ivi ivItem) clone() *ivItem {
return &ivItem{
Id: ivi.Id,
Int: ivi.Int,
Int8: ivi.Int8,
Int16: ivi.Int16,
Int32: ivi.Int32,
Int64: ivi.Int64,
Bool: ivi.Bool,
String: ivi.String,
Float32: ivi.Float32,
Float64: ivi.Float64,
ByteSlice: append(ivi.ByteSlice[:0:0], ivi.ByteSlice...),
CustomTypes: *ivi.CustomTypes.clone(),
BString: append(ivi.BString[:0:0], ivi.BString...),
Key: cloneKey(ivi.Key),
Time: ivi.Time,
BlobKey: ivi.BlobKey,
GeoPoint: ivi.GeoPoint,
Sub: *ivi.Sub.clone(),
SliceTypes: *ivi.SliceTypes.clone(),
CustomSlices: *ivi.CustomSlices.clone(),
NoIndex: ivi.NoIndex,
Casual: ivi.Casual,
Ζεύς: ivi.Ζεύς,
ChildKey: cloneKey(ivi.ChildKey),
ZeroKey: cloneKey(ivi.ZeroKey),
KeySliceNil: cloneKeys(ivi.KeySliceNil),
SaveCount: ivi.SaveCount,
LoadCount: ivi.LoadCount,
}
}
type ivItemInt int
type ivItemInt8 int8
type ivItemInt16 int16
type ivItemInt32 int32
type ivItemInt64 int64
type ivItemBool bool
type ivItemString string
type ivItemFloat32 float32
type ivItemFloat64 float64
type ivItemByteSlice []byte
type ivItemDeepInt ivItemInt
type ivItemCustom struct {
Int ivItemInt
Int8 ivItemInt8
Int16 ivItemInt16
Int32 ivItemInt32
Int64 ivItemInt64
Bool ivItemBool
String ivItemString
Float32 ivItemFloat32
Float64 ivItemFloat64
ByteSlice ivItemByteSlice
DeepInt ivItemDeepInt
}
func (ivic ivItemCustom) clone() *ivItemCustom {
return &ivItemCustom{
Int: ivic.Int,
Int8: ivic.Int8,
Int16: ivic.Int16,
Int32: ivic.Int32,
Int64: ivic.Int64,
Bool: ivic.Bool,
String: ivic.String,
Float32: ivic.Float32,
Float64: ivic.Float64,
ByteSlice: append(ivic.ByteSlice[:0:0], ivic.ByteSlice...),
DeepInt: ivic.DeepInt,
}
}
type ivItemSlice struct {
Int []int
Int8 []int8
Int16 []int16
Int32 []int32
Int64 []int64
Bool []bool
String []string
Float32 []float32
Float64 []float64
BSSlice [][]byte
IntC []ivItemInt
Int8C []ivItemInt8
Int16C []ivItemInt16
Int32C []ivItemInt32
Int64C []ivItemInt64
BoolC []ivItemBool
StringC []ivItemString
Float32C []ivItemFloat32
Float64C []ivItemFloat64
BSSliceC []ivItemByteSlice
DeepInt []ivItemDeepInt
BStrSlice []datastore.ByteString
KeySlice []*datastore.Key
TimeSlice []time.Time
BKSlice []appengine.BlobKey
GPSlice []appengine.GeoPoint
Subs []ivItemSubs
}
func (ivis ivItemSlice) clone() *ivItemSlice {
bsSlice := ivis.BSSlice[:0:0]
for _, bs := range ivis.BSSlice {
bsSlice = append(bsSlice, append(bs[:0:0], bs...))
}
bsSliceC := ivis.BSSliceC[:0:0]
for _, bsc := range ivis.BSSliceC {
bsSliceC = append(bsSliceC, append(bsc[:0:0], bsc...))
}
bstrSlice := ivis.BStrSlice[:0:0]
for _, bstr := range ivis.BStrSlice {
bstrSlice = append(bstrSlice, append(bstr[:0:0], bstr...))
}
subs := ivis.Subs[:0:0]
for _, sub := range ivis.Subs {
subs = append(subs, *sub.clone())
}
return &ivItemSlice{
Int: append(ivis.Int[:0:0], ivis.Int...),
Int8: append(ivis.Int8[:0:0], ivis.Int8...),
Int16: append(ivis.Int16[:0:0], ivis.Int16...),
Int32: append(ivis.Int32[:0:0], ivis.Int32...),
Int64: append(ivis.Int64[:0:0], ivis.Int64...),
Bool: append(ivis.Bool[:0:0], ivis.Bool...),
String: append(ivis.String[:0:0], ivis.String...),
Float32: append(ivis.Float32[:0:0], ivis.Float32...),
Float64: append(ivis.Float64[:0:0], ivis.Float64...),
BSSlice: bsSlice,
IntC: append(ivis.IntC[:0:0], ivis.IntC...),
Int8C: append(ivis.Int8C[:0:0], ivis.Int8C...),
Int16C: append(ivis.Int16C[:0:0], ivis.Int16C...),
Int32C: append(ivis.Int32C[:0:0], ivis.Int32C...),
Int64C: append(ivis.Int64C[:0:0], ivis.Int64C...),
BoolC: append(ivis.BoolC[:0:0], ivis.BoolC...),
StringC: append(ivis.StringC[:0:0], ivis.StringC...),
Float32C: append(ivis.Float32C[:0:0], ivis.Float32C...),
Float64C: append(ivis.Float64C[:0:0], ivis.Float64C...),
BSSliceC: bsSliceC,
DeepInt: append(ivis.DeepInt[:0:0], ivis.DeepInt...),
BStrSlice: bstrSlice,
KeySlice: cloneKeys(ivis.KeySlice),
TimeSlice: append(ivis.TimeSlice[:0:0], ivis.TimeSlice...),
BKSlice: append(ivis.BKSlice[:0:0], ivis.BKSlice...),
GPSlice: append(ivis.GPSlice[:0:0], ivis.GPSlice...),
Subs: subs,
}
}
type IntS []int
type Int8S []int8
type Int16S []int16
type Int32S []int32
type Int64S []int64
type BoolS []bool
type StringS []string
type Float32S []float32
type Float64S []float64
type BSSliceS [][]byte
type IntCS []ivItemInt
type Int8CS []ivItemInt8
type Int16CS []ivItemInt16
type Int32CS []ivItemInt32
type Int64CS []ivItemInt64
type BoolCS []ivItemBool
type StringCS []ivItemString
type Float32CS []ivItemFloat32
type Float64CS []ivItemFloat64
type BSSliceCS []ivItemByteSlice
type DeepIntS []ivItemDeepInt
type BStrSliceS []datastore.ByteString
type KeySliceS []*datastore.Key
type TimeSliceS []time.Time
type BKSliceS []appengine.BlobKey
type GPSliceS []appengine.GeoPoint
type SubsS []ivItemSubs
type ivItemSliceCustom struct {
Int IntS
Int8 Int8S
Int16 Int16S
Int32 Int32S
Int64 Int64S
Bool BoolS
String StringS
Float32 Float32S
Float64 Float64S
BSSlice BSSliceS
IntC IntCS
Int8C Int8CS
Int16C Int16CS
Int32C Int32CS
Int64C Int64CS
BoolC BoolCS
StringC StringCS
Float32C Float32CS
Float64C Float64CS
BSSliceC BSSliceCS
DeepInt DeepIntS
BStrSlice BStrSliceS
KeySlice KeySliceS
TimeSlice TimeSliceS
BKSlice BKSliceS
GPSlice GPSliceS
Subs SubsS
}
func (ivisc ivItemSliceCustom) clone() *ivItemSliceCustom {
bsSlice := ivisc.BSSlice[:0:0]
for _, bs := range ivisc.BSSlice {
bsSlice = append(bsSlice, append(bs[:0:0], bs...))
}
bsSliceC := ivisc.BSSliceC[:0:0]
for _, bsc := range ivisc.BSSliceC {
bsSliceC = append(bsSliceC, append(bsc[:0:0], bsc...))
}
bstrSlice := ivisc.BStrSlice[:0:0]
for _, bstr := range ivisc.BStrSlice {
bstrSlice = append(bstrSlice, append(bstr[:0:0], bstr...))
}
subs := ivisc.Subs[:0:0]
for _, sub := range ivisc.Subs {
subs = append(subs, *sub.clone())
}
return &ivItemSliceCustom{
Int: append(ivisc.Int[:0:0], ivisc.Int...),
Int8: append(ivisc.Int8[:0:0], ivisc.Int8...),
Int16: append(ivisc.Int16[:0:0], ivisc.Int16...),
Int32: append(ivisc.Int32[:0:0], ivisc.Int32...),
Int64: append(ivisc.Int64[:0:0], ivisc.Int64...),
Bool: append(ivisc.Bool[:0:0], ivisc.Bool...),
String: append(ivisc.String[:0:0], ivisc.String...),
Float32: append(ivisc.Float32[:0:0], ivisc.Float32...),
Float64: append(ivisc.Float64[:0:0], ivisc.Float64...),
BSSlice: bsSlice,
IntC: append(ivisc.IntC[:0:0], ivisc.IntC...),
Int8C: append(ivisc.Int8C[:0:0], ivisc.Int8C...),
Int16C: append(ivisc.Int16C[:0:0], ivisc.Int16C...),
Int32C: append(ivisc.Int32C[:0:0], ivisc.Int32C...),
Int64C: append(ivisc.Int64C[:0:0], ivisc.Int64C...),
BoolC: append(ivisc.BoolC[:0:0], ivisc.BoolC...),
StringC: append(ivisc.StringC[:0:0], ivisc.StringC...),
Float32C: append(ivisc.Float32C[:0:0], ivisc.Float32C...),
Float64C: append(ivisc.Float64C[:0:0], ivisc.Float64C...),
BSSliceC: bsSliceC,
DeepInt: append(ivisc.DeepInt[:0:0], ivisc.DeepInt...),
BStrSlice: bstrSlice,
KeySlice: cloneKeys(ivisc.KeySlice),
TimeSlice: append(ivisc.TimeSlice[:0:0], ivisc.TimeSlice...),
BKSlice: append(ivisc.BKSlice[:0:0], ivisc.BKSlice...),
GPSlice: append(ivisc.GPSlice[:0:0], ivisc.GPSlice...),
Subs: subs,
}
}
type ivItemSub struct {
Data string `datastore:"data,noindex"`
Ints []int `datastore:"ints,noindex"`
}
func (ivis ivItemSub) clone() *ivItemSub {
return &ivItemSub{
Data: ivis.Data,
Ints: append(ivis.Ints[:0:0], ivis.Ints...),
}
}
type ivItemSubs struct {
Key *datastore.Key `datastore:"key,noindex"`
Data string `datastore:"data,noindex"`
Extra string `datastore:",noindex"`
}
func (ivis ivItemSubs) clone() *ivItemSubs {
return &ivItemSubs{
Key: cloneKey(ivis.Key),
Data: ivis.Data,
Extra: ivis.Extra,
}
}
func (ivi *ivItem) ForInterface() {}
func (ivi *ivItemPLS) ForInterface() {}
type ivItemI interface {
ForInterface()
}
// Implement the PropertyLoadSave interface for ivItem
type ivItemPLS ivItem
func (ivi *ivItemPLS) Save() ([]datastore.Property, error) {
ivi.SaveCount++
return datastore.SaveStruct(ivi)
}
func (ivi *ivItemPLS) Load(props []datastore.Property) error {
err := datastore.LoadStruct(ivi, props)
ivi.LoadCount++
return err
}
var ivItems []ivItem
var ivItemKeys []*datastore.Key
func initializeIvItems(c context.Context) {
// We force UTC, because the datastore API will always return UTC
t1 := time.Now().UTC().Truncate(time.Microsecond)
t2 := t1.Add(time.Second * 1)
t3 := t1.Add(time.Second * 2)
ivi1 := &ivItem{
Id: 1,
Int: 123,
Int8: 77,
Int16: 13001,
Int32: 1234567890,
Int64: 123456789012345,
Bool: true,
String: "one",
Float32: (float32(10) / float32(3)),
Float64: (float64(10000000) / float64(9998)),
ByteSlice: []byte{0xDE, 0xAD},
CustomTypes: ivItemCustom{
Int: 123,
Int8: 77,
Int16: 13001,
Int32: 1234567890,
Int64: 123456789012345,
Bool: true,
String: "one",
Float32: ivItemFloat32(float32(10) / float32(3)),
Float64: ivItemFloat64(float64(10000000) / float64(9998)),
ByteSlice: ivItemByteSlice([]byte{0x01, 0x02, 0xAA}),
DeepInt: 1,
},
BString: datastore.ByteString([]byte{0xAB}),
Key: datastore.NewKey(c, "Fruit", "Apple", 0, nil),
Time: t1,
BlobKey: appengine.BlobKey("fake #1"),
GeoPoint: appengine.GeoPoint{Lat: 1.1, Lng: 2.2},
Sub: ivItemSub{
Data: "yay #1",
Ints: []int{1, 2, 3},
},
SliceTypes: ivItemSlice{
Int: []int{1, 2},
Int8: []int8{1, 2},
Int16: []int16{1, 2},
Int32: []int32{1, 2},
Int64: []int64{1, 2},
Bool: []bool{true, false},
String: []string{"one", "two"},
Float32: []float32{1.0, 2.0},
Float64: []float64{1.0, 2.0},
BSSlice: [][]byte{{0x01, 0x02}, {0x03, 0x04}},
IntC: []ivItemInt{1, 2},
Int8C: []ivItemInt8{1, 2},
Int16C: []ivItemInt16{1, 2},
Int32C: []ivItemInt32{1, 2},
Int64C: []ivItemInt64{1, 2},
BoolC: []ivItemBool{true, false},
StringC: []ivItemString{"one", "two"},
Float32C: []ivItemFloat32{1.0, 2.0},
Float64C: []ivItemFloat64{1.0, 2.0},
BSSliceC: []ivItemByteSlice{{0x01, 0x02}, {0x03, 0x04}},
DeepInt: []ivItemDeepInt{1, 2},
BStrSlice: []datastore.ByteString{datastore.ByteString("one"), datastore.ByteString("two")},
KeySlice: []*datastore.Key{datastore.NewKey(c, "Key", "", 1, nil), datastore.NewKey(c, "Key", "", 2, nil), datastore.NewKey(c, "Key", "", 3, nil)},
TimeSlice: []time.Time{t1, t2, t3},
BKSlice: []appengine.BlobKey{appengine.BlobKey("fake #1.1"), appengine.BlobKey("fake #1.2")},
GPSlice: []appengine.GeoPoint{{Lat: 1.1, Lng: -2.2}, {Lat: -3.3, Lng: 4.4}},
Subs: []ivItemSubs{
{Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil), Data: "sub #1.1", Extra: "xtra #1.1"},
{Key: nil, Data: "sub #1.2", Extra: "xtra #1.2"},
{Key: datastore.NewKey(c, "Fruit", "Cherry", 0, nil), Data: "sub #1.3", Extra: "xtra #1.3"},
},
},
CustomSlices: ivItemSliceCustom{
Int: IntS{1, 2},
Int8: Int8S{1, 2},
Int16: Int16S{1, 2},
Int32: Int32S{1, 2},
Int64: Int64S{1, 2},
Bool: BoolS{true, false},
String: StringS{"one", "two"},
Float32: Float32S{1.0, 2.0},
Float64: Float64S{1.0, 2.0},
BSSlice: BSSliceS{[]byte{0x01, 0x02}, []byte{0x03, 0x04}},
IntC: IntCS{1, 2},
Int8C: Int8CS{1, 2},
Int16C: Int16CS{1, 2},
Int32C: Int32CS{1, 2},
Int64C: Int64CS{1, 2},
BoolC: BoolCS{true, false},
StringC: StringCS{"one", "two"},
Float32C: Float32CS{1.0, 2.0},
Float64C: Float64CS{1.0, 2.0},
BSSliceC: BSSliceCS{ivItemByteSlice{0x01, 0x02}, ivItemByteSlice{0x03, 0x04}},
DeepInt: DeepIntS{1, 2},
BStrSlice: BStrSliceS{datastore.ByteString("one"), datastore.ByteString("two")},
KeySlice: KeySliceS{datastore.NewKey(c, "Key", "", 1, nil), datastore.NewKey(c, "Key", "", 2, nil), datastore.NewKey(c, "Key", "", 3, nil)},
TimeSlice: TimeSliceS{t1, t2, t3},
BKSlice: BKSliceS{appengine.BlobKey("fake #1.1"), appengine.BlobKey("fake #1.2")},
GPSlice: GPSliceS{appengine.GeoPoint{Lat: 1.1, Lng: -2.2}, appengine.GeoPoint{Lat: -3.3, Lng: 4.4}},
Subs: SubsS{
{Key: datastore.NewKey(c, "Fruit", "Banana", 0, nil), Data: "sub #1.1", Extra: "xtra #1.1"},
{Key: datastore.NewKey(c, "Fruit", "Cherry", 0, nil), Data: "sub #1.2", Extra: "xtra #1.2"},
{Key: nil, Data: "sub #1.3", Extra: "xtra #1.3"},
},
},
NoIndex: 1,
Casual: "clothes",
Ζεύς: "Zeus",
ChildKey: datastore.NewKey(c, "Person", "Jane", 0, datastore.NewKey(c, "Person", "John", 0, datastore.NewKey(c, "Person", "Jack", 0, nil))),
ZeroKey: nil,
KeySliceNil: []*datastore.Key{datastore.NewKey(c, "Number", "", 1, nil), nil, datastore.NewKey(c, "Number", "", 2, nil)},
}
ivi2 := ivi1.clone()
ivi2.Id = 2
ivi3 := ivi1.clone()
ivi3.Id = 3
ivItems = append(ivItems, *ivi1)
ivItems = append(ivItems, *ivi2)
ivItems = append(ivItems, *ivi3)
g := FromContext(c)
for i := range ivItems {
ivItemKeys = append(ivItemKeys, g.Key(&ivItems[i]))
}
}
func getInputVarietyItem(t *testing.T, g *Goon, ivType int, empty bool, indices ...int) interface{} {
var result interface{}
getItem := func(index int) *ivItem {
if empty {
return &ivItem{Id: ivItems[index].Id}
}
return ivItems[index].clone()
}
switch ivType {
case ivTypePtrToSliceOfStructs:
s := []ivItem{}
for _, index := range indices {
s = append(s, *getItem(index))
}
result = &s
case ivTypePtrToSliceOfPtrsToStruct:
s := []*ivItem{}
for _, index := range indices {
s = append(s, getItem(index))
}
result = &s
case ivTypePtrToSliceOfInterfaces:
s := []ivItemI{}
for _, index := range indices {
s = append(s, getItem(index))
}
result = &s
case ivTypeSliceOfStructs:
s := []ivItem{}
for _, index := range indices {
s = append(s, *getItem(index))
}
result = s
case ivTypeSliceOfPtrsToStruct:
s := []*ivItem{}
for _, index := range indices {
s = append(s, getItem(index))
}
result = s
case ivTypeSliceOfInterfaces:
s := []ivItemI{}
for _, index := range indices {
s = append(s, getItem(index))
}
result = s
case ivTypePtrToSliceOfPLS:
s := []ivItemPLS{}
for _, index := range indices {
s = append(s, (ivItemPLS)(*getItem(index)))
}
result = &s
case ivTypePtrToSliceOfPtrsToPLS:
s := []*ivItemPLS{}
for _, index := range indices {
s = append(s, (*ivItemPLS)(getItem(index)))
}
result = &s
case ivTypePtrToSliceOfInterfacesPLS:
s := []ivItemI{}
for _, index := range indices {
s = append(s, (*ivItemPLS)(getItem(index)))
}
result = &s
case ivTypeSliceOfPLS:
s := []ivItemPLS{}
for _, index := range indices {
s = append(s, (ivItemPLS)(*getItem(index)))
}
result = s
case ivTypeSliceOfPtrsToPLS:
s := []*ivItemPLS{}
for _, index := range indices {
s = append(s, (*ivItemPLS)(getItem(index)))
}
result = s
case ivTypeSliceOfInterfacesPLS:
s := []ivItemI{}
for _, index := range indices {
s = append(s, (*ivItemPLS)(getItem(index)))
}
result = s
default:
t.Fatalf("Invalid input variety type! %v", ivType)
return nil
}
return result
}
func getPrettyIVMode(ivMode int) string {
result := "N/A"
switch ivMode {
case ivModeDatastore:
result = "DS"
case ivModeMemcache:
result = "MC"
case ivModeMemcacheAndDatastore:
result = "DS+MC"
case ivModeLocalcache:
result = "LC"
case ivModeLocalcacheAndMemcache:
result = "MC+LC"
case ivModeLocalcacheAndDatastore:
result = "DS+LC"
case ivModeLocalcacheAndMemcacheAndDatastore:
result = "DS+MC+LC"
}
return result
}
func getPrettyIVType(ivType int) string {
result := "N/A"
switch ivType {
case ivTypePtrToSliceOfStructs:
result = "*[]S"
case ivTypePtrToSliceOfPtrsToStruct:
result = "*[]*S"
case ivTypePtrToSliceOfInterfaces:
result = "*[]I"
case ivTypeSliceOfStructs:
result = "[]S"
case ivTypeSliceOfPtrsToStruct:
result = "[]*S"
case ivTypeSliceOfInterfaces:
result = "[]I"
case ivTypePtrToSliceOfPLS:
result = "*[]PLS"
case ivTypePtrToSliceOfPtrsToPLS:
result = "*[]*PLS"
case ivTypePtrToSliceOfInterfacesPLS:
result = "*[]IPLS"
case ivTypeSliceOfPLS:
result = "[]PLS"
case ivTypeSliceOfPtrsToPLS:
result = "[]*PLS"
case ivTypeSliceOfInterfacesPLS:
result = "[]IPLS"
}
return result
}
func isIVTypePLS(ivType int) bool {
switch ivType {
case ivTypePtrToSliceOfPLS,
ivTypePtrToSliceOfPtrsToPLS,
ivTypePtrToSliceOfInterfacesPLS,
ivTypeSliceOfPLS,
ivTypeSliceOfPtrsToPLS,
ivTypeSliceOfInterfacesPLS:
return true
}
return false
}
// getDiff is a helper function that returns string lines describing the differences between a & b
func getDiff(a, b interface{}, aName, bName string) string {
var buf bytes.Buffer
av := reflect.Indirect(reflect.ValueOf(a))
bv := reflect.Indirect(reflect.ValueOf(b))
switch av.Kind() {
case reflect.Slice:
if av.Len() != bv.Len() {
buf.WriteString(fmt.Sprintf("%v has len %v, but %v has len %v\n", aName, av.Len(), bName, bv.Len()))
} else {
for i := 0; i < av.Len(); i++ {
avi := av.Index(i).Interface()
bvi := bv.Index(i).Interface()
buf.WriteString(getDiff(avi, bvi, fmt.Sprintf("%s[%d]", aName, i), fmt.Sprintf("%s[%d]", bName, i)))
}
}
case reflect.Struct:
if av.NumField() != bv.NumField() {
buf.WriteString(fmt.Sprintf("%v has %v fields, but %v has %v fields\n", aName, av.NumField(), bName, bv.NumField()))
} else {
for i := 0; i < av.NumField(); i++ {
avf := av.Field(i)
bvf := bv.Field(i)
avft := av.Type().Field(i)
bvft := bv.Type().Field(i)
avftName := fmt.Sprintf("%s.%s", aName, avft.Name)
bvftName := fmt.Sprintf("%s.%s", bName, bvft.Name)
if avft.Type != bvft.Type {
buf.WriteString(fmt.Sprintf("%v has type %v, but %v has type %v\n", avftName, avft.Type, bvftName, bvft.Type))
} else {
if avft.PkgPath == "" || avft.Anonymous || bvft.PkgPath == "" || bvft.Anonymous {
buf.WriteString(getDiff(avf.Interface(), bvf.Interface(), avftName, bvftName))
}
}
}
}
default:
if !reflect.DeepEqual(a, b) {
buf.WriteString(fmt.Sprintf("MISMATCH: %v == %v | %v == %v\n", aName, a, bName, b))
}
}
return buf.String()
}
func onlyErrNoSuchEntity(err error) bool {
if err == nil {
return false
}
merr, ok := err.(appengine.MultiError)
if !ok || len(merr) == 0 {
return false
}
for i := 0; i < len(merr); i++ {
if merr[i] != datastore.ErrNoSuchEntity {
return false
}
}
return true
}
func ivGetMulti(t *testing.T, g *Goon, ref, dst interface{}, prettyInfo string) error {
// Get our data back and make sure it's correct
if err := g.GetMulti(dst); err != nil {
t.Fatalf("%s > Unexpected error on GetMulti - %v", prettyInfo, err)
return err
} else {
dstLen := reflect.Indirect(reflect.ValueOf(dst)).Len()
refLen := reflect.Indirect(reflect.ValueOf(ref)).Len()
if dstLen != refLen {
t.Fatalf("%s > Unexpected dst len (%v) doesn't match ref len (%v)", prettyInfo, dstLen, refLen)
} else if !reflect.DeepEqual(ref, dst) {
t.Fatalf("%s > ivGetMulti didn't return what was expected:\n%s", prettyInfo, getDiff(ref, dst, "ref", "dst"))
}
}
return nil
}
func setPLSCounts(ref interface{}, saveCount, loadCount bool) {
// Confirm that Save() and Load() are called as specified
v := reflect.Indirect(reflect.ValueOf(ref))
for i := 0; i < v.Len(); i++ {
vi := reflect.Indirect(v.Index(i))
if vi.Kind() == reflect.Interface {
vi = reflect.Indirect(vi.Elem())
}
if saveCount {
vi.FieldByName("SaveCount").SetInt(1)
}
if loadCount {
vi.FieldByName("LoadCount").SetInt(1)
}
}
}
// This function marks either all or the provided indices of target as dirty.
// It's purpose is to use it on entities fetched via Get,
// and then see if refetching those entities returns the dirty entities.
func makeDirty(target interface{}, indices ...int) {
if target == nil {
return
}
v := reflect.Indirect(reflect.ValueOf(target))
for i := 0; i < v.Len(); i++ {
found := (len(indices) == 0) // If no indices are provided, we dirty everything
for _, index := range indices {
if index == i {
found = true
break
}
}
if !found {
continue
}
vi := reflect.Indirect(v.Index(i))
if vi.Kind() == reflect.Interface {
vi = reflect.Indirect(vi.Elem())
}
vi.FieldByName("String").SetString("dirty")
}
}
func validateInputVariety(t *testing.T, g *Goon, srcType, dstType, mode int, txn bool) {
if mode >= ivModeTotal {
t.Fatalf("Invalid input variety mode! %v >= %v", mode, ivModeTotal)
return
}
// Generate a nice debug info string for clear logging
prettyInfo := getPrettyIVType(srcType) + " " + getPrettyIVType(dstType) + " " + getPrettyIVMode(mode)
if txn {
prettyInfo += " TXN"
}
// Generate test data with the specified types
src := getInputVarietyItem(t, g, srcType, false, 0, 1, 2)
ref := getInputVarietyItem(t, g, dstType, false, 0, 1, 2)
dstA := getInputVarietyItem(t, g, dstType, true, 0, 1, 2)
dstB := getInputVarietyItem(t, g, dstType, true, 0, 1, 2)
dstC := getInputVarietyItem(t, g, dstType, true, 0, 1, 2)
setPLSCounts(ref, isIVTypePLS(srcType), isIVTypePLS(dstType))
// Save our test data
if txn {
if err := g.RunInTransaction(func(tg *Goon) error {
_, err := tg.PutMulti(src)
return err
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Fatalf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
} else {
if _, err := g.PutMulti(src); err != nil {
t.Fatalf("%s > Unexpected error on PutMulti - %v", prettyInfo, err)
}
}
// Attempt an immediate get, which should catch any faulty Put-based caching
ivGetMulti(t, g, ref, dstA, prettyInfo+" PC")
// Clear the caches, as we're going to precisely set the caches via loadIVItem
// TODO: Instead of clear, fill the caches with invalid data
g.FlushLocalCache()
memcache.Flush(g.Context)
// This function just populates the cache via GetMulti
loadIVItem := func(indices ...int) {
dst := getInputVarietyItem(t, g, dstType, true, indices...)
if err := g.GetMulti(dst); err != nil {
t.Fatalf("%s > Unexpected error on GetMulti - %v", prettyInfo, err)
}
makeDirty(dst) // Make these dirty to confirm the cache doesn't reflect it
}
// Set the caches into proper state based on given mode
switch mode {
case ivModeDatastore:
// Caches already clear
case ivModeMemcache:
loadIVItem(0, 1, 2) // Left in memcache
g.FlushLocalCache()
case ivModeMemcacheAndDatastore:
loadIVItem(0, 1) // Left in memcache
g.FlushLocalCache()
case ivModeLocalcache:
loadIVItem(0, 1, 2) // Left in local cache
case ivModeLocalcacheAndMemcache:
loadIVItem(0) // Left in memcache
g.FlushLocalCache()
loadIVItem(1, 2) // Left in local cache
case ivModeLocalcacheAndDatastore:
loadIVItem(0, 1) // Left in local cache
case ivModeLocalcacheAndMemcacheAndDatastore:
loadIVItem(0) // Left in memcache
g.FlushLocalCache()
loadIVItem(1) // Left in local cache
}
// Get our data back and make sure it's correct
if txn {
if err := g.RunInTransaction(func(tg *Goon) error {
return ivGetMulti(t, tg, ref, dstB, prettyInfo+" GC")
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Fatalf("%s > Unexpected error on transaction - %v", prettyInfo, err)
}
} else {
ivGetMulti(t, g, ref, dstB, prettyInfo+" GC")
}
// Delete our data
if txn {
if err := g.RunInTransaction(func(tg *Goon) error {
return tg.DeleteMulti(ivItemKeys)
}, &datastore.TransactionOptions{XG: true}); err != nil {
t.Fatalf("%s > Unexpected error on DeleteMulti - %v", prettyInfo, err)
}
} else {
if err := g.DeleteMulti(ivItemKeys); err != nil {
t.Fatalf("%s > Unexpected error on DeleteMulti - %v", prettyInfo, err)
}
}