How to use Batch method of http Package

Best K6 code snippet using http.Batch

notifier_test.go

Source:notifier_test.go Github

copy

Full Screen

...62 for _, c := range cases {63 require.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1))64 }65}66func TestHandlerNextBatch(t *testing.T) {67 h := NewManager(&Options{}, nil)68 for i := range make([]struct{}, 2*maxBatchSize+1) {69 h.queue = append(h.queue, &Alert{70 Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),71 })72 }73 expected := append([]*Alert{}, h.queue...)74 require.NoError(t, alertsEqual(expected[0:maxBatchSize], h.nextBatch()))75 require.NoError(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], h.nextBatch()))76 require.NoError(t, alertsEqual(expected[2*maxBatchSize:], h.nextBatch()))77 require.Equal(t, 0, len(h.queue), "Expected queue to be empty but got %d alerts", len(h.queue))78}79func alertsEqual(a, b []*Alert) error {80 if len(a) != len(b) {81 return errors.Errorf("length mismatch: %v != %v", a, b)82 }83 for i, alert := range a {84 if !labels.Equal(alert.Labels, b[i].Labels) {85 return errors.Errorf("label mismatch at index %d: %s != %s", i, alert.Labels, b[i].Labels)86 }87 }88 return nil89}90func TestHandlerSendAll(t *testing.T) {91 var (92 errc = make(chan error, 1)93 expected = make([]*Alert, 0, maxBatchSize)94 status1, status2 atomic.Int3295 )96 status1.Store(int32(http.StatusOK))97 status2.Store(int32(http.StatusOK))98 newHTTPServer := func(u, p string, status *atomic.Int32) *httptest.Server {99 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {100 var err error101 defer func() {102 if err == nil {103 return104 }105 select {106 case errc <- err:107 default:108 }109 }()110 user, pass, _ := r.BasicAuth()111 if user != u || pass != p {112 err = errors.Errorf("unexpected user/password: %s/%s != %s/%s", user, pass, u, p)113 w.WriteHeader(http.StatusInternalServerError)114 return115 }116 var alerts []*Alert117 err = json.NewDecoder(r.Body).Decode(&alerts)118 if err == nil {119 err = alertsEqual(expected, alerts)120 }121 w.WriteHeader(int(status.Load()))122 }))123 }124 server1 := newHTTPServer("prometheus", "testing_password", &status1)125 server2 := newHTTPServer("", "", &status2)126 defer server1.Close()127 defer server2.Close()128 h := NewManager(&Options{}, nil)129 authClient, _ := config_util.NewClientFromConfig(130 config_util.HTTPClientConfig{131 BasicAuth: &config_util.BasicAuth{132 Username: "prometheus",133 Password: "testing_password",134 },135 }, "auth_alertmanager", false, false)136 h.alertmanagers = make(map[string]*alertmanagerSet)137 am1Cfg := config.DefaultAlertmanagerConfig138 am1Cfg.Timeout = model.Duration(time.Second)139 am2Cfg := config.DefaultAlertmanagerConfig140 am2Cfg.Timeout = model.Duration(time.Second)141 h.alertmanagers["1"] = &alertmanagerSet{142 ams: []alertmanager{143 alertmanagerMock{144 urlf: func() string { return server1.URL },145 },146 },147 cfg: &am1Cfg,148 client: authClient,149 }150 h.alertmanagers["2"] = &alertmanagerSet{151 ams: []alertmanager{152 alertmanagerMock{153 urlf: func() string { return server2.URL },154 },155 },156 cfg: &am2Cfg,157 }158 for i := range make([]struct{}, maxBatchSize) {159 h.queue = append(h.queue, &Alert{160 Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),161 })162 expected = append(expected, &Alert{163 Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),164 })165 }166 checkNoErr := func() {167 t.Helper()168 select {169 case err := <-errc:170 require.NoError(t, err)171 default:172 }173 }174 require.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly")175 checkNoErr()176 status1.Store(int32(http.StatusNotFound))177 require.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly")178 checkNoErr()179 status2.Store(int32(http.StatusInternalServerError))180 require.False(t, h.sendAll(h.queue...), "all sends succeeded unexpectedly")181 checkNoErr()182}183func TestCustomDo(t *testing.T) {184 const testURL = "http://testurl.com/"185 const testBody = "testbody"186 var received bool187 h := NewManager(&Options{188 Do: func(_ context.Context, client *http.Client, req *http.Request) (*http.Response, error) {189 received = true190 body, err := ioutil.ReadAll(req.Body)191 require.NoError(t, err)192 require.Equal(t, testBody, string(body))193 require.Equal(t, testURL, req.URL.String())194 return &http.Response{195 Body: ioutil.NopCloser(bytes.NewBuffer(nil)),196 }, nil197 },198 }, nil)199 h.sendOne(context.Background(), nil, testURL, []byte(testBody))200 require.True(t, received, "Expected to receive an alert, but didn't")201}202func TestExternalLabels(t *testing.T) {203 h := NewManager(&Options{204 QueueCapacity: 3 * maxBatchSize,205 ExternalLabels: labels.Labels{{Name: "a", Value: "b"}},206 RelabelConfigs: []*relabel.Config{207 {208 SourceLabels: model.LabelNames{"alertname"},209 TargetLabel: "a",210 Action: "replace",211 Regex: relabel.MustNewRegexp("externalrelabelthis"),212 Replacement: "c",213 },214 },215 }, nil)216 // This alert should get the external label attached.217 h.Send(&Alert{218 Labels: labels.FromStrings("alertname", "test"),219 })220 // This alert should get the external label attached, but then set to "c"221 // through relabelling.222 h.Send(&Alert{223 Labels: labels.FromStrings("alertname", "externalrelabelthis"),224 })225 expected := []*Alert{226 {Labels: labels.FromStrings("alertname", "test", "a", "b")},227 {Labels: labels.FromStrings("alertname", "externalrelabelthis", "a", "c")},228 }229 require.NoError(t, alertsEqual(expected, h.queue))230}231func TestHandlerRelabel(t *testing.T) {232 h := NewManager(&Options{233 QueueCapacity: 3 * maxBatchSize,234 RelabelConfigs: []*relabel.Config{235 {236 SourceLabels: model.LabelNames{"alertname"},237 Action: "drop",238 Regex: relabel.MustNewRegexp("drop"),239 },240 {241 SourceLabels: model.LabelNames{"alertname"},242 TargetLabel: "alertname",243 Action: "replace",244 Regex: relabel.MustNewRegexp("rename"),245 Replacement: "renamed",246 },247 },248 }, nil)249 // This alert should be dropped due to the configuration250 h.Send(&Alert{251 Labels: labels.FromStrings("alertname", "drop"),252 })253 // This alert should be replaced due to the configuration254 h.Send(&Alert{255 Labels: labels.FromStrings("alertname", "rename"),256 })257 expected := []*Alert{258 {Labels: labels.FromStrings("alertname", "renamed")},259 }260 require.NoError(t, alertsEqual(expected, h.queue))261}262func TestHandlerQueuing(t *testing.T) {263 var (264 expectedc = make(chan []*Alert)265 called = make(chan struct{})266 done = make(chan struct{})267 errc = make(chan error, 1)268 )269 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {270 // Notify the test function that we have received something.271 select {272 case called <- struct{}{}:273 case <-done:274 return275 }276 // Wait for the test function to unblock us.277 select {278 case expected := <-expectedc:279 var alerts []*Alert280 err := json.NewDecoder(r.Body).Decode(&alerts)281 if err == nil {282 err = alertsEqual(expected, alerts)283 }284 select {285 case errc <- err:286 default:287 }288 case <-done:289 }290 }))291 defer func() {292 close(done)293 server.Close()294 }()295 h := NewManager(296 &Options{297 QueueCapacity: 3 * maxBatchSize,298 },299 nil,300 )301 h.alertmanagers = make(map[string]*alertmanagerSet)302 am1Cfg := config.DefaultAlertmanagerConfig303 am1Cfg.Timeout = model.Duration(time.Second)304 h.alertmanagers["1"] = &alertmanagerSet{305 ams: []alertmanager{306 alertmanagerMock{307 urlf: func() string { return server.URL },308 },309 },310 cfg: &am1Cfg,311 }312 go h.Run(nil)313 defer h.Stop()314 var alerts []*Alert315 for i := range make([]struct{}, 20*maxBatchSize) {316 alerts = append(alerts, &Alert{317 Labels: labels.FromStrings("alertname", fmt.Sprintf("%d", i)),318 })319 }320 assertAlerts := func(expected []*Alert) {321 t.Helper()322 for {323 select {324 case <-called:325 expectedc <- expected326 case err := <-errc:327 require.NoError(t, err)328 return329 case <-time.After(5 * time.Second):330 t.Fatalf("Alerts were not pushed")331 }332 }333 }334 // If the batch is larger than the queue capacity, it should be truncated335 // from the front.336 h.Send(alerts[:4*maxBatchSize]...)337 for i := 1; i < 4; i++ {338 assertAlerts(alerts[i*maxBatchSize : (i+1)*maxBatchSize])339 }340 // Send one batch, wait for it to arrive and block the server so the queue fills up.341 h.Send(alerts[:maxBatchSize]...)342 <-called343 // Send several batches while the server is still blocked so the queue344 // fills up to its maximum capacity (3*maxBatchSize). Then check that the345 // queue is truncated in the front.346 h.Send(alerts[1*maxBatchSize : 2*maxBatchSize]...) // this batch should be dropped.347 h.Send(alerts[2*maxBatchSize : 3*maxBatchSize]...)348 h.Send(alerts[3*maxBatchSize : 4*maxBatchSize]...)349 // Send the batch that drops the first one.350 h.Send(alerts[4*maxBatchSize : 5*maxBatchSize]...)351 // Unblock the server.352 expectedc <- alerts[:maxBatchSize]353 select {354 case err := <-errc:355 require.NoError(t, err)356 case <-time.After(5 * time.Second):357 t.Fatalf("Alerts were not pushed")358 }359 // Verify that we receive the last 3 batches.360 for i := 2; i < 5; i++ {361 assertAlerts(alerts[i*maxBatchSize : (i+1)*maxBatchSize])362 }363}364type alertmanagerMock struct {365 urlf func() string366}367func (a alertmanagerMock) url() *url.URL {368 u, err := url.Parse(a.urlf())369 if err != nil {370 panic(err)371 }372 return u373}374func TestLabelSetNotReused(t *testing.T) {375 tg := makeInputTargetGroup()...

Full Screen

Full Screen

batch.go

Source:batch.go Github

copy

Full Screen

...26 "github.com/WolffunGame/experiment-agent/config"27 "github.com/go-chi/render"28 "golang.org/x/sync/errgroup"29)30// BatchResponse has the structure for the final response31type BatchResponse struct {32 StartedAt time.Time `json:"startedAt"`33 EndedAt time.Time `json:"endedAt"`34 ErrorCount int `json:"errorCount"`35 ResponseItems []ResponseCollector `json:"response"`36 lock sync.Mutex37}38// NewBatchResponse constructs a BatchResponse with default values39func NewBatchResponse() *BatchResponse {40 return &BatchResponse{41 StartedAt: time.Now(),42 ResponseItems: make([]ResponseCollector, 0),43 }44}45func (br *BatchResponse) append(col ResponseCollector) {46 br.lock.Lock()47 defer br.lock.Unlock()48 if col.Status != http.StatusOK {49 br.ErrorCount++50 }51 br.ResponseItems = append(br.ResponseItems, col)52 br.EndedAt = time.Now()53}54// ResponseCollector collects responses for the writer55type ResponseCollector struct {56 Status int `json:"status"`57 RequestID string `json:"requestID"`58 OperationID string `json:"operationID"`59 Method string `json:"method"`60 URL string `json:"url"`61 Body interface{} `json:"body"`62 StartedAt time.Time `json:"startedAt"`63 EndedAt time.Time `json:"endedAt"`64 headerMap http.Header65}66// NewResponseCollector constructs a ResponseCollector with default values67func NewResponseCollector(op BatchOperation) ResponseCollector {68 return ResponseCollector{69 headerMap: make(http.Header),70 Method: op.Method,71 URL: op.URL,72 OperationID: op.OperationID,73 StartedAt: time.Now(),74 Status: http.StatusOK,75 }76}77// WriteHeader sets the status code78func (rec *ResponseCollector) WriteHeader(code int) {79 rec.Status = code80}81// Write is just the collector for BatchResponse82func (rec *ResponseCollector) Write(b []byte) (int, error) {83 var data interface{}84 if err := json.Unmarshal(b, &data); err != nil {85 return 0, err86 }87 rec.Body = data88 if header, ok := rec.headerMap["X-Request-Id"]; ok {89 rec.RequestID = header[0]90 }91 return 0, nil92}93// Header returns header map94func (rec *ResponseCollector) Header() http.Header {95 return rec.headerMap96}97// BatchOperation defines a single request within a batch98type BatchOperation struct {99 Method string `json:"method"`100 URL string `json:"url"`101 OperationID string `json:"operationID"`102 Body map[string]interface{} `json:"body"`103 Params map[string]string `json:"params"`104 Headers map[string]string `json:"headers"`105}106// BatchRequest is the original request that is used for batching107type BatchRequest struct {108 Operations []BatchOperation `json:"operations"`109}110func makeRequest(next http.Handler, r *http.Request, op BatchOperation) ResponseCollector {111 col := NewResponseCollector(op)112 bytesBody, err := json.Marshal(op.Body)113 if err != nil {114 col.Status = http.StatusNotFound115 GetLogger(r).Error().Err(err).Msg("cannot convert operation body to bytes for operation id: " + op.OperationID)116 return col117 }118 reader := bytes.NewReader(bytesBody)119 opReq, err := http.NewRequest(op.Method, op.URL, reader)120 if err != nil {121 col.Status = http.StatusBadRequest122 GetLogger(r).Error().Err(err).Msg("cannot make a new request for operation id: " + op.OperationID)123 return col124 }125 for headerKey, headerValue := range op.Headers {126 opReq.Header.Add(headerKey, headerValue)127 col.headerMap[headerKey] = []string{headerValue}128 }129 for paramKey, paramValue := range op.Params {130 values := opReq.URL.Query()131 values.Add(paramKey, paramValue)132 opReq.URL.RawQuery = values.Encode()133 }134 next.ServeHTTP(&col, opReq)135 col.EndedAt = time.Now()136 return col137}138// BatchRouter intercepts requests for the given url to return a StatusOK.139func BatchRouter(batchRequests config.BatchRequestsConfig) func(http.Handler) http.Handler {140 f := func(next http.Handler) http.Handler {141 fn := func(w http.ResponseWriter, r *http.Request) {142 if r.Method == "POST" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/batch") {143 decoder := json.NewDecoder(r.Body)144 var req BatchRequest145 err := decoder.Decode(&req)146 if err != nil {147 http.Error(w, `{"error": "cannot decode the operation body"}`, http.StatusBadRequest)148 return149 }150 if len(req.Operations) > batchRequests.OperationsLimit {151 http.Error(w, fmt.Sprintf(`{"error": "too many operations, exceeding %d operations"}`, batchRequests.OperationsLimit), http.StatusUnprocessableEntity)152 return153 }154 batchRes := NewBatchResponse()155 var eg, ctx = errgroup.WithContext(r.Context())156 ch := make(chan struct{}, batchRequests.MaxConcurrency)157 for _, op := range req.Operations {158 op := op159 ch <- struct{}{}160 eg.Go(func() error {161 defer func() { <-ch }()162 select {163 case <-ctx.Done():164 GetLogger(r).Error().Err(ctx.Err()).Msg("terminating request")165 return ctx.Err()166 default:167 col := makeRequest(next, r, op)168 // Append response item...

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("URL:>", url)4 var jsonStr = []byte(`[{"id":1,"name":"test"},{"id":2,"name":"test2"}]`)5 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))6 req.Header.Set("Content-Type", "application/json")7 client := &http.Client{}8 resp, err := client.Do(req)9 if err != nil {10 panic(err)11 }12 defer resp.Body.Close()13 fmt.Println("response Status:", resp.Status)14 fmt.Println("response Headers:", resp.Header)15 body, _ := ioutil.ReadAll(resp.Body)16 fmt.Println("response Body:", string(body))17}18import (19func main() {20 fmt.Println("URL:>", url)21 var jsonStr = []byte(`[{"id":1,"name":"test"},{"id":2,"name":"test2"}]`)22 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))23 req.Header.Set("Content-Type", "application/json")24 client := &http.Client{}25 resp, err := client.Do(req)26 if err != nil {27 panic(err)28 }29 defer resp.Body.Close()30 fmt.Println("response Status:", resp.Status)31 fmt.Println("response Headers:", resp.Header)32 body, _ := ioutil.ReadAll(resp.Body)33 fmt.Println("response Body:", string(body))34}35import (36func main() {37 fmt.Println("URL:>", url)38 var jsonStr = []byte(`[{"id":1,"name":"test"},{"id":2,"name":"test2"}]`)39 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))40 req.Header.Set("Content-Type", "application/json")41 client := &http.Client{}42 resp, err := client.Do(req)43 if err != nil {44 panic(err)45 }46 defer resp.Body.Close()

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 panic(err)5 }6 batch := new(http.Batch)7 batch.Add(req)8 res, err := batch.Do()9 if err != nil {10 panic(err)11 }12 fmt.Println(res)13}14Content-Type: text/html; charset=ISO-8859-115X-XSS-Protection: 1; mode=block

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 reqs := []*http.Request{req1, req2, req3, req4, req5}4 client := &http.Client{}5 resps, _ := client.Do(reqs...)6 for _, resp := range resps {7 fmt.Println(resp.Status)8 }9}

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1func main() {2 http.HandleFunc("/", handler)3 log.Fatal(http.ListenAndServe(":8080", nil))4}5func handler(w http.ResponseWriter, r *http.Request) {6 w.Header().Set("Content-Type", "text/html; charset=utf-8")

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 requests := make([]*http.Request, 0)4 req, err := http.NewRequest("GET", url, nil)5 if err != nil {6 log.Fatal(err)7 }8 requests = append(requests, req)9 }10 client := &http.Client{}11 responses, err := client.Do(requests[0])12 if err != nil {13 log.Fatal(err)14 }15 defer responses.Body.Close()16 for _, resp := range responses {17 body, err := ioutil.ReadAll(resp.Body)18 if err != nil {19 log.Fatal(err)20 }21 fmt.Printf("%s22 }23}24import (25func main() {26 requests := make([]*http.Request, 0)

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 batch := &http.Batch{}7 batch.Add(req)8 responses := batch.Do()9 for _, response := range responses {10 body, _ := ioutil.ReadAll(response.Body)11 fmt.Println(string(body))12 }13}

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 reqs.Header.Add("Accept", "application/json")4 reqs.Header.Add("Content-Type", "application/json")5 reqs.Header.Add("X-My-Header", "myvalue")6 reqs.Header.Add("X-My-Header2", "myvalue2")7 reqs.Header.Add("X-My-Header3", "myvalue3")8 reqs.Header.Add("X-My-Header4", "myvalue4")9 reqs.Header.Add("X-My-Header5", "myvalue5")10 reqs.Header.Add("X-My-Header6", "myvalue6")11 reqs.Header.Add("X-My-Header7", "myvalue7")12 reqs.Header.Add("X-My-Header8", "myvalue8")13 reqs.Header.Add("X-My-Header9", "myvalue9")14 reqs.Header.Add("X-My-Header10", "myvalue10")15 reqs.Header.Add("X-My-Header11", "myvalue11")16 reqs.Header.Add("X-My-Header12", "myvalue12")17 reqs.Header.Add("X-My-Header13", "myvalue13")18 reqs.Header.Add("X-My-Header14", "myvalue14")19 reqs.Header.Add("X-My-Header15", "myvalue15")20 reqs.Header.Add("X-My-Header16", "myvalue16")21 reqs.Header.Add("X-My-Header17", "myvalue17")22 reqs.Header.Add("X-My-Header18", "myvalue18")23 reqs.Header.Add("X-My-Header19", "myvalue19")24 reqs.Header.Add("X-My-Header20", "myvalue20")25 reqs.Header.Add("X-My-Header21", "myvalue21")26 reqs.Header.Add("X-My-Header22", "myvalue22")27 reqs.Header.Add("X-My-Header23", "myvalue23")28 reqs.Header.Add("X-My-Header24", "myvalue24")29 reqs.Header.Add("X-My-Header25", "myvalue25")

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 batch := []*http.Request{req1, req2, req3, req4}4 client := &http.Client{}5 responses, err := client.Batch(batch)6 if err != nil {7 fmt.Printf("Error: %v", err)8 }9 for _, response := range responses {10 defer response.Body.Close()11 body, err := ioutil.ReadAll(response.Body)12 if err != nil {13 fmt.Printf("Error: %v", err)14 }15 fmt.Printf("Response: %v", string(body))16 }17}

Full Screen

Full Screen

Batch

Using AI Code Generation

copy

Full Screen

1func main(){2 client := &http.Client{}3 if err != nil {4 log.Fatal(err)5 }6 batch := client.Batch()7 batch.Add(req)8 res, err := batch.Do()9 if err != nil {10 log.Fatal(err)11 }12 fmt.Println(res)13}14[&{200 OK 200 HTTP/1.1 1 1 map[Content-Type:[text/plain; charset=utf-8] Date:[Sat, 26 Dec 2020 10:21:48 GMT] Content-Length:[12]] 0xc0000a2f00 12 [] false false map[] 0xc0000a2f00 <nil>}]

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful