How to use NewTagMap method of lib Package

Best K6 code snippet using lib.NewTagMap

request_test.go

Source:request_test.go Github

copy

Full Screen

...105 state := &lib.State{106 Options: lib.Options{RunTags: &stats.SampleTags{}},107 Transport: http.DefaultTransport,108 Logger: logrus.New(),109 Tags: lib.NewTagMap(nil),110 }111 _, err = MakeRequest(ctx, state, preq)112 require.Error(t, err)113 require.Equal(t, err.Error(), "unknown compressionType CompressionType(13)")114 })115 t.Run("invalid upgrade response", func(t *testing.T) {116 t.Parallel()117 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {118 w.Header().Add("Connection", "Upgrade")119 w.Header().Add("Upgrade", "h2c")120 w.WriteHeader(http.StatusSwitchingProtocols)121 }))122 defer srv.Close()123 ctx, cancel := context.WithCancel(context.Background())124 defer cancel()125 logger := logrus.New()126 logger.Level = logrus.DebugLevel127 state := &lib.State{128 Options: lib.Options{RunTags: &stats.SampleTags{}},129 Transport: srv.Client().Transport,130 Logger: logger,131 Tags: lib.NewTagMap(nil),132 }133 req, _ := http.NewRequest("GET", srv.URL, nil)134 preq := &ParsedHTTPRequest{135 Req: req,136 URL: &URL{u: req.URL},137 Body: new(bytes.Buffer),138 Timeout: 10 * time.Second,139 }140 res, err := MakeRequest(ctx, state, preq)141 assert.Nil(t, res)142 assert.EqualError(t, err, "unsupported response status: 101 Switching Protocols")143 })144}145func TestResponseStatus(t *testing.T) {146 t.Parallel()147 t.Run("response status", func(t *testing.T) {148 t.Parallel()149 testCases := []struct {150 name string151 statusCode int152 statusCodeExpected int153 statusCodeStringExpected string154 }{155 {"status 200", 200, 200, "200 OK"},156 {"status 201", 201, 201, "201 Created"},157 {"status 202", 202, 202, "202 Accepted"},158 {"status 203", 203, 203, "203 Non-Authoritative Information"},159 {"status 204", 204, 204, "204 No Content"},160 {"status 205", 205, 205, "205 Reset Content"},161 }162 for _, tc := range testCases {163 tc := tc164 t.Run(tc.name, func(t *testing.T) {165 t.Parallel()166 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {167 w.WriteHeader(tc.statusCode)168 }))169 defer server.Close()170 logger := logrus.New()171 logger.Level = logrus.DebugLevel172 samples := make(chan<- stats.SampleContainer, 1)173 registry := metrics.NewRegistry()174 state := &lib.State{175 Options: lib.Options{RunTags: &stats.SampleTags{}},176 Transport: server.Client().Transport,177 Logger: logger,178 Samples: samples,179 BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),180 Tags: lib.NewTagMap(nil),181 }182 req, err := http.NewRequest("GET", server.URL, nil)183 require.NoError(t, err)184 preq := &ParsedHTTPRequest{185 Req: req,186 URL: &URL{u: req.URL},187 Body: new(bytes.Buffer),188 Timeout: 10 * time.Second,189 ResponseType: ResponseTypeNone,190 }191 ctx, cancel := context.WithCancel(context.Background())192 defer cancel()193 res, err := MakeRequest(ctx, state, preq)194 require.NoError(t, err)195 assert.Equal(t, tc.statusCodeExpected, res.Status)196 assert.Equal(t, tc.statusCodeStringExpected, res.StatusText)197 })198 }199 })200}201func TestURL(t *testing.T) {202 t.Parallel()203 t.Run("Clean", func(t *testing.T) {204 t.Parallel()205 testCases := []struct {206 url string207 expected string208 }{209 {"https://example.com/", "https://example.com/"},210 {"https://example.com/${}", "https://example.com/${}"},211 {"https://user@example.com/", "https://****@example.com/"},212 {"https://user:pass@example.com/", "https://****:****@example.com/"},213 {"https://user:pass@example.com/path?a=1&b=2", "https://****:****@example.com/path?a=1&b=2"},214 {"https://user:pass@example.com/${}/${}", "https://****:****@example.com/${}/${}"},215 {"@malformed/url", "@malformed/url"},216 {"not a url", "not a url"},217 }218 for _, tc := range testCases {219 tc := tc220 t.Run(tc.url, func(t *testing.T) {221 t.Parallel()222 u, err := url.Parse(tc.url)223 require.NoError(t, err)224 ut := URL{u: u, URL: tc.url}225 require.Equal(t, tc.expected, ut.Clean())226 })227 }228 })229}230func TestMakeRequestTimeoutInTheMiddle(t *testing.T) {231 t.Parallel()232 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {233 w.Header().Add("Content-Length", "100000")234 w.WriteHeader(200)235 if f, ok := w.(http.Flusher); ok {236 f.Flush()237 }238 time.Sleep(100 * time.Millisecond)239 }))240 defer srv.Close()241 ctx, cancel := context.WithCancel(context.Background())242 defer cancel()243 samples := make(chan stats.SampleContainer, 10)244 logger := logrus.New()245 logger.Level = logrus.DebugLevel246 registry := metrics.NewRegistry()247 state := &lib.State{248 Options: lib.Options{249 RunTags: &stats.SampleTags{},250 SystemTags: &stats.DefaultSystemTagSet,251 },252 Transport: srv.Client().Transport,253 Samples: samples,254 Logger: logger,255 BPool: bpool.NewBufferPool(100),256 BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),257 Tags: lib.NewTagMap(nil),258 }259 req, _ := http.NewRequest("GET", srv.URL, nil)260 preq := &ParsedHTTPRequest{261 Req: req,262 URL: &URL{u: req.URL, URL: srv.URL},263 Body: new(bytes.Buffer),264 Timeout: 50 * time.Millisecond,265 ResponseCallback: func(i int) bool { return i == 0 },266 }267 res, err := MakeRequest(ctx, state, preq)268 require.NoError(t, err)269 assert.NotNil(t, res)270 assert.Len(t, samples, 1)271 sampleCont := <-samples272 allSamples := sampleCont.GetSamples()273 require.Len(t, allSamples, 9)274 expTags := map[string]string{275 "error": "request timeout",276 "error_code": "1050",277 "status": "0",278 "expected_response": "true", // we wait for status code 0279 "method": "GET",280 "url": srv.URL,281 "name": srv.URL,282 }283 for _, s := range allSamples {284 assert.Equal(t, expTags, s.Tags.CloneTags())285 }286}287func BenchmarkWrapDecompressionError(b *testing.B) {288 err := errors.New("error")289 b.ResetTimer()290 b.ReportAllocs()291 for i := 0; i < b.N; i++ {292 _ = wrapDecompressionError(err)293 }294}295func TestTrailFailed(t *testing.T) {296 t.Parallel()297 srv := httptest.NewTLSServer(httpbin.New().Handler())298 t.Cleanup(srv.Close)299 testCases := map[string]struct {300 responseCallback func(int) bool301 failed null.Bool302 }{303 "null responsecallback": {responseCallback: nil, failed: null.NewBool(false, false)},304 "unexpected response": {responseCallback: func(int) bool { return false }, failed: null.NewBool(true, true)},305 "expected response": {responseCallback: func(int) bool { return true }, failed: null.NewBool(false, true)},306 }307 for name, testCase := range testCases {308 responseCallback := testCase.responseCallback309 failed := testCase.failed310 t.Run(name, func(t *testing.T) {311 t.Parallel()312 ctx, cancel := context.WithCancel(context.Background())313 t.Cleanup(cancel)314 samples := make(chan stats.SampleContainer, 10)315 logger := logrus.New()316 logger.Level = logrus.DebugLevel317 registry := metrics.NewRegistry()318 state := &lib.State{319 Options: lib.Options{320 RunTags: &stats.SampleTags{},321 SystemTags: &stats.DefaultSystemTagSet,322 },323 Transport: srv.Client().Transport,324 Samples: samples,325 Logger: logger,326 BPool: bpool.NewBufferPool(2),327 BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),328 Tags: lib.NewTagMap(nil),329 }330 req, _ := http.NewRequest("GET", srv.URL, nil)331 preq := &ParsedHTTPRequest{332 Req: req,333 URL: &URL{u: req.URL, URL: srv.URL},334 Body: new(bytes.Buffer),335 Timeout: 10 * time.Millisecond,336 ResponseCallback: responseCallback,337 }338 res, err := MakeRequest(ctx, state, preq)339 require.NoError(t, err)340 require.NotNil(t, res)341 require.Len(t, samples, 1)342 sample := <-samples343 trail := sample.(*Trail)344 require.Equal(t, failed, trail.Failed)345 var httpReqFailedSampleValue null.Bool346 for _, s := range sample.GetSamples() {347 if s.Metric.Name == metrics.HTTPReqFailedName {348 httpReqFailedSampleValue.Valid = true349 if s.Value == 1.0 {350 httpReqFailedSampleValue.Bool = true351 }352 }353 }354 require.Equal(t, failed, httpReqFailedSampleValue)355 })356 }357}358func TestMakeRequestDialTimeout(t *testing.T) {359 if runtime.GOOS == "windows" {360 t.Skipf("dial timeout doesn't get returned on windows") // or we don't match it correctly361 }362 t.Parallel()363 ln, err := net.Listen("tcp", "localhost:0")364 if err != nil {365 t.Fatal(err)366 }367 addr := ln.Addr()368 defer func() {369 require.NoError(t, ln.Close())370 }()371 ctx, cancel := context.WithCancel(context.Background())372 defer cancel()373 samples := make(chan stats.SampleContainer, 10)374 logger := logrus.New()375 logger.Level = logrus.DebugLevel376 registry := metrics.NewRegistry()377 state := &lib.State{378 Options: lib.Options{379 RunTags: &stats.SampleTags{},380 SystemTags: &stats.DefaultSystemTagSet,381 },382 Transport: &http.Transport{383 DialContext: (&net.Dialer{384 Timeout: 1 * time.Microsecond,385 }).DialContext,386 },387 Samples: samples,388 Logger: logger,389 BPool: bpool.NewBufferPool(100),390 BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),391 Tags: lib.NewTagMap(nil),392 }393 req, _ := http.NewRequest("GET", "http://"+addr.String(), nil)394 preq := &ParsedHTTPRequest{395 Req: req,396 URL: &URL{u: req.URL, URL: req.URL.String()},397 Body: new(bytes.Buffer),398 Timeout: 500 * time.Millisecond,399 ResponseCallback: func(i int) bool { return i == 0 },400 }401 res, err := MakeRequest(ctx, state, preq)402 require.NoError(t, err)403 assert.NotNil(t, res)404 assert.Len(t, samples, 1)405 sampleCont := <-samples406 allSamples := sampleCont.GetSamples()407 require.Len(t, allSamples, 9)408 expTags := map[string]string{409 "error": "dial: i/o timeout",410 "error_code": "1211",411 "status": "0",412 "expected_response": "true", // we wait for status code 0413 "method": "GET",414 "url": req.URL.String(),415 "name": req.URL.String(),416 }417 for _, s := range allSamples {418 assert.Equal(t, expTags, s.Tags.CloneTags())419 }420}421func TestMakeRequestTimeoutInTheBegining(t *testing.T) {422 t.Parallel()423 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {424 time.Sleep(100 * time.Millisecond)425 }))426 defer srv.Close()427 ctx, cancel := context.WithCancel(context.Background())428 defer cancel()429 samples := make(chan stats.SampleContainer, 10)430 logger := logrus.New()431 logger.Level = logrus.DebugLevel432 registry := metrics.NewRegistry()433 state := &lib.State{434 Options: lib.Options{435 RunTags: &stats.SampleTags{},436 SystemTags: &stats.DefaultSystemTagSet,437 },438 Transport: srv.Client().Transport,439 Samples: samples,440 Logger: logger,441 BPool: bpool.NewBufferPool(100),442 BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),443 Tags: lib.NewTagMap(nil),444 }445 req, _ := http.NewRequest("GET", srv.URL, nil)446 preq := &ParsedHTTPRequest{447 Req: req,448 URL: &URL{u: req.URL, URL: srv.URL},449 Body: new(bytes.Buffer),450 Timeout: 50 * time.Millisecond,451 ResponseCallback: func(i int) bool { return i == 0 },452 }453 res, err := MakeRequest(ctx, state, preq)454 require.NoError(t, err)455 assert.NotNil(t, res)456 assert.Len(t, samples, 1)457 sampleCont := <-samples...

Full Screen

Full Screen

state_test.go

Source:state_test.go Github

copy

Full Screen

...9func TestTagMapSet(t *testing.T) {10 t.Parallel()11 t.Run("Sync", func(t *testing.T) {12 t.Parallel()13 tm := NewTagMap(nil)14 tm.Set("mytag", "42")15 v, found := tm.Get("mytag")16 assert.True(t, found)17 assert.Equal(t, "42", v)18 })19 t.Run("Safe-Concurrent", func(t *testing.T) {20 t.Parallel()21 tm := NewTagMap(nil)22 ctx, cancel := context.WithCancel(context.Background())23 defer cancel()24 go func() {25 count := 026 for {27 select {28 case <-time.Tick(1 * time.Millisecond):29 count++30 tm.Set("mytag", strconv.Itoa(count))31 case <-ctx.Done():32 return33 }34 }35 }()36 go func() {37 for {38 select {39 case <-time.Tick(1 * time.Millisecond):40 tm.Get("mytag")41 case <-ctx.Done():42 return43 }44 }45 }()46 time.Sleep(100 * time.Millisecond)47 })48}49func TestTagMapGet(t *testing.T) {50 t.Parallel()51 tm := NewTagMap(map[string]string{52 "key1": "value1",53 })54 v, ok := tm.Get("key1")55 assert.True(t, ok)56 assert.Equal(t, "value1", v)57}58func TestTagMapLen(t *testing.T) {59 t.Parallel()60 tm := NewTagMap(map[string]string{61 "key1": "value1",62 "key2": "value2",63 })64 assert.Equal(t, 2, tm.Len())65}66func TestTagMapDelete(t *testing.T) {67 t.Parallel()68 m := map[string]string{69 "key1": "value1",70 "key2": "value2",71 }72 tm := NewTagMap(m)73 tm.Delete("key1")74 _, ok := m["key1"]75 assert.False(t, ok)76}77func TestTagMapClone(t *testing.T) {78 t.Parallel()79 tm := NewTagMap(map[string]string{80 "key1": "value1",81 "key2": "value2",82 })83 m := tm.Clone()84 assert.Equal(t, map[string]string{85 "key1": "value1",86 "key2": "value2",87 }, m)88}...

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tags := []string{"tag1", "tag2", "tag3"}4 tagmap := lib.NewTagMap(tags)5 fmt.Println(tagmap)6}

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1func main() {2 lib.NewTagMap("tag1", "tag2", "tag3")3}4func main() {5 lib.NewTagMap("tag1", "tag2", "tag3")6}7func main() {8 lib.NewTagMap("tag1", "tag2", "tag3")9}10func NewTagMap(tags ...string) map[string]string {11 m := make(map[string]string)12 for _, tag := range tags {13 }14}15func NewTagMap(tags ...string) map[string]string {16 m := make(map[string]string)17 for _, tag := range tags {18 }19}

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1func main() {2 t := lib.NewTagMap()3 t.Set("key", "value")4 fmt.Println(t.Get("key"))5}6type TagMap struct {7}8func NewTagMap() *TagMap {9 return &TagMap{m: map[string]string{}}10}11func (t *TagMap) Set(key, value string) {12}13func (t *TagMap) Get(key string) string {14}15import "testing"16func TestNewTagMap(t *testing.T) {17 tmap := NewTagMap()18 if tmap == nil {19 t.Fatalf("NewTagMap returned nil")20 }21}22The test file imports the library using the following line:23import "github.com/awslabs/aws-sdk-go/aws"24 /usr/local/go/src/pkg/github.com/awslabs/aws-sdk-go/aws (from $GOROOT)25 /Users/drew/Development/go/src/github.com/awslabs/aws-sdk-go/aws (from $GOPATH)26func TestFunction(t *testing.T) {27 f := func() {

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1func main() {2 tagMap := lib.NewTagMap()3 fmt.Println(tagMap)4}5import (6type TagMap struct {7}8func NewTagMap() *TagMap {9 fmt.Println("Creating a new TagMap")10 return &TagMap{}11}12&{}13func main() {14 tagMap := lib.NewTagMap()15}16main.main()

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tagMap := lib.NewTagMap("tag1", "tag2", "tag3")4 fmt.Println(tagMap)5}6type TagMap struct {7}8import (9func main() {10 tagMap := lib.NewTagMap("tag1", "tag2", "tag3")11 fmt.Println(tagMap)12}13type TagMap struct {14}15import (16func main() {17 tagMap := lib.NewTagMap("tag1", "tag2", "tag3")18 fmt.Println(tagMap)19}

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tmap := lib.NewTagMap()4 tmap.AddTag("mytag", "myvalue")5 fmt.Println(tmap)6}7import (8func main() {9 tmap := lib.NewTagMap()10 tmap.AddTag("mytag", "myvalue")11 fmt.Println(tmap)12}13import (14func main() {15 tmap := lib.NewTagMap()16 tmap.AddTag("mytag", "myvalue")17 fmt.Println(tmap)18}

Full Screen

Full Screen

NewTagMap

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib := lib.NewTagMap()4 lib.AddTag("Prasanna", "Srinivasan")5 fmt.Println(lib.GetTag("Prasanna"))6}7import (8func main() {9 lib := lib.NewTagMap()10 lib.AddTag("Prasanna", "Srinivasan")11 fmt.Println(lib.GetTag("Prasanna"))12}13import (14func main() {15 lib := lib.NewTagMap()16 lib.AddTag("Prasanna", "Srinivasan")17 fmt.Println(lib.GetTag("Prasanna"))18}19import (20func main() {21 lib := lib.NewTagMap()22 lib.AddTag("Prasanna", "Srinivasan")23 fmt.Println(lib.GetTag("Prasanna"))24}25import (26func main() {27 lib := lib.NewTagMap()28 lib.AddTag("Prasanna", "Srinivasan")29 fmt.Println(lib.GetTag("Prasanna"))30}

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.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful