github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/batch_write_item-param-conf-livetest.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/smugmug/godynamo/conf"
     6  	"github.com/smugmug/godynamo/conf_file"
     7  	batch_write_item "github.com/smugmug/godynamo/endpoints/batch_write_item"
     8  	"github.com/smugmug/godynamo/types/attributevalue"
     9  	"github.com/smugmug/godynamo/types/item"
    10  	"net/http"
    11  	"os"
    12  )
    13  
    14  // these tests are just like batch_write_item-livestest except they use a parameterized conf
    15  
    16  // this tests "RetryBatchWrite", which does NOT do intelligent splitting and re-assembling
    17  // of requests and responses
    18  func Test1() {
    19  	home := os.Getenv("HOME")
    20  	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
    21  	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
    22  	if home_conf_err != nil {
    23  		panic("cannot read conf from " + home_conf_file)
    24  	}
    25  	home_conf.ConfLock.RLock()
    26  	if home_conf.Initialized == false {
    27  		panic("conf struct has not been initialized")
    28  	}
    29  
    30  	tn := "test-godynamo-livetest"
    31  	b := batch_write_item.NewBatchWriteItem()
    32  	b.RequestItems[tn] = make([]batch_write_item.RequestInstance, 0)
    33  	for i := 1; i <= 300; i++ {
    34  		var p batch_write_item.PutRequest
    35  		p.Item = item.NewItem()
    36  		k := fmt.Sprintf("AHashKey%d", i)
    37  		v := fmt.Sprintf("%d", i)
    38  		p.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k}
    39  		p.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v}
    40  		p.Item["SomeValue"] = &attributevalue.AttributeValue{N: v}
    41  		b.RequestItems[tn] =
    42  			append(b.RequestItems[tn],
    43  				batch_write_item.RequestInstance{PutRequest: &p})
    44  	}
    45  	bs, _ := batch_write_item.Split(b)
    46  	for _, bsi := range bs {
    47  		body, code, err := bsi.RetryBatchWriteWithConf(0, home_conf)
    48  		if err != nil || code != http.StatusOK {
    49  			fmt.Printf("error: %v\n%v\n%v\n", string(body), code, err)
    50  		} else {
    51  			fmt.Printf("worked!: %v\n%v\n%v\n", string(body), code, err)
    52  		}
    53  	}
    54  }
    55  
    56  // this tests "DoBatchWrite", which breaks up requests that are larger than the limit
    57  // and re-assembles responses
    58  func Test2() {
    59  	home := os.Getenv("HOME")
    60  	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
    61  	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
    62  	if home_conf_err != nil {
    63  		panic("cannot read conf from " + home_conf_file)
    64  	}
    65  	home_conf.ConfLock.RLock()
    66  	if home_conf.Initialized == false {
    67  		panic("conf struct has not been initialized")
    68  	}
    69  
    70  	b := batch_write_item.NewBatchWriteItem()
    71  	tn := "test-godynamo-livetest"
    72  	b.RequestItems[tn] = make([]batch_write_item.RequestInstance, 0)
    73  	for i := 201; i <= 300; i++ {
    74  		var p batch_write_item.PutRequest
    75  		p.Item = item.NewItem()
    76  		k := fmt.Sprintf("AHashKey%d", i)
    77  		v := fmt.Sprintf("%d", i)
    78  		p.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k}
    79  		p.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v}
    80  		p.Item["SomeValue"] = &attributevalue.AttributeValue{N: v}
    81  		b.RequestItems[tn] =
    82  			append(b.RequestItems[tn],
    83  				batch_write_item.RequestInstance{PutRequest: &p})
    84  	}
    85  	body, code, err := b.DoBatchWriteWithConf(home_conf)
    86  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    87  }
    88  
    89  func main() {
    90  	Test1()
    91  	Test2()
    92  }