How to use Context method of got Package

Best Got code snippet using got.Context

adapter_tx_test.go

Source:adapter_tx_test.go Github

copy

Full Screen

...12 defer adapter.Destruct()13 q1 := `insert into sample(name, password) values ('Success Data 1', 'pwd1')`14 q2 := `insert into sample(name, password) values ('Success Data 2', 'pwd2')`15 q3 := `insert into sample(name, password) values ('Success Data 3', 'pwd3')`16 r, err := adapter.WrapInTx(context.Background(), func(ctx context.Context) (interface{}, error) {17 r, err := adapter.Query(ctx, q1, nil)18 if err != nil {19 return r, err20 }21 r, err = adapter.Query(ctx, q2, nil)22 if err != nil {23 return r, err24 }25 r, err = adapter.Query(ctx, q3, nil)26 if err != nil {27 return r, err28 }29 return r, err30 })31 if err != nil {32 t.Error("Error running query")33 }34 result, ok := r.([]map[string]interface{})35 if !ok {36 t.Fatal("Result type mismatch")37 }38 need := 339 got := int(result[0][internal.LastInsertID].(int64))40 if got != need {41 t.Errorf("Need %d, got %d", need, got)42 }43}44// TestSingleTxFail tests for rolling back of the transaction when one query of the45// list fails.46func TestSingleTxFail(t *testing.T) {47 clearTestTable(t)48 adapter := newDBAdapter(t)49 defer adapter.Destruct()50 q1 := `insert into sample(name, password) values ('Success Query 1', 'pwd1')`51 q2 := `insert into non_existant_table(name, password) values ('Data to non existant table', 'pwd')` // failing query52 q3 := `insert into sample(name, password) values ('Success Query 3', 'pwd3')`53 _, err := adapter.WrapInTx(context.Background(), func(ctx context.Context) (interface{}, error) {54 r, err := adapter.Query(ctx, q1, nil)55 if err != nil {56 return r, err57 }58 r, err = adapter.Query(ctx, q2, nil)59 if err != nil {60 return r, err61 }62 r, err = adapter.Query(ctx, q3, nil)63 if err != nil {64 return r, err65 }66 return r, err67 })68 if err == nil {69 t.Errorf("Need error, got nil")70 }71 need := `Error 1146: Table 'sample.non_existant_table' doesn't exist`72 got := err.Error()73 if need != got {74 t.Errorf("Need %s, got %s", need, got)75 }76}77// TestMultipleTxSuccess tests for successfull execution of multiple transactions.78func TestMultipleTxSuccess(t *testing.T) {79 clearTestTable(t)80 adapter := newDBAdapter(t)81 defer adapter.Destruct()82 ctx := context.Background()83 q1 := `insert into sample(name, password) values ('Success Data 1', 'pwd1')`84 q2 := `insert into sample(name, password) values ('Success Data 2', 'pwd2')`85 // run q186 r, err := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {87 r, err := adapter.Query(ctx, q1, nil)88 if err != nil {89 return nil, err90 }91 return r, err92 })93 if err != nil {94 t.Error("Error running query 1")95 }96 result, ok := r.([]map[string]interface{})97 if !ok {98 t.Fatal("Result type mismatch")99 }100 need := 1101 got := int(result[0][internal.LastInsertID].(int64))102 if got != need {103 t.Errorf("Need %d, got %d", need, got)104 }105 // run q2106 r, err = adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {107 r, err = adapter.Query(ctx, q2, nil)108 if err != nil {109 return nil, err110 }111 return r, err112 })113 if err != nil {114 t.Error("Error running query 2")115 }116 result, ok = r.([]map[string]interface{})117 if !ok {118 t.Fatal("Result type mismatch")119 }120 need = 2121 got = int(result[0][internal.LastInsertID].(int64))122 if got != need {123 t.Errorf("Need %d, got %d", need, got)124 }125 // check whether all data is inserted126 r, err = adapter.Query(context.Background(), `select count(*) as count from sample`, nil)127 result, ok = r.([]map[string]interface{})128 if !ok {129 t.Fatal("Result type mismatch")130 }131 need = 2132 got = int(result[0]["count"].(int64))133 if got != need {134 t.Errorf("Need %d, got %d", need, got)135 }136}137// TestMultipleTxFail tests for multiple transactions in which one of them fails.138func TestMultipleTxFail(t *testing.T) {139 clearTestTable(t)140 adapter := newDBAdapter(t)141 defer adapter.Destruct()142 ctx := context.Background()143 q1 := `insert into sample(name, password) values (no quotes around this string, 'pwd')` // failing query144 q2 := `insert into sample(name, password) values ('Success Data 2', 'pwd2')`145 // run q1 (failing query)146 r, err := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {147 r, err := adapter.Query(ctx, q1, nil)148 if err != nil {149 return nil, err150 }151 return r, err152 })153 if err == nil {154 t.Errorf("Need error, got nil")155 }156 errNeed := "Error 1064"157 errGot := err.Error()[:10]158 if errNeed != errGot {159 t.Errorf("Need %s, got %s", errNeed, errGot)160 }161 // run q2162 r, err = adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {163 r, err = adapter.Query(ctx, q2, nil)164 if err != nil {165 return nil, err166 }167 return r, err168 })169 if err != nil {170 t.Error("Error running query 2")171 }172 result, ok := r.([]map[string]interface{})173 if !ok {174 t.Fatal("Result type mismatch")175 }176 need := 1177 got := int(result[0][internal.LastInsertID].(int64))178 if got != need {179 t.Errorf("Need %d, got %d", need, got)180 }181 // check whether all data is inserted182 r, err = adapter.Query(context.Background(), `select count(*) as count from sample`, nil)183 result, ok = r.([]map[string]interface{})184 if !ok {185 t.Fatal("Result type mismatch")186 }187 need = 1188 got = int(result[0]["count"].(int64))189 if got != need {190 t.Errorf("Need %d, got %d", need, got)191 }192}193// TestNestedTxSuccess tests for successful execution of nested transactions.194func TestNestedTxSuccess(t *testing.T) {195 clearTestTable(t)196 adapter := newDBAdapter(t)197 defer adapter.Destruct()198 ctx := context.Background()199 q1 := `insert into sample(name, password) values ('Success Data 1', 'pwd1')`200 q2 := `insert into sample(name, password) values ('Success Data 2', 'pwd2')`201 // run q1202 r, err := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {203 r1, err1 := adapter.Query(ctx, q1, nil)204 if err1 != nil {205 return nil, err1206 }207 // run q2208 r2, err2 := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {209 r2, err2 := adapter.Query(ctx, q2, nil)210 if err2 != nil {211 return nil, err2212 }213 return r2, err2214 })215 if err2 != nil {216 t.Error("Error running query 2")217 }218 result2, ok2 := r2.([]map[string]interface{})219 if !ok2 {220 t.Fatal("Result type mismatch")221 }222 need2 := 2223 got2 := int(result2[0][internal.LastInsertID].(int64))224 if got2 != need2 {225 t.Errorf("Need %d, got %d", need2, got2)226 }227 // HERE: return results of q1228 return r1, err1229 })230 if err != nil {231 t.Errorf("Error running query 1: %s", err.Error())232 }233 result, ok := r.([]map[string]interface{})234 if !ok {235 t.Fatal("Result type mismatch")236 }237 need := 1238 got := int(result[0][internal.LastInsertID].(int64))239 if got != need {240 t.Errorf("Need %d, got %d", need, got)241 }242 // check whether all data is inserted243 r, err = adapter.Query(context.Background(), `select count(*) as count from sample`, nil)244 if err != nil {245 t.Fatal(err.Error())246 }247 result, ok = r.([]map[string]interface{})248 if !ok {249 t.Fatal("Result type mismatch")250 }251 need = 2252 got = int(result[0]["count"].(int64))253 if got != need {254 t.Errorf("Need %d, got %d", need, got)255 }256}257// TestNestedTxInnerFail tests for the failure of inner operation of the nested transactions.258func TestNestedTxInnerFail(t *testing.T) {259 clearTestTable(t)260 adapter := newDBAdapter(t)261 defer adapter.Destruct()262 ctx := context.Background()263 q1 := `insert into sample(name, password) values ('Success Data 1', 'pwd1')`264 q2 := `insert into sample(name, password) values (no quotes around this string, 'pwd')` // failing query265 // run q1266 r, err := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {267 r1, err1 := adapter.Query(ctx, q1, nil)268 if err1 != nil {269 return nil, err1270 }271 // run q2272 _, err2 := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {273 r2, err2 := adapter.Query(ctx, q2, nil)274 if err2 != nil {275 return nil, err2276 }277 return r2, err2278 })279 if err2 == nil {280 t.Errorf("Need error, got nil")281 }282 errNeed := "Error 1064"283 errGot := err2.Error()[:10]284 if errNeed != errGot {285 t.Errorf("Need %s, got %s", errNeed, errGot)286 }287 // HERE: return results of q1288 return r1, err1289 })290 if err != nil {291 t.Errorf("Error running query 1: %s", err.Error())292 }293 result, ok := r.([]map[string]interface{})294 if !ok {295 t.Fatal("Result type mismatch")296 }297 need := 1298 got := int(result[0][internal.LastInsertID].(int64))299 if got != need {300 t.Errorf("Need %d, got %d", need, got)301 }302 // check whether all data is inserted303 r, err = adapter.Query(context.Background(), `select count(*) as count from sample`, nil)304 if err != nil {305 t.Fatal(err.Error())306 }307 result, ok = r.([]map[string]interface{})308 if !ok {309 t.Fatal("Result type mismatch")310 }311 need = 0312 got = int(result[0]["count"].(int64))313 if got != need {314 t.Errorf("Need %d, got %d", need, got)315 }316}317// TestNestedTxOuterFail tests for the failure of outer operation of the nested transactions.318func TestNestedTxOuterFail(t *testing.T) {319 clearTestTable(t)320 adapter := newDBAdapter(t)321 defer adapter.Destruct()322 ctx := context.Background()323 q1 := `insert into sample(name, password) values (no quotes around this string, 'pwd')` // failing query324 q2 := `insert into sample(name, password) values ('Success Data 2', 'pwd2')`325 // run q1326 _, err := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {327 r1, err1 := adapter.Query(ctx, q1, nil)328 if err1 != nil {329 return r1, err1330 }331 // run q2332 r2, err2 := adapter.WrapInTx(ctx, func(ctx context.Context) (interface{}, error) {333 r2, err2 := adapter.Query(ctx, q2, nil)334 if err2 != nil {335 return r2, err2336 }337 return r2, err2338 })339 if err2 != nil {340 t.Error("Error running query 2")341 }342 result2, ok2 := r2.([]map[string]interface{})343 if !ok2 {344 t.Fatal("Result type mismatch")345 }346 need2 := 2...

