github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/batch_get_item-livetest.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/smugmug/godynamo/conf" 7 "github.com/smugmug/godynamo/conf_file" 8 conf_iam "github.com/smugmug/godynamo/conf_iam" 9 batch_get_item "github.com/smugmug/godynamo/endpoints/batch_get_item" 10 keepalive "github.com/smugmug/godynamo/keepalive" 11 "github.com/smugmug/godynamo/types/attributevalue" 12 "github.com/smugmug/godynamo/types/item" 13 "log" 14 "net/http" 15 ) 16 17 // this tests "RetryBatchGet", which does NOT do intelligent splitting and re-assembling 18 // of requests and responses 19 func Test1() { 20 b := batch_get_item.NewBatchGetItem() 21 tn := "test-godynamo-livetest" 22 b.RequestItems[tn] = batch_get_item.NewRequestInstance() 23 for i := 1; i <= 200; i++ { 24 item := item.NewItem() 25 k := fmt.Sprintf("AHashKey%d", i) 26 v := fmt.Sprintf("%d", i) 27 item["TheHashKey"] = &attributevalue.AttributeValue{S: k} 28 item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} 29 b.RequestItems[tn].Keys = 30 append(b.RequestItems[tn].Keys, item) 31 32 } 33 _, jerr := json.Marshal(b) 34 if jerr != nil { 35 fmt.Printf("%v\n", jerr) 36 } else { 37 //fmt.Printf("%s\n",string(json)) 38 } 39 bs, _ := batch_get_item.Split(b) 40 for _, bsi := range bs { 41 body, code, err := bsi.RetryBatchGet(0) 42 if err != nil || code != http.StatusOK { 43 fmt.Printf("error: %v\n%v\n%v\n", string(body), code, err) 44 } else { 45 fmt.Printf("worked!: %v\n%v\n%v\n", string(body), code, err) 46 } 47 } 48 } 49 50 // this tests "DoBatchGet", which breaks up requests that are larger than the limit 51 // and re-assembles responses 52 func Test2() { 53 b := batch_get_item.NewBatchGetItem() 54 tn := "test-godynamo-livetest" 55 b.RequestItems[tn] = batch_get_item.NewRequestInstance() 56 for i := 1; i <= 300; i++ { 57 item := item.NewItem() 58 k := fmt.Sprintf("AHashKey%d", i) 59 v := fmt.Sprintf("%d", i) 60 item["TheHashKey"] = &attributevalue.AttributeValue{S: k} 61 item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} 62 b.RequestItems[tn].Keys = 63 append(b.RequestItems[tn].Keys, item) 64 65 } 66 _, jerr := json.Marshal(*b) 67 if jerr != nil { 68 fmt.Printf("%v\n", jerr) 69 } else { 70 //fmt.Printf("%s\n",string(json)) 71 } 72 body, code, err := b.DoBatchGet() 73 fmt.Printf("%v\n%v\n%v\n", string(body), code, err) 74 } 75 76 func main() { 77 conf_file.Read() 78 conf.Vals.ConfLock.RLock() 79 if conf.Vals.Initialized == false { 80 panic("the conf.Vals global conf struct has not been initialized") 81 } 82 83 // launch a background poller to keep conns to aws alive 84 if conf.Vals.Network.DynamoDB.KeepAlive { 85 log.Printf("launching background keepalive") 86 go keepalive.KeepAlive([]string{}) 87 } 88 89 // deal with iam, or not 90 if conf.Vals.UseIAM { 91 iam_ready_chan := make(chan bool) 92 go conf_iam.GoIAM(iam_ready_chan) 93 _ = <-iam_ready_chan 94 } 95 conf.Vals.ConfLock.RUnlock() 96 97 Test1() 98 Test2() 99 }