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