Full Screen

Full Screen

handler_test.go

Source:handler_test.go Github

copy

Full Screen

...4 "golang.org/x/net/context"5 "google.golang.org/grpc"6 "google.golang.org/grpc/metadata"7)8func newMetadataContext(ctx context.Context, val string) context.Context {9 md := metadata.Pairs(DefaultAcceptLangKey, val)10 return metadata.NewContext(ctx, md)11}12func TestHandleAcceptLanguage(t *testing.T) {13 table := []string{14 "da, en-gb;q=0.8, en;q=0.7",15 "da ,en-gb;q=0.8 ,en;q=0.7",16 "en;q=0.7, en-gb;q=0.8, da",17 }18 for i := range table {19 ctx := context.Background()20 acceptLangs := HandleAcceptLanguage(newMetadataContext(ctx, table[i]))21 t.Logf("header: %s", table[i])22 if got, want := len(acceptLangs), 3; got != want {23 t.Fatalf("expect len() = %d, but got %d", want, got)24 }25 al := acceptLangs[0]26 if got, want := al.Language, "da"; got != want {27 t.Fatalf("expect language = %q, but got %q", want, got)28 }29 if got, want := al.Quality, float32(1); got != want {30 t.Fatalf("expect quality = %f, but got %f", want, got)31 }32 al = acceptLangs[1]33 if got, want := al.Language, "en-gb"; got != want {34 t.Fatalf("expect language = %q, but got %q", want, got)35 }36 if got, want := al.Quality, float32(0.8); got != want {37 t.Fatalf("expect quality = %f, but got %f", want, got)38 }39 al = acceptLangs[2]40 if got, want := al.Language, "en"; got != want {41 t.Fatalf("expect language = %q, but got %q", want, got)42 }43 if got, want := al.Quality, float32(0.7); got != want {44 t.Fatalf("expect quality = %f, but got %f", want, got)45 }46 }47}48func TestHandleAcceptLanguageOrder(t *testing.T) {49 header := "en-gb, da, en"50 ctx := context.Background()51 acceptLangs := HandleAcceptLanguage(newMetadataContext(ctx, header))52 if got, want := len(acceptLangs), 3; got != want {53 t.Fatalf("expect len() = %d, but got %d", want, got)54 }55 al := acceptLangs[0]56 if got, want := al.Language, "en-gb"; got != want {57 t.Fatalf("expect language = %q, but got %q", want, got)58 }59 al = acceptLangs[1]60 if got, want := al.Language, "da"; got != want {61 t.Fatalf("expect language = %q, but got %q", want, got)62 }63 al = acceptLangs[2]64 if got, want := al.Language, "en"; got != want {65 t.Fatalf("expect language = %q, but got %q", want, got)66 }67}68type testServerStream struct {69 grpc.ServerStream70 ctx context.Context71}72func (ss *testServerStream) Context() context.Context {73 return ss.ctx74}75func (ss *testServerStream) SendMsg(m interface{}) error {76 return nil77}78func (f *testServerStream) RecvMsg(m interface{}) error {79 return nil80}81func TestUnaryServer(t *testing.T) {82 unaryInfo := &grpc.UnaryServerInfo{83 FullMethod: "TestService.UnaryMethod",84 }85 unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {86 acceptLangs := FromContext(ctx)87 if got, want := len(acceptLangs), 1; got != want {88 t.Fatalf("expect len() = %d, but got %d", want, got)89 }90 al := acceptLangs[0]91 if got, want := al.Language, "en"; got != want {92 t.Fatalf("expect language = %q, but got %q", want, got)93 }94 return "output", nil95 }96 ctx := context.Background()97 ctx = newMetadataContext(ctx, "en")98 _, err := UnaryServerInterceptor(ctx, "xyz", unaryInfo, unaryHandler)99 if err != nil {100 t.Fatalf("unexpected error: %v", err)101 }102}103func TestStreamServerWithoutRequestID(t *testing.T) {104 streamInfo := &grpc.StreamServerInfo{105 FullMethod: "TestService.StreamMethod",106 IsServerStream: true,107 }108 streamHandler := func(srv interface{}, stream grpc.ServerStream) error {109 acceptLangs := FromContext(stream.Context())110 if got, want := len(acceptLangs), 1; got != want {111 t.Fatalf("expect len() = %d, but got %d", want, got)112 }113 al := acceptLangs[0]114 if got, want := al.Language, "en"; got != want {115 t.Fatalf("expect language = %q, but got %q", want, got)116 }117 return nil118 }119 testService := struct{}{}120 ctx := context.Background()121 ctx = newMetadataContext(ctx, "en")122 testStream := &testServerStream{ctx: ctx}123 err := StreamServerInterceptor(testService, testStream, streamInfo, streamHandler)124 if err != nil {125 t.Fatalf("unexpected error: %v", err)126 }127}...

