github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/batch_get_item-param-conf-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  	batch_get_item "github.com/smugmug/godynamo/endpoints/batch_get_item"
     9  	"github.com/smugmug/godynamo/types/attributevalue"
    10  	"github.com/smugmug/godynamo/types/item"
    11  	"net/http"
    12  	"os"
    13  )
    14  
    15  // this tests "RetryBatchGet", which does NOT do intelligent splitting and re-assembling
    16  // of requests and responses
    17  func Test1() {
    18  	home := os.Getenv("HOME")
    19  	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
    20  	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
    21  	if home_conf_err != nil {
    22  		panic("cannot read conf from " + home_conf_file)
    23  	}
    24  	home_conf.ConfLock.RLock()
    25  	if home_conf.Initialized == false {
    26  		panic("conf struct has not been initialized")
    27  	}
    28  
    29  	b := batch_get_item.NewBatchGetItem()
    30  	tn := "test-godynamo-livetest"
    31  	b.RequestItems[tn] = batch_get_item.NewRequestInstance()
    32  	for i := 1; i <= 200; i++ {
    33  		item := item.NewItem()
    34  		k := fmt.Sprintf("AHashKey%d", i)
    35  		v := fmt.Sprintf("%d", i)
    36  		item["TheHashKey"] = &attributevalue.AttributeValue{S: k}
    37  		item["TheRangeKey"] = &attributevalue.AttributeValue{N: v}
    38  		b.RequestItems[tn].Keys =
    39  			append(b.RequestItems[tn].Keys, item)
    40  
    41  	}
    42  	_, jerr := json.Marshal(b)
    43  	if jerr != nil {
    44  		fmt.Printf("%v\n", jerr)
    45  	} else {
    46  		//fmt.Printf("%s\n",string(json))
    47  	}
    48  	bs, _ := batch_get_item.Split(b)
    49  	for _, bsi := range bs {
    50  		body, code, err := bsi.RetryBatchGetWithConf(0, home_conf)
    51  		if err != nil || code != http.StatusOK {
    52  			fmt.Printf("error: %v\n%v\n%v\n", string(body), code, err)
    53  		} else {
    54  			fmt.Printf("worked!: %v\n%v\n%v\n", string(body), code, err)
    55  		}
    56  	}
    57  }
    58  
    59  // this tests "DoBatchGet", which breaks up requests that are larger than the limit
    60  // and re-assembles responses
    61  func Test2() {
    62  	home := os.Getenv("HOME")
    63  	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
    64  	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
    65  	if home_conf_err != nil {
    66  		panic("cannot read conf from " + home_conf_file)
    67  	}
    68  	home_conf.ConfLock.RLock()
    69  	if home_conf.Initialized == false {
    70  		panic("conf struct has not been initialized")
    71  	}
    72  
    73  	b := batch_get_item.NewBatchGetItem()
    74  	tn := "test-godynamo-livetest"
    75  	b.RequestItems[tn] = batch_get_item.NewRequestInstance()
    76  	for i := 1; i <= 300; i++ {
    77  		item := item.NewItem()
    78  		k := fmt.Sprintf("AHashKey%d", i)
    79  		v := fmt.Sprintf("%d", i)
    80  		item["TheHashKey"] = &attributevalue.AttributeValue{S: k}
    81  		item["TheRangeKey"] = &attributevalue.AttributeValue{N: v}
    82  		b.RequestItems[tn].Keys =
    83  			append(b.RequestItems[tn].Keys, item)
    84  
    85  	}
    86  	_, jerr := json.Marshal(*b)
    87  	if jerr != nil {
    88  		fmt.Printf("%v\n", jerr)
    89  	} else {
    90  		//fmt.Printf("%s\n",string(json))
    91  	}
    92  	body, code, err := b.DoBatchGetWithConf(home_conf)
    93  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    94  }
    95  
    96  func main() {
    97  	Test1()
    98  	Test2()
    99  }