Full Screen

Full Screen

Context

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 defer cancel()5 go func() {6 time.Sleep(5 * time.Second)7 cancel()8 }()9 select {10 case <-time.After(10 * time.Second):11 fmt.Println("overslept")12 case <-ctx.Done():13 fmt.Println(ctx.Err())14 }15}16import (17func main() {18 d := time.Now().Add(50 * time.Millisecond)19 ctx, cancel := context.WithDeadline(context.Background(), d)20 defer cancel()21 select {22 case <-time.After(1 * time.Second):23 fmt.Println("overslept")24 case <-ctx.Done():25 fmt.Println(ctx.Err())26 }27}28import (29func main() {30 ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)31 defer cancel()32 select {33 case <-time.After(1 * time.Second):34 fmt.Println("overslept")35 case <-ctx.Done():36 fmt.Println(ctx.Err())37 }38}39import (40func main() {41 favContext := favContextKey("favorite number")42 ctx := context.WithValue(context.Background(), favContext, 7)43 num := ctx.Value(favContext).(int)44 fmt.Println(num)45}46import (47func main() {48 ctx, cancel := context.WithCancel(context.Background())49 defer cancel()50 go func() {51 time.Sleep(5 * time.Second)52 cancel()53 }()54 select {55 case <-time.After(10 * time.Second):56 fmt.Println("overslept")57 case <-ctx.Done():58 fmt.Println(ctx.Err())59 }60}

Full Screen

Full Screen

Context

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(stringutil.Reverse("hello, world"))4}5import (6func main() {7 fmt.Println(stringutil.Reverse("hello, world"))8}9import (10func main() {11 fmt.Println(stringutil.Reverse("hello, world"))12}13import (14func main() {15 fmt.Println(stringutil.Reverse("hello, world"))16}17import (18func main() {19 fmt.Println(stringutil.Reverse("hello, world"))20}21import (22func main() {23 fmt.Println(stringutil.Reverse("hello, world"))24}25import (26func main() {27 fmt.Println(stringutil.Reverse("hello, world"))28}29import (30func main() {31 fmt.Println(stringutil.Reverse("hello, world"))32}33import (34func main() {35 fmt.Println(stringutil.Reverse("hello, world"))36}37import (38func main() {39 fmt.Println(stringutil.Reverse("hello, world"))40}

Full Screen

Full Screen

Context

Using AI Code Generation

copy

Full Screen

1import "fmt"2type got struct {3}4func (g got) Context() string {5}6func main() {7 g := got{"game of thrones"}8 fmt.Println(g.Context())9}10import "fmt"11type got struct {12}13func (g got) Context() string {14}15func main() {16 g := got{"game of thrones"}17 fmt.Println(g.Context())18}19import "fmt"20type got struct {21}22func (g got) Context() string {23}24func main() {25 g := got{"game of thrones"}26 fmt.Println(g.Context())27}28import "fmt"29type got struct {30}31func (g got) Context() string {32}33func main() {34 g := got{"game of thrones"}35 fmt.Println(g.Context())36}37import "fmt"38type got struct {39}40func (g got) Context() string {41}42func main() {43 g := got{"game of thrones"}44 fmt.Println(g.Context())45}46import "fmt"47type got struct {48}49func (g got) Context() string {50}51func main() {52 g := got{"game of thrones"}53 fmt.Println(g.Context())54}55import "fmt"56type got struct {57}58func (g got) Context() string {59}60func main() {61 g := got{"game of thrones"}62 fmt.Println(g.Context())63}64import "fmt"65type got struct {66}67func (g got) Context() string {68}69func main() {70 g := got{"game of thrones"}71 fmt.Println(g.Context())72}73import "fmt"74type got struct {75}76func (g got) Context() string {77}78func main() {79 g := got{"game of thrones"}80 fmt.Println(g.Context())81}

Full Screen

Full Screen

Context

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 fmt.Println("main: Started")5 fmt.Println("main: About to do something for a while")6 go doSomething(ctx)7 time.Sleep(5 * time.Second)8 fmt.Println("main: Time to shut down")9 cancel()10 time.Sleep(5 * time.Second)11 fmt.Println("main: Completed")12}13func doSomething(ctx context.Context) {14 fmt.Println("doSomething: Started")15 for {16 select {17 case <-ctx.Done():18 fmt.Println("doSomething: Time to stop")19 fmt.Println("doSomething: Doing something")20 time.Sleep(1 * time.Second)21 }22 }23}24func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)25import (26func main() {27 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)28 fmt.Println("main: Started")29 fmt.Println("main: About to do something for a while")30 go doSomething(ctx)31 time.Sleep(10 *

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